Getting URL Address in PHP variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mparker1113
Forum Commoner
Posts: 28
Joined: Wed Apr 05, 2006 9:39 am

Getting URL Address in PHP variable

Post by mparker1113 »

I know that this is likely easy, but I need to know so that I can figure out where the user is in my developers code. I need to read which directory they are in, and what page they are opening. ex (home.html/directory2/page%2055.html .

input is very appreciated.

thank you.

mike
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

One way to do this is to use:

Code: Select all

$_SERVER[PHP_SELF];
This should return the information you require in the form of a full URL string. If you want to break it down to just top directory and filename, use explode() with "/" as the separator and reverse the array of the exploded parts with array_reverse(); then, use the first two elements of the array to create the info you need.

For http://www.mydomain.com/path/to/file.php, something like:

Code: Select all

$location = $_SERVER[PHP_SELF];
$split = explode("/","$location");
$array = array_reverse($split);
$filename = $array[0];
$directory = $array[1];
Will give you "to/file.php".
To output:

Code: Select all

;
echo $directory."/".$filename;
That's off the top of my head and untested, but is the way I would try to do it... let me know how you go!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

$_SERVER['SCRIPT_NAME'];
(#10850)
mparker1113
Forum Commoner
Posts: 28
Joined: Wed Apr 05, 2006 9:39 am

Post by mparker1113 »

Thank you, Men.

Some of this i had figured out, but you gave me better info to work with.

Thanks!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

take a look here for the full URL:

viewtopic.php?t=37950&highlight=phpself
Post Reply