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?
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
substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
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.
basename(__FILE__)
is also a way.
__FILE__ 'magic' constant