PHP:为什么带有路径信息的$url起作用?
我对此有疑问:PHP:为什么带有路径信息的$url起作用?,php,Php,我对此有疑问: $url = isset($_SERVER['PATH_INFO']) ? explode('/', ltrim($_SERVER['PATH_INFO'],'/')) : '/'; if($url == '/'){ require_once __DIR__.'/Models/index_model.php'; require_once __DIR__.'/Controllers/index_controller.php'; require_once
$url = isset($_SERVER['PATH_INFO']) ? explode('/', ltrim($_SERVER['PATH_INFO'],'/')) : '/';
if($url == '/'){
require_once __DIR__.'/Models/index_model.php';
require_once __DIR__.'/Controllers/index_controller.php';
require_once __DIR__.'/Views/index_view.php';
$indexModel = New IndexModel();
$indexController = New IndexController($indexModel);
$indexView = New IndexView($indexController, $indexModel);
print $indexView->index();
} else{
在xampp服务器上,这项工作。但在web主机上,Php只做其他事情。不要加载索引 您遇到的问题是,如果
PATH\u INFO
包含/
,则在检查该字符串是否等于字符串时,您正在分解该字符串,该字符串将为您提供一个数组
您只需立即检查值:
// Get the path if it exists, or / if it doesn't
// PHP 7+ version
$path = $_SERVER['PATH_INFO'] ?? '/';
// PHP 5.x version (if you need this, you should really update your PHP version asap)
$path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/';
// Let's just compare the value straight off
if ($path == '/') {
// Start page
} else {
// Something else
// Explode the path here if you need to
}
你检查了服务器上的
$\u服务器['PATH\u INFO']
实际包含的内容了吗?@MagnusEriksson我尝试了这个“echo$\u服务器['PATH\u INFO'];”但是它没有显示任何内容。顺便说一句,如果路径信息是//code>,变量$url
将是一个数组,其中有一个空字符串作为值,因为你正在分解它,所以它不是一个字符串。@MagnusEriksson你能给我一个小例子吗?当我这样做时,@MagnusEriksson:“if($\u SERVER['REQUEST\u URI']=='/'){”工作:应该注意,?
是PHP7+独占三元。