user2116287
user2116287

Reputation: 77

$_SESSION variable in php (page to page)

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?

Upvotes: 0

Views: 91

Answers (4)

cernunnos
cernunnos

Reputation: 2806

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";
}

Upvotes: 1

Martin Turjak
Martin Turjak

Reputation: 21224

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 =)

Upvotes: 1

Martin
Martin

Reputation: 6687

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

Upvotes: 0

Kyle
Kyle

Reputation: 1733

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

Upvotes: 3

Related Questions