PHP web登录未保持用户登录。
我在我的一些网站上使用了这段PhP代码。但由于某种原因,我产生了一个小问题 现在,如果您尝试转到受此代码保护的页面,您将被重定向到index.php页面,其中有一个供您登录的论坛。一旦用户登录,它会将他们带到home.php,这一切都很好。但问题是,如果用户转到settings.php或其他页面,而不是主页,我会说40%的时间会将他们重定向回index.php页面重新登录。我以为这是时间的安排,但你可以登录并立即转到add.php,它会将你重定向到index.php,如果我返回一个页面(back to home.php)并单击另一个页面(example remove.php),它会允许我去那里。我不明白为什么它有时有效,有时无效 代码在我使用的所有页面上都是相同的,以请求下面的代码。 connection.phpPHP web登录未保持用户登录。,php,mysql,Php,Mysql,我在我的一些网站上使用了这段PhP代码。但由于某种原因,我产生了一个小问题 现在,如果您尝试转到受此代码保护的页面,您将被重定向到index.php页面,其中有一个供您登录的论坛。一旦用户登录,它会将他们带到home.php,这一切都很好。但问题是,如果用户转到settings.php或其他页面,而不是主页,我会说40%的时间会将他们重定向回index.php页面重新登录。我以为这是时间的安排,但你可以登录并立即转到add.php,它会将你重定向到index.php,如果我返回一个页面(back
<?php
$con = new mysqli("localhost","****","********","********");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "9,10";
$MM_donotCheckaccess = "false";
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;
// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && false) {
$isValid = true;
}
}
return $isValid;
}
$MM_restrictGoTo = "index.php?error=3";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
$MM_referrer .= "?" . $QUERY_STRING;
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
限制访问.php
<?php
$con = new mysqli("localhost","****","********","********");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "9,10";
$MM_donotCheckaccess = "false";
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;
// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && false) {
$isValid = true;
}
}
return $isValid;
}
$MM_restrictGoTo = "index.php?error=3";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
$MM_referrer .= "?" . $QUERY_STRING;
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
我会尝试删除对isset($\u SESSION)的检查,我会假设该检查将返回true,并且从未调用SESSION\u start $\u会话是PHP中注册的全局会话,PHP总是创建它。即使未调用session_start,并且对于空数组(),isset返回true,也可以修改此全局设置 请让我知道删除isset检查是否对您有效 只要简单地改变这个
if (!isset($_SESSION)) {
session_start();
}
简单地说
session_start();
我猜它有时会起作用,而不是其他时候,因为您正在手动设置会话变量,然后检查它是否会起作用,但是,会话没有写入文件系统或数据库,因此页面有时会请求另一个脚本,也就是说它会失败,因为这是一个新的页面请求,并且会话没有在页面请求之间写入。将
会话_start()
放在页面顶部;不要先检查它。谢谢你,我现在正在尝试。。。我将公布结果。