首页 > 解决方案 > 延长会话令牌 PHP 的到期时间

问题描述

我试图在用户登录时将会话存储在用户的本地存储中,但是会话的到期时间很短,每次我路由到另一个页面时它都会被删除,我无法弄清楚出了什么问题。下面是我的一些代码片段。

连接.php

<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "ezcar2";

$conn = mysqli_connect($host, $username, $password, $database);
?>

ct_home.php

<body>
<?php
        include_once 'connect.php';
        session_start();

        
        if (!isset($_SESSION['username'])) {
            header("Location: ct_login.php");
            exit();
        } 
    ?>
</body>

每当我刷新页面时,我都会被重定向回ct_login.php. 我希望会话一直持续到用户注销。

编辑(ct_login.php && 设置会话)

<?php
        include_once 'connect.php';
        session_start();
        
        if(isset($_POST['btnlogin'])){
            $c_username = trim($_POST['txtusername']);
            $c_password = trim($_POST['txtpwd']);
            $sql_query = "SELECT * FROM tblcustomer WHERE CT_USERNAME = '$c_username' AND CT_PASSWORD = '$c_password'";
            $sql_role = "SELECT * FROM tblcustomer WHERE CT_USERNAME = '$c_username' AND CT_PASSWORD = '$c_password' AND CT_ROLE = 'CAR OWNER'";
            $sql_status = "SELECT * FROM tblcustomer WHERE CT_USERNAME = '$c_username' AND CT_PASSWORD = '$c_password' AND CT_STATUS = 'APPROVED'";
            
            if($result = mysqli_query($conn, $sql_query)){
                $rows = mysqli_num_rows($result);
                if($rows == 1) {
                    if ($status = mysqli_query($conn, $sql_status)) {
                            $row_ = mysqli_num_rows($status);
                                if($row_ == 1) {
                                    if($role = mysqli_query($conn, $sql_role)){
                                     $rows_ = mysqli_num_rows($role);
                                     if($rows_ == 1) {
                                        //store username & password in session variable
                                        $rec = mysqli_fetch_row($role);
                                        $_SESSION['username'] = $rec[7];
                                        $_SESSION['role'] = $rec[9];
                                        header("Location: ct_home.php");
                                        // session_start();
                                } else {
                                        $rec = mysqli_fetch_row($result);
                                        $_SESSION['username'] = $rec[7];
                                        $_SESSION['role'] = $rec[9];
                                        header("Location: ct_home.php");
                                        // session_start();
                                }
                            }
                        } else {
                            echo('<script>alert("Account request is still pending. Please wait for confirmation email.");</script>');
                            echo "<meta http-equiv='refresh' content='0'>";
                            exit();
                        }
                    } 
                } else {
                        echo('<script>alert("Invalid Credentials. Please try again!");</script>');
                        echo "<meta http-equiv='refresh' content='0'>";
                        exit();
                        }
            }
        }
    ?>

编辑(#1):数据库连接用于验证用户登录

请让我知道我还能提供什么来更清楚地了解整个情况。提前谢谢了。

标签: phphtmlsessiontoken

解决方案


推荐阅读