php - 延长会话令牌 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):数据库连接用于验证用户登录
请让我知道我还能提供什么来更清楚地了解整个情况。提前谢谢了。
解决方案
推荐阅读
- r - 来自df中多列的grep向量并在R中打印第n列
- c# - 来自 Assembly 的 Autofac 寄存器类型
- rest - HttpClient.post 的离子问题(不存在“Access-Control-Allow-Origin”标头)
- sql - 在 Oracle/H2 中以标准方式规范化或存储 SQL 时间戳
- ios - Swift AWSPolly 中的错误处理
- powershell - 如何检索原始的、未解析的(未扩展的)注册表值?
- c++ - C++ 中的 cURL POST 请求
- c# - C#读取excel文件>100MB
- angular - 如何在 Angular 4 中使用 OpenLayers 5 动态地将弹出窗口添加到标记?
- python - 为什么“(值)!= range(0,256)”不检测0-256范围之外的值?