简体   繁体   中英

prevent mysql query on page referesh

I would like to prevent a script from repeating when a user refreshes a web page.

currently, I have a php header file that calls a mysql script that inputs the time a user logs in into my DB. I have noticed that on page refresh, the script is ran again, updating the DB with the time of refresh. This defeats the purpose of obtaining the time of login. The ultimate goal is to determine the length of time a user remains on my site. Is there a better way to go about this? right now, I am just using the NOW() mysql function to insert the date and time into the DB.

Thank you

Your login script should direct the user to another page, such as the dashboard or whatever the main page of your site is. Having a whole page just to tell the user they logged in is a case similar to Stopping the Proceedings with Idiocy , so just redirect straight to the main page.

By performing a redirect, reloading will just reload the main page, it won't reload the "log me in" part, thus solving your problem and making your site more efficient.

Try this:

<?php
   if ($_SESSION['user']) {
       header('location: /');
   } else {
       mysql_query('insert into table(dateline) values(NOW())');
       $_SESSION['user'] = 1; //assign user's info.
   }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM