我有一个非常奇怪的退出问题。
工作流程:
我正在使用Session和cookies来登录。
注销用户尝试登录后,问题只发生一次
他成功地达到了他的主视图(每个用户都不同)
成功选择任务,进入任务视图,输入提交
然后,他收到"退出,而不是下一页,请登录"声明,意思是我的" checkUser"没有找到一个会话或一个cookie并踢出他。
当他下次登录时,一切正常。
我不明白从哪里开始寻找这个问题。
我的登录页面相关代码:
session_start();
$error_msg = "";
//Do you have Session or cookies?
if (isset($_SESSION['user_id']) && isset( $_SESSION['user_role']))
{
If ($_SESSION['user_role']=='DM')
header('Location: DMView.php');
else if ($_SESSION['user_role']=='Vendor')
header('Location: VendorView.php');
exit;
}
//If you dont - Did you enter sumbit?
if (!isset($_SESSION['user_id']) && isset($_POST['submit']))
{
// Grab the user-entered log-in data
$user_username = mysqli_real_escape_string($con, trim($_POST['username']));
$user_password = mysqli_real_escape_string($con, trim($_POST['password']));
if (!empty($user_username) && !empty($user_password)) {
// Look up the username and password in the database
$query = "SELECT * FROM Users WHERE UserName = '$user_username' AND UserPassword = SHA('$user_password')";
$data = mysqli_query($con, $query);
if (mysqli_num_rows($data) == 1) {
// The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page
$row = mysqli_fetch_array($data);
$_SESSION['user_id'] = $row['UserID'];
$_SESSION['username'] = $row['UserName'];
setcookie('user_id', $row['UserID'], time() + (60 * 60 * 24 * 30)); // expires in 30 days
setcookie('username', $row['UserName'], time() + (60 * 60 * 24 * 30)); // expires in 30 days
$user_role = $row['UserRole'];
$_SESSION['user_role'] = $row['UserRole'];
setcookie('user_role', $row['UserRole'], time() + (60 * 60 * 24 * 30)); // expires in 30 days
$_SESSION['user_group'] = $row['UserGroup'];
setcookie('user_group', $row['UserGroup'], time() + (60 * 60 * 24 * 30)); // expires in 30 days
If ($user_role=='DM')
header('Location: DMView.php');
else
header('Location: VendorView.php');
}
else {
// The username/password are incorrect so set an error message
$error_msg = 'Sorry, you must enter a valid username and password to log in.';
}
}
else {
// The username/password weren't entered so set an error message
$error_msg = 'Sorry, you must enter your username and password to log in.';
}
}
我的checkUser文件:
if (session_status() == PHP_SESSION_NONE)
{
session_start();
}
// If the session vars aren't set, try to set them with a cookie
if (!isset($_SESSION['user_id'])) {
if (isset($_COOKIE['user_id']) && isset($_COOKIE['username'])) {
$_SESSION['user_id'] = $_COOKIE['user_id'];
$_SESSION['username'] = $_COOKIE['username'];
$_SESSION['user_role']=$_COOKIE['user_role'];
$_SESSION['user_group'] = $_COOKIE['user_group'];
}
}
if ((!isset($_SESSION['user_id'])) ) {
echo '<p>Please <a href="index.php">log in</a> to access this page.</p>';
exit();}
和我的退出文件:
// If the user is logged in, delete the session vars to log them out
session_start();
if (isset($_SESSION['user_id'])) {
// Delete the session vars by clearing the $_SESSION array
$_SESSION = array();
// Delete the session cookie by setting its expiration to an hour ago (3600)
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time() - 3600);
}
// Destroy the session
session_destroy();
}
// Delete the user ID and username cookies by setting their expirations to an hour ago (3600)
setcookie('user_id', '', time() - 3600);
setcookie('username', '', time() - 3600);
// Redirect to the home page
header('location: index.php');
提交文件(仅用一个查询简化):
<?php
require('connection.php');
require ('checkUser.php');
$task=$_POST['task_id'];
$row=$_POST['row_id'];
if( $_POST['answer']==1 )
{
$query="UPDATE ecnmatchingdetails SET RowStatus=2,Agent='".$uName."' , VendorAnswer='Yes', VendorComment='".$_POST['comments']."' , end_tag='".date("Y-m-d H:i:s")."' where TaskID=".$task." and RowId=".$row;
mysqli_query($con, $query);
}
else...
}
if( isset( $_POST['answer'])) {
header( 'Location: http://dub-entas-124/tool/TT/WorkOnTask.php?id='.$task . '&start_task=0&prevID='.$Ebay_PRD_ID);
exit();
}
?>
答案 0 :(得分:1)
如果我猜你的问题是正确的, 您正在注册cookie并在您正在检查的同一页面中,存储在客户端的cookie,只有在重新加载后或在其他页面上才可用...
答案 1 :(得分:0)
我有一段时间这个问题,到处都是。
我所做的一切都是为了解决这个问题,在我的php.ini
中进行了修改session.cookie_domain = ".example.com"
将它放在session_start()之前,它应位于PHP的最顶端
ini_set('session.cookie_domain', '.example.com' );
session_start();