问题
I have a page tab app that is a contest with a like gate.
A user likes a page, provides their email address and hits submit to enter the contest.
After they enter, I set a session variable $_SESSION['entered'] = '1'; or something similar
Once they enter, I show more information on the page(html) and remove the email form.
PROBLEM: If the users stays on the page, refreshes it a million times (in the same browser window), the session variable stays and I'm able to tell that the user has entered and display the correct info.
If the user closes the window, comes back later in a new window, etc. I lose the session information and the user is presented with the enter form again (although they've already entered the contest)
How do I sustain the session for the user in the page app if they leave and come back?
回答1:
You could use a cookie instead. Sessions will close out on exiting the browser, but cookies will last until the expiration date is reached on them.
You could also set it up that they have to log in, and in their user, you set whether they have entered the contest or not and adjust the page accordingly. (just an idea...)
回答2:
Sessions are destroyed by the browser when closed - nothing code wise will change how browsers handle that. You can try a cookie instead, or try to save the session to their IP/useragent (wouldn't recommend for security though) and reestablish it when they come back.
回答3:
As written by others Sessions are destroyed if a user closes the browser. i won`t use cookies on your side either. I suggest you to get the users uid and check your database if the user has already entered the form and display the correct page after you have this info.
This might be a more secure and consistent way than sessions or cookies.
Greets
来源:https://stackoverflow.com/questions/19012633/facebook-php-page-tab-app-session-does-not-sustain-when-a-user-closes-their