Jump to content

Looping problem


seikan

Recommended Posts

I'm doing a PHP script timer and write the duration to MySQL database every second. The code is something like this:

 

for($i=0; $i<60; $i++){

    //Update MySQL database

    sleep(1);

}

 

However, this script won't stop after I close my browser. It keep updating my database until it finish the loop. Is there any solution for this?

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/62799-looping-problem/
Share on other sites

As far as i'm aware, this shouldn't happen. It was my understanding that you can MAKE it happen, by using the ignore_user_abort() function. Perhaps i'm wrong.

 

Edit: You can also set this in php.ini - perhaps this has been turned on in there for some reason.

Link to comment
https://forums.phpfreaks.com/topic/62799-looping-problem/#findComment-312696
Share on other sites

I am guessing this query would be heavy on the server.

Writing to the Database every second.

 

Good website, with 100 people on the site at one time

100 queries writing to the database EVERY second

600 queries a minute

ouch

 

What are you trying to do?

 

Actually, I want to track the duration of visitor staying on a specific page. Is it possible to be done in PHP, instread of other languges ?

Link to comment
https://forums.phpfreaks.com/topic/62799-looping-problem/#findComment-312771
Share on other sites

This isn't possible purely in PHP. Since php is a server side language, it has already been executed once the page has loaded. However, you could possibly achieve this using an ajax technique:

 

1.) Your php page loads. You record the time of the page load for the user in your database.

2.) You use the javscript onunload event to run a function which makes a request to a php page, which takes the differance between the time it is called, and the time in the database, to work out how long a user has been on a page.

 

Of course, it would be completely useless for people without javascript/who have javascript turned off.

 

 

Link to comment
https://forums.phpfreaks.com/topic/62799-looping-problem/#findComment-312782
Share on other sites

Like I said; Google have a suite of free tools which allows you to work out how long people spend on pages- it's called Google analytics, and it uses JavaScript.

 

As has been mentioned, PHP can't be used without another technology, such as JavaScript.

Adding a function to the onUnLoad handler of your page which fires an "I'm leaving the page" function is what you need to do.

 

Getting PHP to make requests to your SQL server every second someone is on your page will probably result in a big fat bill from your host.

Link to comment
https://forums.phpfreaks.com/topic/62799-looping-problem/#findComment-313794
Share on other sites

Finally, I found what I want.

 

<?
echo str_repeat(" ",300);
ignore_user_abort(true); //this way, the user can stop the output, but not the script.
while (true) {
         echo "test<br>\n";
         flush();
         sleep(2);
         if (connection_status()!=0){
                 include ('dbconnect.inc');
                 $sql="delete from online_users where online_user=$user";
                 $sql_exec=pg_exec($vChatDB, $sql);
                 die(); //kills the script
         }
}
?>

 

I found this from http://www.php.net/manual/en/function.connection-status.php, but I think still need some modifications. Anyway, thank for helps ...

Link to comment
https://forums.phpfreaks.com/topic/62799-looping-problem/#findComment-313819
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.