Determine the current page name?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Johnnyjoe2000
Forum Newbie
Posts: 6
Joined: Mon May 21, 2007 9:20 am

Determine the current page name?

Post by Johnnyjoe2000 »

I have what is probably a super basic question, but it is something I have never seemed to need to do before, and must not be using the right Key words with google.

I have a very simple menu. On the home page I don't want it to display "Home" but I do on all the other pages. Like wise I want "About Us" to display on all the pages but the About page.

So in short I need to do the following:

if not current page = "index.php" display home link;
if not current page = "about.php" display about link;

etc...

Any ideas on how to do this? I came across some examples in my google research, but they all were very complicated and lengthy for what seams should be a simple one line of code per link.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You could check $_SERVER['PHP_SELF'], but everyone here who's a better programmer than me will recommend against it.

A more sure way is to create a variable in every page called, like, $title, and set it to the title you want it to be. Then, check against it. Just be sure to set the variable on every page before you check.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

__FILE__ may be of interest.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

feyd wrote:__FILE__ may be of interest.
I've always wondered what the alternative was. I wasn't aware that it was a constant, but changed for each page.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I use __FILE__ or $_SERVER['SCRIPT_FILENAME'] in conjunction with basename(). I stay away from all things $_SERVER['PHP_SELF'].
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Everah wrote:I use __FILE__ or $_SERVER['SCRIPT_FILENAME'] in conjunction with basename(). I stay away from all things $_SERVER['PHP_SELF'].
All I ever remember you guys saying was
Everah wrote:stay away from all things $_SERVER['PHP_SELF'].
Then, I frantically searched Google for an alternative, and finally decided to just assume that I had to specify it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I asked this exact same question about two and half years ago. feyd showed me the way and I haven't looked back since.

As for the PHP_SELF thing, do enough local tests on it trying to corrupt it, you'll see why most stay away from it.
Post Reply