我在“index.php”中加载了“content.php”。当用户请求直接对“content.php”进行URL访问时,如何将include重定向到“index.php”?
答案 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']
只返回/
。