php 页面受角色保护但不针对访客
php page protected with roles but not agains guests
我正在尝试使用角色保护一些 php 页面。当人们登录时,我使用的代码可以正常工作,但如果他们知道 link 并且不进行登录,他们就可以访问该页面,但我不明白为什么。
谁能帮帮我?
我正在使用此代码来保护只有具有 "admin" 角色的用户才能访问的页面。
<?php
// Initialize the session
session_start();
// If session variable is not set it will redirect to login page
if(isset($_SESSION['username'])){if ($_SESSION['role']=='admin') {
} else {
header('location: index.php');
}
}
?>
试试这个:
<?php
// Initialize the session
if(!isset($_SESSION)) {
session_start();
}
// If session variable is not set it will redirect to login page
if(empty($_SESSION['username'])) {
header('Location: index.php');
} else {
if ($_SESSION['role'] != 'admin') {
header('Location: index.php');
}
}
?>
我正在尝试使用角色保护一些 php 页面。当人们登录时,我使用的代码可以正常工作,但如果他们知道 link 并且不进行登录,他们就可以访问该页面,但我不明白为什么。
谁能帮帮我?
我正在使用此代码来保护只有具有 "admin" 角色的用户才能访问的页面。
<?php
// Initialize the session
session_start();
// If session variable is not set it will redirect to login page
if(isset($_SESSION['username'])){if ($_SESSION['role']=='admin') {
} else {
header('location: index.php');
}
}
?>
试试这个:
<?php
// Initialize the session
if(!isset($_SESSION)) {
session_start();
}
// If session variable is not set it will redirect to login page
if(empty($_SESSION['username'])) {
header('Location: index.php');
} else {
if ($_SESSION['role'] != 'admin') {
header('Location: index.php');
}
}
?>