r/PHPhelp • u/Ill-Year-3141 • Sep 02 '25
Need help with navbar logic
I have a navigation menu in my side bar. As it is, when a page is loaded, the page you're currently on gets highlighted on the nav menu. It works for every page except 2 of them.
I have an index.php in root, and an admin/index.php.
Whenever I am on either of those pages, both the home link and the admin link are highlighted (As if I am on 2 pages at once).
I do not understand what is causing this, but here's the code, hopefully someone can point me in the right direction:
<?php// Sidebar / Navbar includeif (!function_exists('is_active')) {function is_active(string $path): string {$req = $_SERVER['SCRIPT_NAME'] ?? '';if (function_exists('str_ends_with')) {return str_ends_with($req, $path) ? 'active' : '';}return (substr_compare($req, $path, -strlen($path)) === 0) ? 'active' : '';}}?><?php if (!isset($NAV_ACTIVE)) { $NAV_ACTIVE = null; } ?><!-- Sidebar --><aside class="sidebar" role="complementary">
<nav aria-label="Primary"><ul><li><a href="<?= BASE_URL ?>/index.php" class="<?= is_active('/index.php') ?>"><span class="icon">🏠</span> Home</a></li><li><a href="<?= BASE_URL ?>/pages/podcast.php" class="<?= is_active('/pages/podcast.php') ?>"><span class="icon">🎧</span> Podcast Episodes</a></li><li><a href="<?= BASE_URL ?>/pages/submit.php" class="<?= is_active('/pages/submit.php') ?>"><span class="icon">📝</span> Submit a Question</a></li><li><a href="<?= BASE_URL ?>/pages/trivia.php" class="<?= is_active('/pages/trivia.php') ?>"><span class="icon">🎯</span> The Trivia Game!</a></li>
<li style="margin-top:.75rem; opacity:.8; pointer-events:none;">— Account —</li><?php if (empty($_SESSION['user_id'])): ?><li><a href="<?= BASE_URL ?>/auth/login.php" class="<?= is_active('/auth/login.php') ?>"><span class="icon">👤</span> Account</a></li><li><a href="<?= BASE_URL ?>/auth/register.php" class="<?= is_active('/auth/register.php') ?>"><span class="icon">➕</span> Create account</a></li><?php else: ?><li><a href="<?= BASE_URL ?>/auth/account.php" class="<?= is_active('/auth/account.php') ?>"><span class="icon">👤</span> Account</a></li><?php if (!empty($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin'): ?><li><a href="<?= BASE_URL ?>/admin/index.php" class="<?= is_active('/admin/index.php') ?>"><span class="icon">🛠️</span> Admin</a></li><?php endif; ?><li><a href="<?= BASE_URL ?>/auth/logout.php"><span class="icon">🚪</span> Log out</a></li><?php endif; ?></ul>
</nav></aside>
As you can see, the is_active(' shows the page name, and if that's the page you're on, that menu link is highlighted.
For home, it is index.php. For admin, it is admin/index.php.
Are those not different? I cannot figure out why, if they're named different, each one would highlight both Home and Admin. :/ Been working on this fun little problem half the night.
4
u/colshrapnel Sep 02 '25
It's the way your function works. Basically it looks whether $_SERVER['SCRIPT_NAME'] ends with $path. And naturally, /admin/index.php ends with /index.php.
I would rather make the function this way
1
u/MateusAzevedo Sep 02 '25
In a simple test I did, both
$_SERVER['REQUEST_URI']and$_SERVER['SCRIPT_NAME']contains/admin/index.phpand/index.phprespectively. Given the paths you pass tois_active()are absolute, the function can simple check values are equal:1
u/Timely-Tale4769 Sep 02 '25
Your checking end string is only right. I think you must check the full length.