seikan Posted August 1, 2007 Share Posted August 1, 2007 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 More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 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 More sharing options...
onlyican Posted August 1, 2007 Share Posted August 1, 2007 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? Link to comment https://forums.phpfreaks.com/topic/62799-looping-problem/#findComment-312700 Share on other sites More sharing options...
seikan Posted August 1, 2007 Author Share Posted August 1, 2007 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 More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 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 More sharing options...
mrjcfreak Posted August 1, 2007 Share Posted August 1, 2007 Or just got a google analytics account, use no effort and find out not only how long they stayed, but also where they went to, where they came from, what browser they were using and what they had for breakfast*. *ok, maybe not. Link to comment https://forums.phpfreaks.com/topic/62799-looping-problem/#findComment-312790 Share on other sites More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 Haha. It won't be long before google do know what you had for breakfast. Link to comment https://forums.phpfreaks.com/topic/62799-looping-problem/#findComment-312792 Share on other sites More sharing options...
seikan Posted August 2, 2007 Author Share Posted August 2, 2007 Seem like nobody can help me about this. For some reasons, I only able to use PHP in this case. Any idea? Actually what I want is a script that counts time. Once user close their browser or leave that page, the timer stopped and save the duration into database. Link to comment https://forums.phpfreaks.com/topic/62799-looping-problem/#findComment-313776 Share on other sites More sharing options...
mrjcfreak Posted August 2, 2007 Share Posted August 2, 2007 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 More sharing options...
seikan Posted August 2, 2007 Author Share Posted August 2, 2007 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 More sharing options...
DeadEvil Posted August 2, 2007 Share Posted August 2, 2007 $i =""; for($i=0; $i<60; $i++){ //Update MySQL database sleep(1); break; } Link to comment https://forums.phpfreaks.com/topic/62799-looping-problem/#findComment-313843 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.