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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?