0

我正在尝试向我的登录添加一个查询,以检查我的 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");
4

3 回答 3

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会话变量。

于 2013-01-03T15:21:32.463 回答
0

您可以这样尝试-(在 where 条件下添加了 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”是一个时间戳/日期时间字段。

于 2013-01-03T15:06:35.717 回答
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 时间戳和存储的日期时间提供了一个工具。基本上我们验证订阅时间小于或等于当前时间(这意味着过期)并使用您的重定向功能将它们发送出去。

于 2013-01-03T15:13:09.520 回答