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;

?>
5
  • 1
    Check this question: stackoverflow.com/questions/6768793/get-the-full-url-in-php Commented Aug 2, 2015 at 13:23
  • 1
    You don't have to use sessions, get the page file name and do the if statement Commented Aug 2, 2015 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? Commented Aug 2, 2015 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? Commented Aug 2, 2015 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. Commented Aug 2, 2015 at 17:36

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.