PHP Menu: Get Current Page
Posted by NitoApr 10
This is a quick and easy way to determine which page you’re currently on, and then setting your menu button’s overlay based on that page.
First we need to create a function to determine the page we’re on.
function getCurrPage() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
Next we need to check our this against the menu.
$thisPage = getCurrPage();
if($thisPage == 'index.php') { $navHome = '[Current Class]'; } else { $navHome = '[Noncurrent Class]'; }
if($thisPage == 'search.php') { $navSearch = '[Current Class]'; } else { $navSearch = '[Noncurrent Class]'; }
if($thisPage == 'about.php') { $navAbout = '[Current Class]'; } else { $navAbout = '[Noncurrent Class]'; }
if($thisPage == 'faq.php') { $navFAQ = '[Current Class]'; } else { $navFAQ = '[Noncurrent Class]'; }
if($thisPage == 'contact.php') { $navContact = '[Current Class]'; } else { $navContact = '[Noncurrent Class]'; }
After everything’s been checked, you can simply set your menu as-needed.
echo $navHome . $navSearch . $navAbout . $navFAQ. $navContact;
If you’re dealing with a dozen or more menu links, it would be more efficient to create an array to scan and verify the current page, but this will work fine for smaller menus. Enjoy.




2 comments
Pingback by Tweets that mention PHP Menu: Get Current Page :Nito's Nook -- Topsy.com on April 28, 2010 at 5:48 am
[...] This post was mentioned on Twitter by Nito Belmont. Nito Belmont said: PHP Menu: Get Current Page http://bit.ly/cqCPS6 (via @NitoBelmont) [...]
Comment by Candie on April 20, 2011 at 8:09 pm
Thanks alot – your answer solved all my problems after several days sutrgginlg