0

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;

?>
Pat Green
  • 95
  • 1
  • 8
  • 1
    Check this question: http://stackoverflow.com/questions/6768793/get-the-full-url-in-php – Raphael Rafatpanah Aug 02 '15 at 13:23
  • 1
    You don't have to use sessions, get the page file name and do the if statement – Sayed Aug 02 '15 at 13:23
  • After I have the url in a variable, how would I use the variable in an if statement to know what page the user is on? – Pat Green Aug 02 '15 at 13:45
  • How do you know which page the user is requesting? By file name like subject.php? or by page id like index.php?p=1 or something else? – Michael Eugene Yuen Aug 02 '15 at 14:40
  • 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. I posted the example below. – Jordan Davis Aug 02 '15 at 17:36

2 Answers2

1

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>
Junius L
  • 15,881
  • 6
  • 52
  • 96
0

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.

Jordan Davis
  • 1,485
  • 7
  • 21
  • 40