Search code examples
phphtmlssishtml

Alternate Headers with SSI and PHP


I have two headers: one to display for a logged in user, and one to display when logged out/not a member. I also have a footer that should be duplicated on each page.I had the idea to use SSI to include the header and footer.

As of now, we haven't started much server-side processing, and thus don't keep track of logged in/logged out users. As such, for now, I just want to use the page that is including the header to determine which to display. I had the idea to use a PHP file as the header instead of an SHTML file, so I could do some processing to determine which header to show.

So is it possible to determine which page is calling the include with PHP?

Am I going about this all wrong? If so, what solution is more appropriate?

For example, each html page fits this general layout:

<html>
<header>
    <!-- relevant header calls -->
<header>
<body>
    <div id="body">
        <!--#include virtual="header.php"-->
        <!-- actual page content -->
    </div>
    <!--#include virtual="footer.shtml"-->
</body>
</html>

And in header.php I want something like:

<?php
if(/*page is a non-logged in page*/){
    echo(/*logged out header*/);
} else {
    echo(/*logged in header*/);
}
?>

Solution

  • So is it possible to determine which page is calling the include with PHP?

    No idea. But if it is possible it will be via $_SERVER. Put this in your header.php for testing:

    <?php
    echo '<pre>';
    print_r($_SERVER);
    echo '</pre>';
    

    However, if the page is being requested as *.html with Server-Side Includes I can't even begin to predict what kind of havoc this is going to play with PHP Sessions. I have doubts that session_start() will ever be able to set proper headers in this context, or if the PHP session cookie will ever be sent to the client or be passed through SSI back to PHP.

    As far as I am aware/concerned SSI should only ever be used to include static content or dynamic content that does not rely on any sort of interaction with the user, including something as basic as if they're logged in or not. SSI is a kludge between static and dynamic pages and should be referred to as "kinda-sorta-dynamic-but-not-really".

    Short answer: SSI is going to be a massive pain in the ass, ditch it and just use PHP include().

    Edit: Your page would look something like this at the most basic level, and is not really any more complex than using SSI. If you took a more MVC-oriented approach [namely the C and V parts] it would become more manageable:

    <?php
    session_start();
    // other initialization
    ?><html>
    <head>
        <!-- relevant header calls -->
    <head>
    <body>
        <div id="body">
            <?php
    if($_SESSION['is_logged_in']){
        echo(/*logged out header*/);
    } else {
        echo(/*logged in header*/);
    }
    ?>
            <!-- actual page content -->
        </div>
        <?php include("footer.php"); ?>
    </body>
    </html>