检查用户登录的ajax页面是否更改 [英] Check if user logged in on ajax page change

查看:99
本文介绍了检查用户登录的ajax页面是否更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我正在构建的Webapp的一部分,我将需要检查用户何时更改页面是否已登录.在普通的非ajax站点上,这很容易,因为我可以将PHP会话条件语句放在标头中,并且在每次页面更改时调用标头都将确定是否显示登录页面,而是将其视为标头文件在初始加载的ajax应用程序中仅设置一次,我如何在用户发出ajax页面请求时检查会话是否仍然有效?因为我使用的是pushState和popState,所以我知道可以将条件设​​置为在URL发生更改时运行,但是我不知道如何编写该检查以在每次发生时运行.

As part of a webapp I'm building I'll need to check if the user is logged in whenever they change page. On a normal non-ajax site this is easy because I can just place the PHP session conditional statement in the header, and the header being called on every page change will determine whether the login page is displayed or not, but seeing as the header file is only set once within an ajax application, which is on the initial load, how can I check whenever the user makes an ajax page request whether or not the session is still valid? Since I'm using pushState and popState I know I can set the condition to run whenever the URL is changed, but I don't know exactly how to code the check to run whenever this happens.

任何人和所有帮助表示赞赏.

Any and all help appreciated.

谢谢.

推荐答案

您应该返回false,以便可以执行一些明智的操作,例如将用户重定向到登录页面,或者更改您在问题中提到的标题,使用 die('false')

You should return false so you can do something intelligent, like redirect the user to a log in page, or change the header as you mention in your question, instead of killing the script with die('false')

if(!isset($_SESSION['valid_user'])) { 

    return false;
}

或更妙的是,将json对象返回到脚本中,并使用jquery/javascript更改标头.

or better yet, return a json object to your script and use jquery/javascript to change the header.

$response = array();

if(!isset($_SESSION['valid_user'])) { 

    $response['logged_in'] = false;

} else {

    $response['logged_in'] = true;

}

// return to jquery for handling
return json_encode($response);

无论哪种方式,都表示您正在构建Web应用程序,因此您可能希望尝试处理事件而不是终止程序.

either way, you say you're building a web application, so you might want to try handling the event instead of killing the program.

这篇关于检查用户登录的ajax页面是否更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆