/    Sign up×
Community /Pin to ProfileBookmark

How to close session when user leaves page

I am trying to work my way around this. I have this forms page, and it has 3 pages. I store data entered in forms in $_SESSION. I have session_start() at the beginning of the page, and session_destroy() at the last page.

But say the user hits HOME page or goes to another link in the middle of filling out the form… like on page 2. And then goes back… the session data will still be there from last time. But when I do a session_destroy() at the end of each page, it erases the session data so i can’t continue.

Is there a check to do to see if user exists the page in the middle so I can end the session? How should I do this?

-Pizzaman

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@BigMoosieApr 30.2005 — Hmmm, I thought that sessions do that already, but Im probably wrong cos Im a beginner with PHP.

One way might be to put <body onunload="javascript:whatever()"> and that might be able to call php somehow but meh, sorry cant help you mcuh.
Copy linkTweet thisAlerts:
@NogDogApr 30.2005 — Hmmm, I thought that sessions do that already, but Im probably wrong cos Im a beginner with PHP.

One way might be to put <body onunload="javascript:whatever()"> and that might be able to call php somehow but meh, sorry cant help you mcuh.[/QUOTE]

I don't think that would be practical for a number of reasons. It wouldn't know the difference between an onunload to your next page or to an external page. Also, redirecting to your site when the user is trying to go somewhere else is not exactly user-friendly.

All I can think of doing would be to check the $_SESSION['HTTP_REFERER'] and see if it's coming from within your site or not. Alternatively, you can ask yourself whether it actually matters if that session data still exists. Does it really hurt anything? If they return to the earlier part of the form sequence, will they just overwrite the old values with "no harm, no foul"?
Copy linkTweet thisAlerts:
@pizzamanauthorApr 30.2005 — True. But I guess the reason I ask is cuz I kept track of the page number in the $_SESSION as well.. so if the user exits on page 2, and comes back, it'll still be page 2 and all the variables gets messed up.

I tried ways to just use normal variables.. but it just gets reset all the time when I click continue on the forms cuz its the same page, running the same code. I tried everything... and found putting page info in $_SESSION to work. But if you can give me help on just using normal variables, i'd be happy to see it.

-Pizzaman
Copy linkTweet thisAlerts:
@SpectreReturnsApr 30.2005 — I wrote a system a while back which sends data between forms, not using sessions. You could make the first form send the data, as well as send a _POST variable telling it what page you're on. Then on the next one, you can either read the form data, or continue to send it to the next one. As long as your pages are all a series of forms, it'll work.
Copy linkTweet thisAlerts:
@pizzamanauthorMay 01.2005 — I wrote a system a while back which sends data between forms, not using sessions. You could make the first form send the data, as well as send a _POST variable telling it what page you're on. Then on the next one, you can either read the form data, or continue to send it to the next one. As long as your pages are all a series of forms, it'll work.[/QUOTE]

That did the trick! Thanks

-Pizzaman
×

Success!

Help @pizzaman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...