我正在写一个小型项目,一个页面,其中非注册用户只能访问几页,其他注册用户不多,其他注册用户也可以注册。这是一款游戏,所以只有这些玩'匹配'才能进入后者。此外,我们还会有一些页面,管理员(秘密页面)。这是结构:如何正确处理重定向和包含页面?
$Public_Path = '/public_html/public/';
$Private_Path = '/public_html/private/';
$Secret_Path = '/public_html/secret/';
我穿过/public_html/index.php一切,并从那里包含的文件。我认为将所有include和重定向放在一个地方会更好,而不是分散在每个文件的开头。事情是,代码开始在这里变得混乱,还有更多的选项要添加。有没有简化它的模式?这是我的代码看起来现在:
// Some extra code goes here
// If an unregistered user tries to go anywhere he shouldn't
if (!file_exists($Public_Path . $Url . '/index.php') && empty($User))
header ('Location: /login/');
// PUBLIC. Load the public pages
if (file_exists($Public_Path . $Url . '/index.php'))
include $Public_Path . $Url . '/index.php';
// SECRET. Only for admin
else if (file_exists($Secret_Path . $Url . '/index.php') && in_array($User->email, $Admins))
include $Secret_Path . $Url . '/index.php';
// PRIVATE. Load the template and include private pages
else if (file_exists($Private_Path . $Url . '/index.php'))
{
if ($UrlArray[0] != 'games' && $User->game == 0)
header ('Location: /games/');
if ($UrlArray[1] == 'save')
include $Private_Path . $Url . '/index.php';
else
{
$Page = $Private_Path . $Url . '/index.php';
include $Include_Path . 'template.php';
}
}
// 404. Nor public nor private nor secret
else
header ('Location: /error/404');
注:我只知道能与此访问index.php
的限制,我强加我自己。
我的问题是,如何以某种方式订购此代码,使我可以添加更多的功能,但仅增加一点复杂性?如何减少后者?
这只是给了我一个想法,我将为用户使用一个开关,并从数据库中部分检索一个数组,以检查用户是否可以访问该页面。但是我并没有真正理解''__SESSION ['userPath']是什么意思,你能解释一下那里会有些什么吗? –
会话变量是存储在服务器上的值,链接到用户计算机上的cookie。它们总是以'$ _SESSION'开始,'$ _POST','$ _GET','$ _FILE'是保留的系统变量。作为用户登录脚本的一部分,我通常会设置几个与用户关联的会话变量(我将变量“权威”命名为数组,并将其作为数组,并遍历分配给该用户的权限。)希望用户访问的路径显示(基于登录ID)将设置在他们的$ _SESSION变量中,在登录时检索并设置。每个页面首先检查'$ _SESSION'变量。 – Sablefoste