PHP:无法限制对页面的访问

时间:2014-04-13 09:37:40

标签: php mysql login

我有header.php,它包含启动会话的init.php,并且还包含数据库查询的所有函数和类。 我有header.php包含在每个文件中,因为它包含菜单。如果用户已登录,则更新菜单。这很好。

但我的问题是:当在另一个页面上包含header.php时,我无法进一步包含init.php,因为它已经包含在header.php中,因此我无法调用那些页面中的任何函数需要。

例如,有一个名为new.php的页面(也包含header.php),admin可以在其中添加新条目;我希望它只对管理员可访问。所以我想做的是检查用户的等级是否为2,否则重定向。因此,要检索用户的等级,我需要在init.php中使用类中的函数。为此,如果我将逻辑代码放在header.php(在new.php中)下面,因为只有这样我才能访问init.php的对象,我无法使用header()函数,因为标题已经发送。如果我尝试通过再次包含init.php文件在header.php(在new.php中)上面声明它,它会给我:不允许类的多个声明错误。

这是一段代码:

的header.php

<?php
      require 'core/init.php';
      if($general->logged_in()){
        $user = $user->userdata($_SESSION['uid']);
        $username = $user['username'];
        $rank = $user['rank'];
      }
?>
<!--Menu bar html below-->

new.php

<?php  
    include 'includes/header.php';
    <!--Check for the rank and redirect if invalid-->
?>

2 个答案:

答案 0 :(得分:0)

看看require_once。这可能是你问题的答案。

答案 1 :(得分:0)

我认为include_once你可以阅读它here,而this other good question你可以了解到服务器速度和性能有什么不同,哪一个更贵执行。 这也错了

<!--Check for the rank and redirect if invalid-->
php中的

注释就像这样

 /* Check for the rank and redirect if invalid */