get path after php script
I have search high and low for an answer to my question and I cannot find it. Basically what I want to do is get the path after a php script. ex. "http://www.example.com/index.php/arg1/arg2/arg3/etc/" and get arg1, arg2, arg3, etc in an array. How can I do this in php and once I do this will "http://www.example.com/arg1/arg2/arg3/etc" still return the same results. If not then how can I achieve this?
Here is how to get the answer, and a few others you will have in the future. Make a script, eg "test.php", and just put this one line in it:
<?php phpinfo();
Then browse to it with http://www.example.com/test.php/one/two/three/etc
Look through the $_SERVER
values for the one that matches what you are after.
I see the answer you want in this case in $_SERVER["PATH_INFO"]: "/one/two/three/etc" Then use explode
to turn it into an array:
print_r( explode('/',$_SERVER['PATH_INFO'] );
However sometimes $_SERVER["REQUEST_URI"]
is going to be the one you want, especially if using URL rewriting.
UPDATE:
Responding to comment, here is what I'd do:
$args = explode('/',$_SERVER['REQUEST_URI']);
if($args[0] == 'index.php')array_shift($args);
像这样尝试:
$url ="http://www.example.com/index.php/arg1/arg2/arg3/etc/";
$array = explode("/",parse_url($url, PHP_URL_PATH));
if (in_array('index.php', $array))
{
unset($array[array_search('index.php',$array)]);
}
print_r(array_values(array_filter($array)));
你可以尝试使用parse_url
和parse_str
$url =$_SERVER['REQUEST_URI'];
$ParseUrl = parse_url($url);
print_r($ParseUrl);
$arr = parse_str($ParseUrl['query']);
print_r($arr);
链接地址: http://www.djcxy.com/p/56798.html
上一篇: bash命令获取文件的绝对路径:`%〜dpnx1`
下一篇: 在php脚本之后获取路径