PDA

View Full Version : php url stuff


jonathaw
01-23-2005, 10:03 AM
Word up, PHP phreaks. I was wanting to use the basename (http://us2.php.net/manual/en/function.basename.php) function for something that I would put in a template. Each page will then use the same script to do something different, depending on the filename. Problem is I can't figure out how to read the URL into PHP as a variable. I'm sure this must be possible...?

BTW, I have other solutions lined up, I just want to know how to do it this way.

fturtle
01-23-2005, 10:21 AM
There's a variable php uses ($_SERVER) to store a bunch of useful basic client/server information per script, which you can use.

It's an associative array, try a simple script with print_r($_SERVER) to see what it's got in it.

The one you're lookin' for could be REQUEST_URI, or SCRIPT_NAME, accessed as
$_SERVER["REQUEST_URI"] and so on, depending on your purpose.

edit:
-----
If you want a nice explanation of each array element (rather than just what you can see in print_r()), check here (http://us3.php.net/reserved.variables).

jonathaw
01-23-2005, 04:39 PM
Awesome. That set me on the right track. To figure it out, I tried to get "test" out of http://test.jonathanadairwilson.com/test.php. $_SERVER[SCRIPT_NAME] returns "/test.php" so here's the code to do that.

$file = substr($_SERVER[SCRIPT_NAME], 1);
$file = explode(".", $file);
echo $file[0];

I'm going to use this in a navigation bar script in a template that highlights/bolds/whatever the current link you're on. If that makes sense.