[英]user can access admin page using session in php
我有一个带有表用户的数据库。 在用户表上,我的用户名值为“ corny”和username =“ admin”。
在我的user.php上,我有此代码
if(!isset($_SESSION['username'])){
header('location: index.php');
}
在我的admin.php上,我有这个会议
if(!isset($_SESSION['username'])){
if($_SESSION['username'] != "gft-admin"){
header('location: index.php');
}
}
所以我的问题是,从users.php我可以访问admin.php。 如果用户不等于“ admin”,应如何避免users.php访问admin.php。
删除!
在第二段代码中,并为未设置$_SESSION['username']
添加else语句。
if(isset($_SESSION['username'])){ //remove here
if($_SESSION['username'] != "gft-admin"){
header('location: index.php');
}
} else {
header('location: index.php');
}
这是因为if
已设置会话,则if
完全阻止,则会绕开管理员。 我认为您只需要删除!
标记。
您当前的逻辑是在说:
IF there is no username
THEN if the username is not gft-admin
THEN REDIRECT
但是您想说的是:
IF there is no username
OR if the username is not gft-admin
THEN redirect
您要表达的意思是:
if(!isset($_SESSION['username']) || $_SESSION['username'] != "gft-admin"){
header('location: index.php');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.