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
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?

Link to comment
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
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
Share on other sites

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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.