PHP variables and 'back' button

Joined
Feb 9, 2022
Messages
3
Reaction score
0
Hello,
I'm very new to PHP and have begun using SESSION variables.
Everything seems to work with setting session_start() and passing SESSION values in a forward direction. What I need to understand is what happens when the browser 'back' button is pressed. My data disappears when this happens. Do those pages no longer belong to the same SESSION? Why is the data now missing? I'm sure it is something fundamental I've not done, but my research has come up empty thus far.

I'm working on an e-commerce checkout application and would like to either preserve the data with the back button press or redirect the customer to begin again. The data entry is extremely simple so starting over should not be an issue.
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
What do you mean by "passing SESSION values in a forward direction"? Once declared your session variables should always be there. Do you or did you start the PHP code with 'session_start()'?
 
Joined
Feb 9, 2022
Messages
3
Reaction score
0
True. That was a poor choice of words. The SESSION variables are set at various points as I have session_start() with each successive php page. Those are available as I move forward in the checkout process. What I am confused about is if the 'back' button of the browser is clicked, then the display on the reloaded page pointed to by the back button should output unchanged SESSION variables. The variables.in that page are not retaining their values or not showing that they do. They come up blank.
My impression was that those values would not change unless I specifically changed them as long as the SESSION was active.

Maybe I do not understand SESSION variables completely?
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
Your understanding is the same as mine, although I have never checked them like you just did. If I have some free time I'll give them a check.

If anybody wants to jump in here it's wide open.
 
Joined
Feb 9, 2022
Messages
3
Reaction score
0
Your understanding is the same as mine, although I have never checked them like you just did. If I have some free time I'll give them a check.

If anybody wants to jump in here it's wide open.
My goal was to get something like the Amazon.com checkout system where one can back up several pages and the order data is still there. Maybe it cannot be done solely with PHP and they use a database to store information instead. I'm still too new to know all the ins and outs.
 
Joined
Mar 11, 2022
Messages
227
Reaction score
32
SESSION and COOKIE are header related. Since PHP isn't compiled but parsed your session_start must be on the very first line of your php file.
Ver first line do not mean as first at all
Code:
session_start();
/*MY OTHER CODE HERE*/
will work.
Code:
echo 'I am here'; session_start();
/*MY OTHER CODE HERE*/
will work (with a warning)

Code:
echo 'I am here';
session_start();
/*MY OTHER CODE HERE*/
wont't work and will cause a headerwarning or headererror (depends on your php Version)

But what will work for sure is

sess.php
Code:
session_start();

On all your php files
Code:
require('sess.php');
/*MY OTHER CODE HERE*/

!!But Notice!!

Sessions die after a while. Unlike cookies sessions can't have an expiredate. So it's your server/php settings how long your sessions are alive.
E.g. on my server my sessions die after 5 minutes. If i have to work with session, i keep em alive by ajax and recalling a session php file every 2 minutes.
sessions are also refresshed by reload (if they are still alive)

So in your case it could be a bunch of problems.

Session timout, session_destroy somewhere in your codes, and many many other things.

SESSION handling can be tricky, as session won't change during parsing. So changing a value on runtime do not mean, it's parsed from there with this new value.

Browser-back-button actually shouldn't harm your session unless the page your users return to is also startet by a session_start(). (on the same domain of course). If they go back to a previous domain, your session still should be available when they come back to your domain with the expire date.

I would go with a error_reporting(E_ALL) to see if there is any header warnings to find the issues.

Encoding can also cause troubles. filecncoding should be the same for all php files (through the whole pipe)
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top