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! Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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 ? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 ... Quote Link to comment 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; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.