Pat Green
Pat Green

Reputation: 110

PHP displaying content based on webpage

I have a website which includes a footer on every page. I want the text in the footer to change depending on what page they are on.

The only way I have come up with would be to create a session to see what page they are on, use the session in a comparative if statement and then destroy it upon exit.

My question: Is there an easier way to change my footer text content, based upon the current page the user is on?

    if(isset($_SESSION['at_index']))
       { 
          $login = "<p>Already registered? <a href='login.php'>Sign in</a></p>";
       }

    if(isset($_SESSION['at_login']))
       {
          $login = "<p>Forgotten your password? <a href='reset_password.php'>Reset</a> your password!</p>";
       }


$footer = <<<FOOTER
    <div id='footer'>
          $login
    </div>
FOOTER;

?>

Upvotes: 0

Views: 57

Answers (2)

Jordan Davis
Jordan Davis

Reputation: 1520

Create two separate pages with the different footers, then check if the user is signed-in or not? if so then redirect the user to which ever page you want.

if(isset($_SESSION['at_index'])){ 
    <?php header("Location: http://www.yoursite.com/loggedin.php"); ?>
}
if(isset($_SESSION['at_login'])){
    <?php header("Location: http://www.yoursite.com/login.php"); ?>
}

If you don't wish to have two pages... then you really should use ajax to make an asynchronous request to update the page.

Upvotes: 0

Junius L
Junius L

Reputation: 16132

you don't need to use $_SESSION just get the name of the current page and display the footer you want to display.

$current_page = basename($_SERVER['SCRIPT_FILENAME']);
if($current_page === 'home.php'{
 $footer = 'home contents';
}else{
 $footer = 'some page contents';
}

then in your footer echo the $footer.

<div id="footer">
  <?php echo $footer; ?>
</div>

Upvotes: 1

Related Questions