doulan9188 2016-10-15 06:50
浏览 36
已采纳

使用Jquery Ajax检查OOP PHP $ _SESSION

Im doing a project in school, and i need to control that the user has the right to access one of the pages. All functions need to go trough Ajax.

My problem is with the callback for the Ajax. Struggled for a couple of days, but cant seem to fix it. If i skip the Ajax, it works. But i need to get it to work with ajax. I dont know what to use as a callback, i've tried alot of solutions, none working.

What happens is that anyone can reach the page, no matter Session.

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 class

 public function loggedIn() {
    // Control, accessed page via login page.
    if(isset($_SESSION["username"]) && $_SESSION["username"] != null) {
     }  else { header("location:index.php");
            } } 
  • 写回答

1条回答 默认 最新

  • doucheng5705 2016-10-15 07:17
    关注

    You can't set header in a AJAX request. What you can do is send a logged in or request status in response to AJAX request. You can use JSON string to return the response and validate the same on client side.

    If the user is logged in send the JSON like this,

    {
        "status":1
    }
    

    If the user is not logged send the JSON like this,

    {
        "status":0
    }
    

    Now check the value of status on client side to know if the user is logged in or not. You can also send HTML content along with status field to show it on webpage if the user is already logged in. Refresh or redirect the page to index.php in case the status field is you get from script is 0.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。