mysql查询:如果到期日=当前日期+登录时重定向?

时间:2013-01-03 15:03:53

标签: php mysql login

我正在尝试向我的登录添加一个查询,该查询检查我的sql表中的'subscription_expires'列。这基本上检查用户成员资格是否已过期,如果日期是当前日期+那么它应该重定向到另一个页面?

这是我的代码,但我不知道如何检查subscription_expires日期是否为当前日期+

有人可以告诉我怎么做这个吗?感谢。

// Check database to see if email and the hashed password exist there.
            $query = "SELECT id, email, close_account ";
            $query .= "FROM ptb_users ";
            $query .= "WHERE email = '{$email}' ";
            $query .= "AND password = '{$hashed_password}' ";
            $query .= "AND close_account = '0' ";
            $query .= "LIMIT 1";
            $result_set = mysql_query($query);
            confirm_query($result_set);
            if (mysql_num_rows($result_set) == 1) {
                // email/password authenticated
                // and only 1 match
                $found_user = mysql_fetch_array($result_set);
                $_SESSION['user_id'] = $found_user['id'];
                $_SESSION['email'] = $found_user['email'];
                $_SESSION['sub_expires'] = $found_user['subscription_expires'];
                $_SESSION['logged_in'] = true;

                $result = mysql_query("UPDATE ptb_users SET user_online='Online' WHERE id=".$_SESSION['user_id']."") 
or die(mysql_error());



                redirect_to("dashboard.php");

3 个答案:

答案 0 :(得分:0)

您可以尝试这样 - (在条件中添加subscription_expire)

// Check database to see if email and the hashed password exist there.
            $query = "SELECT id, email, close_account ";
            $query .= "FROM ptb_users ";
            $query .= "WHERE email = '{$email}' ";
            $query .= "AND password = '{$hashed_password}' ";
            $query .= "AND close_account = '0' ";
            $query .= "AND subscription_expires < now() ";
            $query .= "LIMIT 1";

我认为“subscription_expires”是时间戳/日期时间字段。

答案 1 :(得分:0)

这将是您根据存储时间完成重定向所需的逻辑。

<强> PHP

// If you store a timestamp
$sub_time = $found_user['subscription_expires'];

// If you store a string date time
$user_time = new DateTime($found_user['subscription_expires']);
$sub_time = $user_time->format("U");

if($sub_time <= time())
{
    // Assuming this function works
    redirect_to("mypage.php");
}

<强>解释

我为存储的unix时间戳和存储的日期时间提供了一个工具。基本上我们验证订阅时间是否小于或等于当前时间(这意味着已过期)并使用重定向功能将其发送出去。

答案 2 :(得分:0)

我不确定您的重定向功能包含哪些内容,但您可以使用header()相应地重定向用户。

此代码:

$_SESSION['sub_expires'] = $found_user['subscription_expires'];
$_SESSION['logged_in'] = true;

将替换为:

$_SESSION['sub_expires'] = $found_user['subscription_expires'];

if($_SESSION['sub_expires'] <= time()){
    header('Location: login.php');
    exit();
}else{
    $_SESSION['logged_in'] = true;
}

如果到期时间<=到当前时间,则此修改将导致重定向用户并退出页面。否则,用户仍然登录,因此请继续并设置logged_in会话变量。