Reputation: 46479
I need to detect page to do some styling on a website, right now I'm using
$currentpage = $_SERVER['REQUEST_URI'];
but this returns something like /development/en/contact.php
I would like something that will only return contact.php
is there a method to achieve this?
Upvotes: 2
Views: 5468
Reputation: 32701
basename($_SERVER['SCRIPT_NAME']);
It will return only the file and strips the folders. For more information see basename()
Note the usage of SCRIPT_NAME
instead of REQUEST_URI
as REQUEST_URI
may contain additional "crap":
http://example.com/dev/en/contact.php/this/is/crap
REQUEST_URI: /dev/en/contact.php/this/is/crap
SCRIPT_NAME: /dev/en/contact.php
Upvotes: 9
Reputation: 1662
This should get you what you want:
basename($_SERVER["SCRIPT_FILENAME"]);
Though be careful, as it will give you the same answer for files with the same name in different locations.
Upvotes: 3
Reputation:
substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
Upvotes: 1