如果在php包含文件将被另一个包含文件关闭

时间:2014-12-30 14:50:49

标签: php

我需要为普通用户限制一些区域。我写了以下php代码:

<?php if (logged_in() == false): ?>
<?php       redirect_to("../members/login.php?msg=You+have+to+login+first"); ?>
<?php else: ?>
<?php if(checkrole('superadmin') || checkrole('admin')): ?>
//Normal code like HTML mixed with PHP and so on..
<?php else: ?>
<?php include("../error/403.php");   ?>
<?php endif ?>
<?php endif ?>

此代码没有任何问题。 当我将代码放在一个包含文件中的//普通代码上面,并且在包含文件中的//普通代码下面的代码时,它将如下所示:

<?php include("../includes/restrict_begin.php"); ?>
//Normal code like HTML mixed with PHP and so on..
<?php include("../includes/restrict_end.php"); ?>

我当然在restrict_begin.php文件中收到错误:

Parse error: syntax error, unexpected end of file in ..\includes\restrict_begin.php on line 4

是否有解决此消息的工作,我可以使用包含?我有很多需要实现此代码的东西,如果有新用户名,我将不得不重写每个文件中的代码......

我希望有一个解决方案。

1 个答案:

答案 0 :(得分:1)

restrict_begin.php

中加入这样的内容
<?php
if (logged_in() == false) {
    redirect_to("../members/login.php?msg=You+have+to+login+first");
    exit();
}

if (! (checkrole('superadmin') || checkrole('admin'))) {
    include("../error/403.php");
    exit();
}

删除restrict_end.php

这就是你所需要的一切。