Howdy_McGee
Howdy_McGee

Reputation: 10635

PHP - Getting Current URL

This seems like a common questions but none of them seem to be what i'm looking for. If there is a website:

www.example.com/test.php

I want to pull the test.php and if it's something like:

www.example.com/folder/folder1/test.php

I want again to just pull test.php but everything seems to pull the exact path instead of just the page. Any suggestions?

Upvotes: 38

Views: 83422

Answers (3)

Tadeck
Tadeck

Reputation: 137310

This will get you the name of the file that was requested (based on the URL invoked, not real location of the file - just in case you are using mod_rewrite):

$filename = basename($_SERVER['REQUEST_URI']);

For both http://www.example.com/testing01.php and http://www.example.com/x/y/z/testing01.php will give you:

testing01.php

Upvotes: 41

Matt Healy
Matt Healy

Reputation: 18531

$file = $_SERVER["SCRIPT_NAME"];
$break = explode('/', $file);
$pfile = $break[count($break) - 1]; 

Upvotes: 3

Nahydrin
Nahydrin

Reputation: 13507

$query = $_SERVER['PHP_SELF'];
$path = pathinfo( $query );
$what_you_want = $path['basename'];

Voila.

Upvotes: 51

Related Questions