2

I have a web page with a huge form to fill. In most cases, session time out and users lost a lot of data. I searched for this problem and found this Prevent session expired in PHP Session for inactive user

I implemenet ajax call

function heartbeat() {
    clearTimeout(window.intervalID);
    $.ajax({
        url:    "trash.png",
        type:   "post",
        cache:  false,            
        dataType: 'json',
        success: function(data) {
        },
        complete: function() {
            window.intervalID = setTimeout(function() {
                heartbeat();    
            }, 300000); 
        }
    });
}

and call heartbeat(); in $(document).ready, trash.png is in the same directory as file where I have jQuery code with Ajax.

I checked with fiddler and jQuery is sending requests to trash.png every 5 minutes. But after 30 minutes my session still expires.

session_start(); is called when user log in to the web page.

What am I doing wrong?

2 Answers 2

1

You can not keep the session alive without calling a php script which starts a session, just downloading a png file will not keep the session from dying. Create a PHP Script like this:

<?php session_start(); ?>

Put it into the directory and call this instead of that trash.png asset.

You might need to call other things before calling session_start() depending on how you are starting it in your other scripts.

Sign up to request clarification or add additional context in comments.

6 Comments

I updated my question. I already start session when user log in.
This won't help, the session will timeout if you do not call a php script that (re)starts the session before the timeout occurs.
And how should I restart the session? I tried sending the ajax request to image because of this post and accepted answer stackoverflow.com/questions/5962671/…
As I already stated in my answer, create a php script called hearbeat.php put the code I show above in it, upload it to the directory where your script is and call hearbeat.php instead of trash.png
Ok I'll try this. But I'm still a bit concerned because of this from PHP docs: 4.3.3 As of PHP 4.3.3, calling session_start() after the session was previously started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.
|
0

In ajax you can post timeout as

jQuery.ajax({
       url: 'ajaxhandler.php',
       success: function (result) {                               
            returned_value=result;
       },
       timeout: 10000,
       async: false
    });

Comments

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.