Ricky55 Posted May 11, 2011 Share Posted May 11, 2011 Hi Still very new to PHP but getting some good use out of it with includes. I'm currently using this code to check the file name of the page and then give the corresponding anchor tag a class of active in order to style my main menu. This is all working for most of my pages but my problem is that I now have multiple files called index.php one in the root of my site and some are in folders. Is there any way around this issue? Can PHP check to see if its the index file in the root of the site or is there a better way. I suppose I could create a second variable in the index file in my root ie my home page and then check to say if the current page is called index and has the variable of home but how would I write this? My code $currentPage = basename($_SERVER['SCRIPT_NAME'], '.php'); <li><a href="/" <?php if ($currentPage == 'index') {echo 'class="active"';} ?>>Home</a></li> Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/ Share on other sites More sharing options...
PHPete Posted May 11, 2011 Share Posted May 11, 2011 What I'd do is give each page a variable, let's say in each page you have something like <?php $pageID = '1'; ?> But change the number for each page. Then, for your navigation, you could have <ul> <li><a href="#" <?php if ($pageID == '1') {echo 'class="active"';} ?>>Home</a></li> <li><a href="#" <?php if ($pageID == '2') {echo 'class="active"';} ?>>Link 2</a></li> </ul> Etc... As far as I can see, this could work. I'm not sure if there are any problems with this method, or if it'll cause any problems within your site. I hoe it works alright though. Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213766 Share on other sites More sharing options...
cyberRobot Posted May 11, 2011 Share Posted May 11, 2011 You could use $_SERVER['PHP_SELF']: http://www.php.net/manual/en/reserved.variables.server.php It gives you the folders and filename. Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213767 Share on other sites More sharing options...
PHPete Posted May 11, 2011 Share Posted May 11, 2011 You could use $_SERVER['PHP_SELF']: http://www.php.net/manual/en/reserved.variables.server.php It gives you the folders and filename. From what I've read, this can be very insecure. Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213768 Share on other sites More sharing options...
cyberRobot Posted May 11, 2011 Share Posted May 11, 2011 From what I've read, this can be very insecure. It can be when used with things like forms. Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213769 Share on other sites More sharing options...
PHPete Posted May 11, 2011 Share Posted May 11, 2011 I'm hoping this isn't too far off topic, but this article explains the security issues within PHP_Self. Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213771 Share on other sites More sharing options...
Ricky55 Posted May 11, 2011 Author Share Posted May 11, 2011 Cheers lads, just used the pageID, as I had a few I've created an array to hold these. Working a treat. I know what I need to do in plain in english I just can't always turn that into PHP without some prompting. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213775 Share on other sites More sharing options...
cyberRobot Posted May 11, 2011 Share Posted May 11, 2011 I'm hoping this isn't too far off topic, but this article explains the security issues within PHP_Self. Yep, that whole article talks about the dangers of using $_SERVER['PHP_SELF'] in the action attribute of a form tag. It doesn't say anything about using PHP_SELF elsewhere. Using it to get the folder and filename for testing purposes should be just fine. Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213777 Share on other sites More sharing options...
PHPete Posted May 11, 2011 Share Posted May 11, 2011 Yep, that whole article talks about the dangers of using $_SERVER['PHP_SELF'] in the action attribute of a form tag. It doesn't say anything about using PHP_SELF elsewhere. Using it to get the folder and filename for testing purposes should be just fine. Oh right. I've never really used PHP_SELF, jsut read a little bit about it. Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213779 Share on other sites More sharing options...
cyberRobot Posted May 11, 2011 Share Posted May 11, 2011 Cheers lads, just used the pageID, as I had a few I've created an array to hold these. Working a treat. I know what I need to do in plain in english I just can't always turn that into PHP without some prompting. Cheers You could modify your original code using $_SERVER['PHP_SELF']: <li><a href="/" <?php if ($_SERVER['PHP_SELF'] == '/index.php') { echo 'class="active"'; } ?>>Home</a></li> Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213784 Share on other sites More sharing options...
PHPete Posted May 11, 2011 Share Posted May 11, 2011 You could modify your original code using $_SERVER['PHP_SELF']: <li><a href="/" <?php if ($_SERVER['PHP_SELF'] == '/index.php') { echo 'class="active"'; } ?>>Home</a></li> Would that not cause problems in sub directories? say the URL was website.com/directory/index.php The link to website.com/index.php would be active. Correct me if I'm wrong. Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213788 Share on other sites More sharing options...
cyberRobot Posted May 11, 2011 Share Posted May 11, 2011 Would that not cause problems in sub directories? say the URL was website.com/directory/index.php The link to website.com/index.php would be active. Correct me if I'm wrong. Nope, $_SERVER['PHP_SELF'] would contain a different value. So your test would be: <li><a href="/" <?php if ($_SERVER['PHP_SELF'] == '/index.php') { echo 'class="active"'; } ?>>Home</a></li> <li><a href="/directory/" <?php if ($_SERVER['PHP_SELF'] == '/directory/index.php') { echo 'class="active"'; } ?>>Directory</a></li> Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213791 Share on other sites More sharing options...
PHPete Posted May 11, 2011 Share Posted May 11, 2011 Nope, $_SERVER['PHP_SELF'] would contain a different value. So your test would be: <li><a href="/" <?php if ($_SERVER['PHP_SELF'] == '/index.php') { echo 'class="active"'; } ?>>Home</a></li> <li><a href="/directory/" <?php if ($_SERVER['PHP_SELF'] == '/directory/index.php') { echo 'class="active"'; } ?>>Directory</a></li> Ah right. Cool. That seems a lot easier than the $pageID method I used. Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213792 Share on other sites More sharing options...
cyberRobot Posted May 11, 2011 Share Posted May 11, 2011 Ah right. Cool. That seems a lot easier than the $pageID method I used. Yep, I've used the $pageID method in the past. It worked well in the beginning, but as my websites evolved it became more of a maintenance issue. Quote Link to comment https://forums.phpfreaks.com/topic/236095-issue-with-giving-anchor-tags-a-class-based-on-its-file-name/#findComment-1213798 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.