开发者

Allowing the user to login once every 24 hours [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

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 Tim开发者_C百科e 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.

$timestamp = strtotime('-24 hours', currentMysqlTime($dbh)); 
$allowedToLogin = (bool) $dbh->fetchOne('SELECT 1 FROM  * FROM userpoints WHERE uid = ? AND timestamp <= ?', array($uid, $timestamp)); // Assuming ZF syntax here

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.


For your use, you probably want to use DATEDIFF instead of TIMEDIFF. You're currently checking how many hours it is since the user logged in, not how many days.

Also, this code

if($daysSinceLastLogin < 0) {

is wrong - for one thing, you're taking a string "-00:12:34.123456", then taking the first part of that - "-00". Then you're converting it to an integer. Since there's no expression for negative zero as an integer, you just get 0, so your condition is never true in this context.

The other reason that it's wrong is that you want to be checking if it's less than 1 (hour), not less than 0 days.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜