...

View Full Version : Resolved How To Show Name of Page Being Viewed?



Phred
11-23-2011, 06:03 PM
I did a search but didn't find anything here. Is there a single command to show the name of the page you are viewing?

For example, if you have a page named 'menu.php' is there a command that will show ONLY the page name without showing the directory it's in?

I tried both of these:

$_SERVER['SCRIPT_NAME']

$_SERVER['PHP_SELF']

but they also show the name of the folder the page is in, like this: /demo/menu.php

I want to only see: menu.php

I have a code that fixes this, but I want to know if there is a built-in command that does the same thing without having to use a small script?

kbluhm
11-23-2011, 06:11 PM
http://php.net/basename

Here are some alternatives, as I have found that PHP_SELF or SCRIPT_NAME are not always available.

This code would go in the file being loaded:


$file_name = basename( __FILE__ );

Or using $_SERVER['REQUEST_URI'] if you'd like to call for the name anywhere (within an included file, etc):


list( $file_uri ) = explode( '?', $_SERVER['REQUEST_URI'] );
$file_name = basename( $file_uri );

Phred
11-23-2011, 07:33 PM
Bingo! That's perfect!

I wrote this: echo basename(__FILE__);

+1! :thumbsup: :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum