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?