Dick
Dick

Reputation: 21

Session variable content and the back button

I'm on page 1 of a series of pages and save some data to Session. I now click a link on page 1 and go to page 2. I again save some data to the same Session variable. I do this several more times saving data to the same Session variable. I now click the back button several times returning to a previous page in the series. What data will exist in the Session variable? The data saved from the last page in the series or data from the page that I now sit on from using the back button.

Upvotes: 0

Views: 7180

Answers (4)

JJ.
JJ.

Reputation: 5475

All data saved to the session stays in the session variable until you either remove it or the session expires. Back/forward/etc in the session doesn't affect the contents of the session variables - unless of course by loading a page you are changing the contents of those variables.

Upvotes: 2

David
David

Reputation: 218960

What data will exist in the Session variable?

Whatever data you put there until either you overwrite it (or remove it) or the session expires. Regardless of what the user does with their browser or how they move from one page to another, the session isn't controlled by the user/browser. It's controlled by your server-side code.

If the user performs a request for Page 4 after having performed a request for Page 5 then what happens to the session values is entirely up to you. If your code on Page 4 assumes that the user came from Page 3 and updates the session values to indicate being on Page 4, then the session values are updated. If your code checks for a later state in the session values and retains that state, then the session values are not updated.

It's entirely up to you how you want this to work.

Upvotes: 0

Benjamin Crouzier
Benjamin Crouzier

Reputation: 41925

Give it a try: (assuming you use php)

page1.php :

<?php
session_start();
$_SESSION['page1'] = 'from page1';

var_dump($_SESSION['page1']);
var_dump($_SESSION['page2']);
?>
<br /><a href="page2.php">page 2</a>

page2.php :

<?php
session_start();
$_SESSION['page2'] = 'from page2';

var_dump($_SESSION['page1']);
var_dump($_SESSION['page2']);

Go to page1, click link to page2, click back. You will see that $_SESSION['page2'] has the value still has the value you setted.

Upvotes: 1

bat
bat

Reputation: 314

I assume you're talking about PHP - if so, then data saved in $_SESSION does not change upon pressing going to different page, since session data is kept on the server. In short: you will always have last saved data in session, regardless of navigation.

Upvotes: 0

Related Questions