AJAX请求中的PHP会话变量

时间:2014-08-29 22:28:30

标签: javascript php ajax session

通过AJAX请求使用会话变量我遇到了极大的困难。层次结构是:

i. index.php
   require_once('config.php');
   <script class="include" type="text/javascript" src="js/jquery.manage.js"></script>  

ii. config.php
    session_start();
    $loggedInUser = $_SESSION["user"];

iii. manage.js
     $.post('functions.php', 'checkPermissions', function(data){});

iv. functions.php
    checkPermissions(){
       if ($loggedInUser->permission == "1"){
       //stuff
       }
     }

我认为$ loggedInUser将在config.php中全局化,因此可以访问functions.php。但是我通过ajax请求运行的问题是什么?有时我觉得这意味着我所请求的PHP文件位于一个独立的星球上,并且不与全局PHP变量或会话变量交互。每次变化都会出错。我已尝试直接在functions.php中调用$ _SESSION变量(使用session_start())但是我收到类似

的错误
The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "loggedInUser" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition

1 个答案:

答案 0 :(得分:1)

我不是说必须这样做,因为我不知道你的loggedInUser是如何工作的,但是你不能在登录时分配那个班级中的$_SESSION['user']吗?看看我的例子。那么它将在一个会话中,你总是只能通过它的会话变量来访问它。我不确定你的AJAX调用是做什么的,但被调用的页面可能不需要检查除$_SESSION之外的任何函数。

i. index.php
   require_once('config.php');
   <script class="include" type="text/javascript" src="js/jquery.manage.js"></script>  

ii. config.php
    session_start();

// Your login class
class loggedInUser
    {
        // Default actions
        public  function __construct()
            {
                if(isset($_POST['login'])) {
                        // Assign the user info to array (or false/0 if user/pass wrong)
                        $_userinfo  =   $this->ProcessLogin();

                        // If user is good, then assign all the returned db info
                        if($_userinfo !== 0) {                          
                                // Here is where you would assign the usergroup/permissions
                                $_SESSION['user']       =   $_userinfo['user'];

                                // assign more session vars from returned info 
                                $_SESSION['username']   =   $_userinfo['username'];
                                $_SESSION['first_name'] =   $_userinfo['first_name'];
                                //...etc.
                            }
                        else
                            $_error['login']    =   'Incorrect User/Password';
                    }
            }

        protected   function ProcessLogin()
            {
                // Include db connection if not in assigned as a global
                include_once($_SERVER['DOCUMENT_ROOT'].'/my/dbconnection.php');
                // -->Check user/pass against db code here<--
                // Presume $_userinfo is the checked against credentials and user is valid,
                // This is just a representation of what your DB returns for demonstration
                $_userinfo = array('id'=>123,'username'=>'dougfrank','user'=>1);                    

                // If row count for user is 1 (whatever the logic is here)
                return (!empty($_userinfo))? $_userinfo: 0; 
            }
    }

$_setUser   =   new loggedInUser();

iii. manage.js
     $.post('functions.php', 'checkPermissions', function(data){});


// This is how you would check whenever you are checking
// who has user 1 credentials
if($_SESSION['user'] == 1) {
        // do stuff.
    }