重定向包括到索引页面

时间:2011-10-11 13:12:45

标签: php include

我在“index.php”中加载了“content.php”。当用户请求直接对“content.php”进行URL访问时,如何将include重定向到“index.php”?

3 个答案:

答案 0 :(得分:3)

您可以在index.php之前在include content.php;中设置一个常量,然后在content.php中检查是否已定义。如果没有重定向到index.php。例如:

<?php // index.php

define('IN_APP', true);

include 'content.php';

?>

<?php // content.php

if(!defined('IN_APP')){ 
    header('Location: index.php') 
}

?>

答案 1 :(得分:0)

在content.php的顶部:

if( strstr($_SERVER['REQUEST_URI'], 'index.php') != 0 ) {
    header('Location: index.php');
    exit;
}

答案 2 :(得分:0)

您可以使用basename($_SERVER["SCRIPT_FILENAME"])查找当前正在执行的脚本的名称。

即使文件名是索引文件,也会返回文件名,并使用类似path/的网址调用。 $_SERVER['REQUEST_URI']只返回/

相关问题