1

I followed the wikihow tutorial for building a secure session management system : http://www.wikihow.com/Create-a-Secure-Session-Managment-System-in-PHP-and-MySQL

and it works fine.

Now, I have a problem that session id cookie _s change (and log in information) when 2 successive ajax are called like this :

<input type="button" value="go" id="mybutton" />
<script>
        $("#mybutton").click( function() {
            $.get("ajax1.php");
            $.get("ajax2.php");
        });

</script>

where both ajax1.php and ajax2.php have just require the session class file

<?php
require('session.class.php');
$session = new session();
$session->start_session('_s', false);
?>

upon clicking the button, the session id stored in _s cookie changes to a new one. I added an alert between the two ajax like this

<input type="button" value="go" id="mybutton" />
<script>
        $("#mybutton").click( function() {
            $.get("ajax1.php");
            alert("anything");
            $.get("ajax2.php");
        });

</script>

by separating the two ajax calls, the session id didn't change.

EDIT: the actual code isn't like this ,, but rather many separate buttons each have it's ajax,, but it happens that a user clicks buttons successively before response.

<input type="button" value="go" id="mybutton" />
<script>
        $("#mybutton").click( function() {
            $.get("ajax1.php");
        });

</script>
<input type="button" value="go" id="mybutton2" />
<script>
        $("#mybutton2").click( function() {
            $.get("ajax2.php");
        });

</script>

any ideas??

1 Answer 1

3

When you make the first AJAX call, the server starts a session and sends the session ID back to the browser in a cookie. In order for the second AJAX call to be in the same session, it has to send that cookie back to the server.

But you're not waiting for the response to the first call before you send the second call. So the cookie hasn't been received, and it can't be sent with the second call. Anything that depends on the result of an AJAX call has to be done in its callback function. So you should do:

$.get('ajax1.php', function() {
    $.get('ajax2.php');
});
Sign up to request clarification or add additional context in comments.

6 Comments

thanks .. but the actual code has only one ajax in a sequence ,, but has many buttons on the page and the users click on many buttons before waiting for the response .. how can I overcome this
Display some kind of "loading..." thing when they click on a button, and don't allow them to click on anything else until the response comes back.
I thought of doing this,, but that will annoy users as they have to make a lot of choices, each takes about 1 second to finish,, I thought there may be another solution
There's really no other solution. If you want two calls to be in the same session, you have to wait for the first one to finish before making the second one.
You could display the loader only the first time they click on any button. Once you get that reply, you know the session is set up, and all other buttons can be processed without making the user wait.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.