I am having difficulty creating sessions for my mobile application that is written in JQuery. I want my application to check to see whether a user session exists, if not it redirects user to a login form. I use ajax to check the login credentials and the user details are returned in JSON format.
How can I store these JSON variables in a session so that I can access these within my app?
function ajaxPost(LoadUrl, redirect) {
jQuery.ajax({
type: "POST",
url: LoadUrl,
data: "email=" + $("#email").val() + "&pw=" + $("#pw").val(),
complete: function(xhr, statusText){
if (xhr.status == 401){
alert('Unauthorised');
}
} ,
success:function(response){
if(redirect != null){
alert(response);
<?
//session_start();
//$_SESSION['user'] = $response; // this is JSON: {"id":"45454","name":"Joe Blog","username":"joebloguser"}
?>
app.navigate(redirect);
}
}
});
}
I have commented out the PHP code that starts the PHP session as this seems to start a session whether or not the ajax call is made, therefore there is always a $_SESSION set, which I obviously do not want. Can anyone advise on the best way to proceed with this?
If you put php like that between your javascript, it is run at the moment the page is first loaded (before any javascript is run…) so that will not work.
You need to move your session logic to your
loadUrlscript; verify the login there and start the session if necessary.