检测并显示当前页面作为标记链接

until today I've been using following function

function menu_active($pagename)
{
    $active = basename($_SERVER['PHP_SELF'], ".php");
    if ($active === $pagename) echo "id='active' ";
}


And in HTML
<?php menu_active('page_name'); ?>

to detect which page is active to mark it as current page, but when I changed my page display method it doesn't work anymore. So I wonder how to change the function to make it work. I've tried to declare $pagename as filename.inc.php in the script below, but no use.

I'm using this script to display pages

$pages_dir = 'pages';
if (!empty($_GET['p'])) 
{
    $pages = scandir($pages_dir, 0);
    unset($pages[0], $pages[1]);

    $p = $_GET['p'];

    if (in_array($p . '.inc.php', $pages)) 
    {
        include ($pages_dir . '/' . $p . '.inc.php');
    } 
    else 
    {
        echo 'Error';
    }
} 
else 
{
    include ($pages_dir . '/home.inc.php');
}

I appreciate any help, feedback or logic and ideas of how to do this right.

Instead of checking which script is running, check $_SERVER['REQUEST_URI']. This will give you the relative URL as requested by the client.