Php 未授予发布者对管理页面链接的访问权限
我使用这个会话逻辑,如果用户不是管理员,那么他就不必访问管理员页面功能,这是好的会话逻辑吗?方法或我应该使用另一个请指导我进一步。看这个索引页,我有一些链接必须访问其他成员,然后管理员和管理员的所有链接请告诉我你正在使用的url组件中有哪些链接Php 未授予发布者对管理页面链接的访问权限,php,mysql,Php,Mysql,我使用这个会话逻辑,如果用户不是管理员,那么他就不必访问管理员页面功能,这是好的会话逻辑吗?方法或我应该使用另一个请指导我进一步。看这个索引页,我有一些链接必须访问其他成员,然后管理员和管理员的所有链接请告诉我你正在使用的url组件中有哪些链接 <?php include "config.php"; session_start(); if( (!isset($_SESSION['username'])) && (!isset($_SESSION['type'
<?php
include "config.php";
session_start();
if( (!isset($_SESSION['username'])) && (!isset($_SESSION['type'])) ){
header('location:login.php');
}
if($_SESSION['type'] != 'Administrator')
{
header('location:index.php');
}
?>
index.php
<?php
include "config.php";
session_start();
if(!isset($_SESSION['username']))
{
header('location:login.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3">
<div class="list-group">
<a href="#" class="list-group-item">Article</a>
<a href="#" class="list-group-item">Categories</a>
<?php
if($_SESSION['type']=='Administrator'){
?>
<a href="#" class="list-group-item">Media</a>
<a href="#" class="list-group-item">tempelate</a>
<a href="test.php" class="list-group-item">Setting</a>
<?php
}else{
?>
<a href="#" class="list-group-item">Profile</a>
<?php
}
?>
<a href="logout.php" class="list-group-item">Logout</a>
</div>
</div>
</body>
</html>
index.php
由于您不希望用户访问管理页面,因此可能需要先进行一次更为稳健的检查,以确定当前页面是否确实是admin.php
。如果是,则可以验证类型
,以便在授予访问权限之前知道它是否确实设置为管理员
如果未设置或设置为其他内容,则用户可能会返回到index.php
页面
<?php
include "config.php";
session_start();
$url_components = explode('/', $_SERVER['SCRIPT_NAME']);
$current_url = $url_components[count($url_components) - 1];
if(!isset($_SESSION['username'])){
header('location:login.php');
}
if(!($current_url == 'admin.php'
&& isset($_SESSION['type'])
&& $_SESSION['type'] == 'Administrator')){
header('location:index.php');
}
?>
我添加了index.php代码,请检查我对explode什么scrpt名称和当前url感到困惑?我使用的东西提供了运行脚本的URL的完整路径。例如:http://localhost/project/file.php
。我们使用基于/
的方法将其拆分,以在$current\u url
变量中获得file.php
。现在,如果$current\u url
是admin.php
,则可以进行其余检查,以了解是否必须授予访问权限。如果您仍然感到困惑,请尝试对这些变量中的每一个进行print\r
或echo
,您可以看到它们是如何使用的。让我检查:)检查上面我添加了此代码,但它无法访问test.php页面,该页面必须授予管理员$\u服务器['http://localhost/test/index.php']
-这是不正确的。请将其设置为$\u服务器['SCRIPT\u NAME']
,然后,为了进行测试,请尝试转到http://localhost/test/index.php
或http://localhost/test/test.php
或http://localhost/test/test.php
通过键入地址或单击超链接。对于不同的用户类型,请尝试这样做,只有当类型为管理员时,才允许您使用admin.php
。