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

2019-07-25 03:05发布

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 ?

2条回答
戒情不戒烟
2楼-- · 2019-07-25 03:22

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-07-25 03:27

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

查看更多
登录 后发表回答