-->

iPad/iPhone : how to keep the session in a web app

2019-07-25 02:30发布

问题:

It's straight forward to add a web page as a web application. Find here a nice article.

The issue know is keeping a session open. Once switching to another app the session is closed and the user needs to re-enter his credentials.

I've found a similar entry without answer in stack-overflow.

Some hints ?

回答1:

Instead of storing the login info in a $_SESSION variable, store it in a $_COOKIE. The cookie will be saved depending on when you set it to expire. As long as they log in "inside" the web app, or the regular web version (and the cookie is the same) they will not have to log in every time or when switching between the two.



回答2:

The trick is to do this:

// Start or resume session
session_start(); 

// Extend cookie life time by an amount of your liking
$cookieLifetime = 365 * 24 * 60 * 60; // A year in seconds
setcookie(session_name(),session_id(),time()+$cookieLifetime);

I tested this to work on iOS 4.2.1, 5.1.1, 6.0 and 6.1. The session is even restored after turning off and restarting the device.

For a more elaborate discussion of this strategy you can take a look at my answer of this question: Maintain PHP Session in web app on iPhone