List Info

Thread: Jquery, AJAX, and php session variables




Jquery, AJAX, and php session variables
country flaguser name
United States
2007-03-30 03:50:05
Hi folks,

I apologize if this is a little less Jquery and a
little more "general AJAX/PHP" in nature, but I
am
completely stuck and was hoping there's tribal
knowledge I failed to learn.

I have a login panel on each page of my site. The
actual authentication takes place in a file called
login.php, called via ajaxsubmit (async set to false).
login.php calls a few functions that are in an
included file called startsession.php (meaning,
startsession.php is included in every normal page;
it's not called/included using ajax or anything).
These startsession functions handle setting $_SESSION
vars once the user is authenticated (or removes them
if the user is logged out or information is corrupt,
etc).

Both the regular page and login.php call
session_start(). 

I'm seeing some very weird behavior. 

startsession appears to be setting the $_SESSION vars
correctly. The problem is, they don't seem to  be
sticking. First of all, startsession.php appears to
refresh itself AFTER the ajax call is complete, even
though the only places this file would be in are in
login.php (already called in ajaxsubmit) and the
regular file. When startsession.php refreshes itself,
it doesn't see the $_SESSION vars and subsequently
logs me out.

Furthermore, I was under the impression that I could
do something like this, after the ajax call:

var loggedin = <?=$_SESSION['loggedin']?>;

if (loggedin == true)
{
					$("div#login").hide();		$("div#logout&qu
ot;).unhide();
}
else
{
	$("div#login").unhide();
	$("div#logout").hide();
}

I had been hoping to avoid reading the responseText...
though after looking at it now, I'm realizing that
there's no way this would work; even though session
vars are set on, well, the session, it seems that
you'd have to reload the page for PHP to pick it up,
period. 

Any help would be greatly appreciated. I know this is
probably something easy. :/

thanks,
-kim


 
____________________________________________________________
________________________
No need to miss a message. Get email on-the-go 
with Yahoo! Mail for Mobile. Get started.
http://mobile.yahoo.com/
mail 

_______________________________________________
jQuery mailing list
discussjquery.com
http://jquery.com/discuss/


Re: Jquery, AJAX, and php session variables
user name
2007-03-30 04:36:42
I don't know much about your specific problem, but I thought
I'd share
how I do AJAXified login. First I have a form with action
set to the
login page so it will work without javascript. Then I hijack
the form
with ajaxForm() from the form plugin. I have the ajax return
JSON and
have a callback function like:

function(data) {
  if(data.loggedIn) {
    // Do logged in stuff
  } else {
    $('#login_error').html(data.errorMessage);
  }
}

Also, I use asynchronis  ajax and disable the fields and
display an
indicator in the form plugin's beforeSubmit() function. The
blockUI
plugin is good for doing this kind of thing.

Hope it helps.

--Erik

On 3/30/07, Kim Johnson <egypt1anyahoo.com> wrote:
> Hi folks,
>
> I apologize if this is a little less Jquery and a
> little more "general AJAX/PHP" in nature, but
I am
> completely stuck and was hoping there's tribal
> knowledge I failed to learn.
>
> I have a login panel on each page of my site. The
> actual authentication takes place in a file called
> login.php, called via ajaxsubmit (async set to false).
> login.php calls a few functions that are in an
> included file called startsession.php (meaning,
> startsession.php is included in every normal page;
> it's not called/included using ajax or anything).
> These startsession functions handle setting $_SESSION
> vars once the user is authenticated (or removes them
> if the user is logged out or information is corrupt,
> etc).
>
> Both the regular page and login.php call
> session_start().
>
> I'm seeing some very weird behavior.
>
> startsession appears to be setting the $_SESSION vars
> correctly. The problem is, they don't seem to  be
> sticking. First of all, startsession.php appears to
> refresh itself AFTER the ajax call is complete, even
> though the only places this file would be in are in
> login.php (already called in ajaxsubmit) and the
> regular file. When startsession.php refreshes itself,
> it doesn't see the $_SESSION vars and subsequently
> logs me out.
>
> Furthermore, I was under the impression that I could
> do something like this, after the ajax call:
>
> var loggedin = <?=$_SESSION['loggedin']?>;
>
> if (loggedin == true)
> {
>                                        
$("div#login").hide();         
$("div#logout").unhide();
> }
> else
> {
>         $("div#login").unhide();
>         $("div#logout").hide();
> }
>
> I had been hoping to avoid reading the responseText...
> though after looking at it now, I'm realizing that
> there's no way this would work; even though session
> vars are set on, well, the session, it seems that
> you'd have to reload the page for PHP to pick it up,
> period.
>
> Any help would be greatly appreciated. I know this is
> probably something easy. :/
>
> thanks,
> -kim
>
>
>
>
____________________________________________________________
________________________
> No need to miss a message. Get email on-the-go
> with Yahoo! Mail for Mobile. Get started.
> http://mobile.yahoo.com/
mail
>
> _______________________________________________
> jQuery mailing list
> discussjquery.com
> http://jquery.com/discuss/

>

_______________________________________________
jQuery mailing list
discussjquery.com
http://jquery.com/discuss/


Re: Jquery, AJAX, and php session variables
user name
2007-03-30 12:14:11
On 3/30/07, Kim Johnson < egypt1anyahoo.com">egypt1anyahoo.com> wrote:
called via ajaxsubmit (async set to false).

You should *really* avoid doing this as it completely freezes the entire browser until the request is complete (very bad!!). I highly recommend that you check out Mike Alsup's blockUI plugin ( http://www.malsup.com/jquery/block/) instead.

--
Aaron Heimlich
Web Developer
aaron.heimlichgmail.com">aaron.heimlichgmail.com
http://aheimlich.freepgs.com
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )