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?

Community
  • 1
  • 1
thecoparyew
  • 715
  • 1
  • 9
  • 24

2 Answers2

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.

Alexander Kludt
  • 854
  • 6
  • 17
  • I updated my question. I already start session when user log in. – thecoparyew Jun 23 '15 at 06:57
  • 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. – Alexander Kludt Jun 23 '15 at 06:58
  • And how should I restart the session? I tried sending the ajax request to image because of this post and accepted answer http://stackoverflow.com/questions/5962671/prevent-session-expired-in-php-session-for-inactive-user – thecoparyew Jun 23 '15 at 07:03
  • 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 – Alexander Kludt Jun 23 '15 at 07:06
  • 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.` – thecoparyew Jun 23 '15 at 07:25
  • You really should try to learn the difference between your javascript frontend and your php backend, what you are talking about there is using `session_start()` twice in one php script. But you are running it once while your users login, then just let them use the html form - the session timeout timer will start right after the login and won't be reset until you call a php script that contains `session_start()` again. – Alexander Kludt Jun 23 '15 at 08:56
0

In ajax you can post timeout as

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