The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

__FILE__ or PHP SELF to detect what page user is on?

This is my first time working with a very simple PHP template system. Each of my site's pages includes a template file. The template file has all of the info for the header and footer of the page.

The site is completely CSS/XHTML based and uses no tables.

I've used the "Happy Cog method" of image "swapping" to create the rollover states in my main navigation.

I want to be able to create a "You Are Here" effect by keeping the nav buttons highlighted while on their corresponding pages. (ex: you're on the links page and the links tab in the navigation menu is highlighted).

In the past, I've done this by using a simple "body id=" tag in my HTML. The body id tells the CSS what page I'm on and the CSS highlights the correct nav button.

Because my PHP template generates the header info and everything right up to the body content (including the nav menu), I can't hard code a body id into each and every page like I have in sites that I've done before.

Is there a way I could use PHP to detect what page the user is on, thus highlighting what page the user is on?

I'm guessing it's something like an if statement that might be like, "if page = links.php, then set body id=links, which would highlight the appropriate button.

Unfortunately, I've never used __FILE__ or PHP SELF, but I think one of the two might be the solution to what I'm trying to accomplish. Heck, I don't even know if I'd be able to let PHP set a body id or insert something into a body tag.

It really depends how you have setup the page
If you're using query strings, like index.php?act=something
Then you can just do an if ($_GET['act'] == 'something')
Or even better, use an ternary operator
$_GET['act'] == 'something' ? : ' class="selected"' : ''

Or you if you have a different file for every page, you could use $_SERVER['PHP_SELF']

In both case, you're better off storing all your pages inside an array and display your menu like this: