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


IF in php include file which will be closed by another include file

我需要为普通用户限制一些区域。我编写了以下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 ?>

此代码工作没有任何问题。当我将代码放在包含文件中//normal 代码上方,将代码放在包含文件中//normal 代码下方时,它将如下所示:

<?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

是否有解决方法可以避免此消息,我可以使用包含?我有很多东西需要实现这段代码,如果有新的用户角色,我将不得不在每个文件中重写代码......

我希望有一个解决方案。

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

这就是您所需要的。