我在学校做一个项目,我需要控制用户有权访问其中一个页面。所有的功能都需要用到Ajax。
我的问题是Ajax的回调。挣扎了几天,但似乎无法修复它。如果我跳过Ajax,它就能工作。但我需要让它与ajax一起工作。我不知道该用什么作为回调,我尝试了很多解决方案,但都没有用。
所发生的是,任何人都可以到达页面,无论是会话。
Jquery:
<script>
$(window).load( function checkUser() {
$.post('calling_check_staff.php',
function(data){
$(). html(data); // I think the problem is here?????
});
});
</script>calling_check_staff.php
<?php
session_start();
include ("php-classes/classUsers.php");
// Calling the class User and controlling that the visitor is logged in
$user = new User();
if($user->loggedIn()) {
}
?>PHP类
public function loggedIn() {
// Control, accessed page via login page.
if(isset($_SESSION["username"]) && $_SESSION["username"] != null) {
} else { header("location:index.php");
} } 发布于 2016-10-15 07:17:38
不能在AJAX请求中设置header。您可以做的是发送登录状态或请求状态,以响应AJAX请求。您可以使用JSON字符串返回响应,并在客户端验证响应。
如果用户登录了,就像这样发送JSON,
{
"status":1
}如果用户没有被记录,就像这样发送JSON,
{
"status":0
}现在,检查客户端状态的值,以确定用户是否登录。如果用户已经登录,您还可以发送HTML内容和状态字段,以便在网页上显示。刷新或重定向页面到index.php,以防从脚本获取的状态字段为0。
https://stackoverflow.com/questions/40055956
复制相似问题