Php 无法限制管理寻呼机的会话
我在管理部分工作,它有一个带有登录id和pasword表单的登录页面。 在我的管理部分,我有很多页面,比如login.php、st_admin.php、tmg_admin.php等 如果我必须访问st_admin.php页面,那么我只需键入链接就可以访问它 无需输入登录用户和密码 我想限制通过管理面板只访问所有页面。任何人都不能通过直接键入url来访问任何页面。这怎么可能 login.php代码:Php 无法限制管理寻呼机的会话,php,login,session-variables,restrict,Php,Login,Session Variables,Restrict,我在管理部分工作,它有一个带有登录id和pasword表单的登录页面。 在我的管理部分,我有很多页面,比如login.php、st_admin.php、tmg_admin.php等 如果我必须访问st_admin.php页面,那么我只需键入链接就可以访问它 无需输入登录用户和密码 我想限制通过管理面板只访问所有页面。任何人都不能通过直接键入url来访问任何页面。这怎么可能 login.php代码: <?php require_once('conn/conn.php'); ?> &
<?php require_once('conn/conn.php'); ?>
<?php
// *** Validate request to login to this site.
session_unset();
session_start();
$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($accesscheck)){
$GLOBALS['PreUrl']=$accesscheck;
session_register('PreUrl');
}
if (isset($_POST['staffid'])) {
$loginUsername=$_POST['staffid'];
$password=md5($_POST['password']);
$MM_fldUserAuthorization = "accessid";
$MM_redirectLoginSuccess = "staff/st_admin.php";
$MM_redirectLoginFailed = "login.php?error=1";
$MM_redirecttoReferrer = false;
mysql_select_db($database_conn, $conn);
$LoginRS__query=sprintf("SELECT staffid, password, teamcode, accessid FROM tbl_staff WHERE staffid='%s' AND password='%s'",
get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));
$LoginRS = mysql_query($LoginRS__query, $conn) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
$loginStrGroup = mysql_result($LoginRS,0,'accessid');
//declare two session variables and assign them
$GLOBALS['MM_Username'] = $loginUsername;
$GLOBALS['MM_UserGroup'] = $loginStrGroup;
//register the session variables
session_register("MM_Username");
session_register("MM_UserGroup");
if (isset($_SESSION['PrevUrl']) && false) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
switch ($loginStrGroup) {
case $MM_fldUserAuthorization='2':
$_SESSION['staffid']=$loginUsername;
header("Location: tutor/tr_admin.php");
break;
case $MM_fldUserAuthorization='3':
$_SESSION['staffid'] = $loginUsername;
header("Location: manager/mg_calendar.php");
break;
default:
$_SESSION['staffid'] = $loginUsername;
header("Location: " . $MM_redirectLoginSuccess );
}
} else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>
st_admin.php代码是:
<?php
session_start();
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";
// *** 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 == "") && true) {
$isValid = true;
}
}
return $isValid;
}
?>
非常感谢您的建议。通常我有一个authentication.php文件,包含在每个需要验证的页面上。此类应在加载页面之前检查会话是否经过身份验证 如果用户未通过身份验证,则可以设置全局,或者干脆死掉并显示错误消息 如果您有一个admin后端,那么您可能需要第二个authentication.php来处理admin 如果你网站的每个页面都有这个,那么就没有人能够直接访问URL
也可能有其他/更好的方法通过apache HTTP身份验证来实现这一点。如果您有一个文件可以或确实可以包含在所有其他页面中,您可以:
define("NOACCESS", "dkgjeifnt");//just some random text
现在,在您希望停止直接访问的页面中,您可以添加:
if (! defined("NOACCESS"));
{
echo "You can not access this page directly"
return;
}
如果有人直接在URL中输入,那么referer是空的,所以可以检查一下
if (!isset($_SERVER["HTTP_REFERER"]))
{
header("Location: index.php");
return;
}
如果没有设置http引用程序,这会将它们发送到您希望的任何地方我想确保代码将添加到St.admin.php上,并在返回$isValid;}后添加?您应该将其添加到您试图限制访问的任何文件的顶部。这样当脚本到达那一行时就停止执行了,现在我想起来了,这很好,但对于include,我的不好。我创建了一个新答案,并将保留此答案,以防有人想尝试,但这是我的新答案: