Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php - 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?

like image 362
john01dav Avatar asked Nov 21 '13 05:11

john01dav


2 Answers

Here is how to get the answer, and a few others you will have in the future. Make a script, e.g. "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);
like image 108
Darren Cook Avatar answered Sep 22 '22 08:09

Darren Cook


Try like this :

$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)));
like image 45
Mahmood Rehman Avatar answered Sep 19 '22 08:09

Mahmood Rehman