PHP - 获取php脚本后的路径

joh*_*dav 5 html php apache path

我到处寻找我的问题的答案,但找不到。基本上我想做的是获取 php 脚本后的路径。前任。“ http://www.example.com/index.php/arg1/arg2/arg3/etc/ ” 并获取数组中的 arg1、arg2、arg3 等。我如何在 php 中执行此操作,一旦执行此操作,“ http://www.example.com/arg1/arg2/arg3/etc ”仍会返回相同的结果。如果不是那么我怎样才能实现这一目标?

Dar*_*ook 5

以下是如何获得答案,以及您将来还会得到的其他一些答案。制作一个脚本,例如“test.php”,然后将这一行放入其中:

<?php phpinfo();
Run Code Online (Sandbox Code Playgroud)

然后使用http://www.example.com/test.php/one/two/third/etc浏览到它

查看$_SERVER与您想要的值相匹配的值。

我在 $_SERVER["PATH_INFO"]: "/one/two/third/etc" 中看到了您想要的答案,然后使用explode将其转换为数组:

print_r( explode('/',$_SERVER['PATH_INFO'] );
Run Code Online (Sandbox Code Playgroud)

然而,有时$_SERVER["REQUEST_URI"]这将是您想要的,特别是在使用 URL 重写时。

更新:

回复评论,这就是我要做的:

$args = explode('/',$_SERVER['REQUEST_URI']);
if($args[0] == 'index.php')array_shift($args);
Run Code Online (Sandbox Code Playgroud)