0

Whenever I go to a page i.e. login page or any other page, I want to save the name of the page in a $_SESSION variable.

login page:

<?php
    session_start();
    $_SESSION['page'] = 'login.htm';    
?>

It works only for the login page and doesnt overwrite in other pages for e.g. home page:

<?php
    session_start();
    $_SESSION['page'] = "home.htm"; 
?>

I need the sesssion variable 'page' to hold the last page I was, can anyone help please?

0

4 Answers 4

3

Why not just use $_SERVER['HTTP_REFERER']? This will give you the previous page in PHP, without having to add anything to sessions.

Sign up to request clarification or add additional context in comments.

5 Comments

am making like an app so i need to know it for REDIRECTION purpose
Note that HTTP_REFERER will not work properly when user refreshes page
You can still use this for redirection purposes. Not sure what you mean.
sorry am new on web design, how would the $_SERVER['HTTP_REFERER'] work like in the case of my example ..
$_SERVER['HTTP_REFERER'] holds the page the user came from, so assuming the user was at "home.htm" and clicked a link to go to "login.htm", $_SERVER['HTTP_REFERER'] = "home.htm
1

when you navigate to a new page first retrive the saved "back" variable (and use it in a back link/breadcrumbs or something), and then overwrite the sessions "back" variable with the curent page, to have it ready for the next move =)

Comments

1

If all you need is default "back" functionality you should let the browser handle it.

If what you want is something to be used as a breadcrumb following some internal order (or path in a tree) my advice is to let each page "know" the path that leads to it.

If you really need to know from what page the user came from save it to a previous variable before you write over the current variable.

// Make sure user didnt just refresh the page
if ($_SESSION["current"] !== "currentPage.php") {
  $_SESSION["previous"] = $_SESSION["current"];
  $_SESSION["current"] = "currentPage.php";
}

Comments

0

You're using different keys.. 'page' and 'back'.

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.