开发者

Storing $_SESSION variable in class

开发者 https://www.devze.com 2023-02-16 21:27 出处:网络
I want to b开发者_JAVA技巧e able to store a $_SESSION variable from my class but it does not seem to be working. I searched stackoverflow and googled for quite some time and was not able to find suffi

I want to b开发者_JAVA技巧e able to store a $_SESSION variable from my class but it does not seem to be working. I searched stackoverflow and googled for quite some time and was not able to find sufficient information to know whether this is possible or not.

Can I store a $_SESSION variable in my class? I cant seem to access it outside of the class.

class myClass {

    public function authorizeLogin($email,$password) {

        global $_SESSION;

        //Query to authorize user
        $q_auth = $this->query("
            SELECT first_name,last_name,email,date_registered,access_id
            FROM users
            WHERE email = '$email' AND password = '$password'");

        $authorized = $q_auth ? 1 : 0 ;

        if($q_auth) {
            //Loop through values and assign in session
            foreach($q_auth as $key => $value) {
                $_SESSION[$key] = $value;
            }
        }

        return $authorized;
    }

}


$_SESSION is superglobal - no need to use global.

Your code looks fine - but did you run session_start();?


I might be mistaken, but I believe you have to include the class before you start the session. Then create your object. For some odd reason that I cannot explain I remember hearing this somewhere.

So the order should be include("myClass.class.php"); session_start(); $myClass = new myClass();

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号