I'm having a problem where I set a session after checking login details, and then change the section respectively via ajax. The problem that I've noticed is that the session is only set once the page is refreshed - I was wondering if it would be possible to set the session via ajax as well?


Member Avatar

Yes you can certainly set the session via ajax. Just remember to place a session_start() at the head of your script. As session is a cookie, pages can't show it until the server responds (usually page refresh or new page).

When I test the login, I do the following:

1. Login with dummy user/pass
2. Send the input field values to a php file via ajax
3. Set a session = the email address entered
4. Display the session where the login/pass input fields were

However, after clicking login, it gives me a "Notice: Undefined index: " error - But if I press F5 and login again, then it works. So I think the session isn't being passed until the page refreshes. This is why I wanted to find out an alternative way to refreshing the page via ajax.

You have a disconnect somewhere because this certainly is possible to do. I have to agree with ardav, make sure that you have a "session_start()" at the beginning of all of your scripts.


Thanks a lot.

At the beginning of all my scripts meaning all my php includes?

...I tried doing this and got this error:

Notice: A session had already been started - ignoring session_start() in


Member Avatar

I assume you have a login function as opposed to just loose code in an include file. However, if the include file is only ever accessed after an ajax call, it shouldn't really matter. Here's an idea:

1. Call your js script from the onsubmit() or the onclick() call of your form or button. Your choice.
2. The js script send a POST call to your php login file and awaits a response - remember to pass the raw data from the form - let php do the validating/cleaning.
3. Your php login file:

  //clean, validate, verify the data - set the session if everything OK
  //you can just echo back a simple response or go json (my fave method) or xml etc 
  //echo something to say you're already logged in

I use jQuery to handle all my ajax calls. An example:

function login(){
  var user = $('#user').val();
  var pass = $('#pass').val();
  $.post("includes/login.inc.php", {"usr": user,"pw": pass},
      if(data.logged == '1'){      


Thanks again.

I've been messing around with it today and it seems like the problem came where I tried to customize the session.

When I use:


Then it doesn't work, but if I use:


Then it works...

This isn't really what I want though, I'm trying to set the session to a string, with the users login name. So something like:


Is it possible?


I've seen people use this technique as a way of casting to int:


I am pretty sure that this will do the same thing:


I don't think that is what you want because this may very well be just converting your session variable to a number, but I am not sure.

Member Avatar

is fine - have you tried it?

Why you're trying to do this...


...I can't imagine. This only makes sense if the session var is numeric, in which case you could do:


You may get an error if the session var doesn't yet exist. Check with !isset().