$_SESSION variables don't get saved when automatically logged in

佐手、 提交于 2019-12-11 06:27:25

问题


I have an iPhone application that communicates with a database which includes things like users data (i.e. username, email, password), I created a PHP API for that and I connect to that API in the iPhone app. My problem here is: I save the user id in $_SESSION to restore it later when needed, when I manually log in, $_SESSION variables get saved easily and I can call them whenever I want, but when the app automatically logs in (which is obviously after the first log in where username and password get store in the iPhone keychain), $_SESSION variables doesn't get saved, I noticed that they only get stored and called with no problems after I log out which translates to after session_destroy();, here's the php code I use for logging in and out

session_start();

function login($user, $pass) {

    $result = query("SELECT IdUser, username FROM login WHERE username='%s' AND pass='%s' limit 1", $user, $pass);

    if (count($result['result'])>0) {

        $_SESSION['IdUser'] = $result['result'][0]['IdUser'];

        print json_encode($result);

    } else {
        errorJson('Authorization failed');
    }
}

function logout() {

    $_SESSION = array();
    session_destroy();
}

I tried to add session_destroy(); at the start of login method, but it didn't work.

By the way, I can alternatively save IdUser in NSUserDefaults or iPhone keychain and send it to the server when needed. Is it safe/not wrong to do such things on the app side rather than the server side?


回答1:


Try the logout function like this ...

function logout() {
session_start();
    $_SESSION = array();
    session_destroy();
}



回答2:


Try the logout function like this

function logout() {

   session_start();
   session_destroy();

}

  1. When you call the logout function,it doesn't call the session_start(),so you can't get the the session variables.You should get it before you will edit it.

  2. Detail:php.net session_destory()



来源:https://stackoverflow.com/questions/21868551/session-variables-dont-get-saved-when-automatically-logged-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!