简体   繁体   English

移动浏览器的Javascript / PHP页面超时

[英]Javascript/PHP Page Timeout For Mobile Browsers

I'm working on a mobile based web application that's mostly AJAX and Javascript powered. 我正在研究一个主要基于AJAX和Javascript的基于移动的Web应用程序。 To access the main page a login page sends the data via post to the main page where it is checked with a mySQL query. 要访问主页,登录页面会通过发布将数据发送到主页,并使用mySQL查询对数据进行检查。 If there's a problem with the login it will return you to the login page, else it will continue to the main page. 如果登录有问题,它将使您返回登录页面,否则将继续进入主页。 I'm trying to find the best way to implement a time out to force a user to re-authenticate past a threshold. 我正在尝试找到实现超时以迫使用户重新验证超过阈值的最佳方法。

Everything is controlled from the main page via jQuery and AJAX so they don't leave main.html at any point. 一切都通过jQuery和AJAX从主页进行控制,因此它们在任何时候都不会离开main.html。 My issue is how do mobile browsers handle being minimized or closed? 我的问题是移动浏览器如何处理最小化或关闭? For example I'm using Chrome on my Android device and I login to the web app. 例如,我在Android设备上使用Chrome,然后登录到网络应用。 I do some actions and then minimize the browser to do other things on my phone. 我执行一些操作,然后最小化浏览器以在手机上执行其他操作。 3 hours later I open up Chrome with the tab still on the page and continue on with what I was doing earlier on the page. 3小时后,我打开了Chrome,但该标签仍在页面上,然后继续我之前在页面上所做的操作。 How can I intercept that and force them back to the login page after that time? 在那之后,我该如何拦截并迫使他们返回登录页面? I was thinking about storing the login time in a cookie and running a check against it every time a function is called but that doesn't seem very elegant. 我当时正在考虑将登录时间存储在cookie中,并在每次调用函数时对其进行检查,但这似乎并不十分优雅。

Use session. 使用会话。 Not use cookie. 不使用cookie。 I think you miss some issues about PHP. 我认为您会错过有关PHP的一些问题。

Cookies can always be changed. Cookies可以随时更改。

This should be very simple if you're using PHP, just keep track of the last action performed: 如果您使用的是PHP,这应该非常简单,只需跟踪最近执行的操作即可:

$_SESSION['last_action'] = time();

Then, when there is an Ajax request to your server, check that value. 然后,当有对服务器的Ajax请求时,请检查该值。 If it's too long ago, force the user to login again (so, the response would return some value indicating that you need to show the login screen again). 如果时间太久,请强制用户再次登录(因此,响应将返回一些值,指示您需要再次显示登录屏幕)。

I assume that you now how sessions work and you do start_session() etc.. 我假设您现在如何进行会话,并执行start_session()等。

You can also set an expiry date on your session if you don't mind doing that, but it might not be reliable . 如果您不介意这样做, 也可以在会话中设置到期日期,但这可能并不可靠

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM