[jQuery] Jquery, AJAX, and php session variables
Thanks to both of you for your responses,
I guess I'm still having a hard time wrapping my head
around what AJAX is doing with my session vars that
make it impossible to use in my existing PHP files. I
was under the impression that session vars are just
... around in the browser, so if they are set on an
ajax called page, that I'd be able to use them in the
page I called it from.
Even if I passed the session ID back in the
requestText, let's say... based on the behavior I saw
it's not seeing any of the variables I set in the ajax
file, so it doesn't matter if I have one of 5
variables but not the rest. Users won't always set
variables in a cookie, as that's just if they want to
be remembered.
I could switch to ajaxform, I just don't understand
why that would work for accessing the sessions instead
of what I'm doing. Should I be recalling
session_start? Or just reloading the whole page and
not bothering with ajax? etc...
thanks,
-kim
--- Jake McGraw <jmcgraw1@gmail.com> wrote:
> I believe you aren't handling the session correctly,
> remember, AJAX
> calls don't automatically attach the session ID to
> your URL variables,
> so you have to do it manually (if it's not in the
> cookie), either by
> directly attaching to the target url:
>
> $.getJSON("target.php?sid="+<session id>,...,...);
>
> or within you arguments (my preferred method):
>
> $.getJSON("target.php",{sid:<session id>,...},...);
>
> Then on the server side:
>
> // Check if our session is still active
> if (session_id() == "") {
> // Not active, check if sid was sent
> if (!$_REQUEST['sid']) {
> // Not sent, bounce them out (or attempt to
> log them in)
> } else {
> session_id($_REQUEST['sid']);
> session_start();
> }
> }
>
> Note, that this is the bare bones way of doing it
> and I can't vouch
> for the security. You're probably best off creating
> a class for
> handling sessions.
>
> - jake
> On 3/30/07, Erik
> Beeson <erik.beeson@gmail.com> wrote:
> > 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 <egypt1an@yahoo.com>
>