非常感谢您的阅读 我使用了以下代码,由DW CS 6.0生成 - 登录用户ServerBehaviour - ,用两个用户组(即用户和管理员)开发受密码保护的站点。该网站仅用于演示目的(因为DW生成PHP - MYSQL代码已过时 - 没有PDO或MySQLi)
数据库连接代码:
$hostname_imerida = "xxxxx";
$database_imerida = "xxxxx";
$username_imerida = "xxxxx";
$password_imerida = "xxxxx";
$imerida = mysql_pconnect($hostname_imerida, $username_imerida, $password_imerida) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_set_charset('utf8',$imerida);
date_default_timezone_set('Europe/Athens');`
如果凭据正确,DW生成的用于登录网站的代码:
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
session_start();
}
$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}
if (isset($_POST['username'])) {
$loginUsername=$_POST['username'];
$password=$_POST['password'];
$MM_fldUserAuthorization = "role";
$MM_redirectLoginSuccess = "managepub.php";
$MM_redirectLoginFailed = "index.php";
$MM_redirecttoReferrer = true;
mysql_select_db($database_imerida, $imerida);
$LoginRS__query=sprintf("SELECT username, password, role FROM users WHERE username=%s AND password=%s",
GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
$LoginRS = mysql_query($LoginRS__query, $imerida) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
$loginStrGroup = mysql_result($LoginRS,0,'role');
if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;
if (isset($_SESSION['PrevUrl']) && true) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>
检查(在这种情况下)管理员是否已登录的代码段,如果没有,则检查 强制从您尝试访问的页面注销:
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "admin";
$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";
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($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)
$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
网站在localhost上运行良好(XAMPP 1.7.7 [PHP:5.3.8])。 在生产环境(使用PHP版本5.3.3的Linux)中,要么需要两次或三次或四次尝试才能使用正确的密码登录或(假设您以管理员身份登录)登录并尝试访问页面后重定向到您再次到初始页面再次输入凭证(请用作以下参考)
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
实验表明,如果我评论出来
if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
生产服务器中的问题得到缓解,并且经常发生。这指向了我的方向
服务器中的会话处理有问题,但我无法想出接下来要做的事情或为什么会这样。
请分享您可能有的任何想法。
非常感谢。
答案 0 :(得分:0)
我建议调试网络标头(Chrome中的F12)并查看问题所在。
似乎在生产服务器上偶尔会有来自服务器的奇怪的重定向响应。
我建议查看Apache配置文件。在OP的情况下,托管提供商负责Apache配置。 OP与托管服务提供商联系并解决了他的问题。托管服务提供商的会话管理存在问题。