我已经包括"content.php"加载在"index.php"。我如何重定向包括加载到"index.php"当用户请求直接URL访问"content.php"?
您可以在include content.php;
之前设置index.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')
}
?>
在你的content.php的顶部:
if( strstr($_SERVER['REQUEST_URI'], 'index.php') != 0 ) {
header('Location: index.php');
exit;
}
您可以使用basename($_SERVER["SCRIPT_FILENAME"])
来查找当前正在执行的脚本的名称。
它将返回文件名,即使它是索引文件,并使用像path/
这样的url调用。$_SERVER['REQUEST_URI']
只返回/
。