What I am trying to do is give user access to an application once every 24 hours.
Once they log out they have to wait another 24 hours before they can log in to.
But what I have notices is if it has been longer then an hour it works.
But if it has been less then an hour it still gives them access which I don’t want to happen.
What can I do to fix this code?
$dsn = 'mysql:dbname=DBNAME;host=127.0.01';
$user = 'USER';
$password = 'PASSWORD';
try {
$dbh = new PDO($dsn, $user, $password); }
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
function currentMysqlTime($dbh) {
$sql = $dbh->prepare("SELECT CURRENT_TIMESTAMP()");
$sql->execute();
$x = $sql->fetchAll();
return $x[0][0];
}
function lastLoginTime($dbh, $id) {
$sql = $dbh->prepare("SELECT * FROM userpoints WHERE uid = ? AND pid = 2 ORDER BY timestamp desc LIMIT 1");
$sql->execute(array($id));
$y = $sql->fetchAll();
return $y[0][3];
}
function mysqlTimeDiff($dbh, $x, $y) {
$sql = $dbh->prepare("SELECT TIMEDIFF(?, ?)");
$sql->execute(array($x, $y));
$z = $sql->fetchAll();
return $z[0][0];
}
function daysSinceLastLogin($x) {
$parts = explode(':',$x);
return $parts[0];
}
$currentMysqlTime = currentMysqlTime($dbh);
$lastLoginTime = lastLoginTime($dbh, $id);
$timeDiff = mysqlTimeDiff($dbh, $currentMysqlTime, $lastLoginTime);
$daysSinceLastLogin = daysSinceLastLogin($timeDiff);
if($daysSinceLastLogin < 0) {
//NO ACCESS AS THE USER HAS ALREADY LOGGED IN
} else {
//GRANT ACCESS AS IT IS THERE FIRST TIME LOGGING IN TO THE APPLICATION
}
This is the output of the functions
Time of Registration 2011-09-16 07:10:04
Current Mysql Time 2011-09-17 05:41:22
Last Login Time 2011-09-14 05:00:07
Time Difference 72:41:15
Date Difference 3
With the code below will give you a boolean you can use to determine the authorization to log in.
If there is a match for a timestamp inferior to the reference, you’re good to go for a login by verifying the boolean
$allowedToLogin.