john01dav
john01dav

Reputation: 1962

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?

Upvotes: 5

Views: 2811

Answers (3)

Darren Cook
Darren Cook

Reputation: 28928

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);

Upvotes: 5

Mahmood Rehman
Mahmood Rehman

Reputation: 4331

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)));

Upvotes: 1

Krish R
Krish R

Reputation: 22711

Can you try using parse_url and parse_str

$url =$_SERVER['REQUEST_URI'];

 $ParseUrl = parse_url($url);
 print_r($ParseUrl);

 $arr =  parse_str($ParseUrl['query']);

 print_r($arr);

Upvotes: 0

Related Questions