Jump to content
  • Advertisement
Sign in to follow this  
deadlydog

[web] How to detect a refresh in PHP

This topic is 5415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I'm currently just making a counter for my webpage in php. I'm just storing the counter in a file, and each time the page loads I read the file, increment it, then write the file back, but it increments even if the user just hits refresh. I would like to change this so that the counter doesn't go up so fast. Does anyone know if there's a way to check if the page has been refreshed so I don't increment my counter? Thanks

Share this post


Link to post
Share on other sites
Advertisement
You could check the referer, I suppose.

Note that any method for maintaining state in HTTP can usually be fooled fairly easily (assuming that the state info or a handle to the state info is being held/transmitted client-side, as is predominantly the case).

I think the best thing to do would be to check for unique visits to the page or visits with a certain delay between them, if you're actually trying to figure out how frequently a person visits a page.

Share this post


Link to post
Share on other sites
You can set a session-level cookie for each person that visits your page, and if they have this cookie then the counter ignores them. So the first time they hit the page, the counter increments and the cookie is set. Any other times they hit the page during that session, the counter is unaffected.

Share this post


Link to post
Share on other sites
So if I use a session cookie to keep track or it, does that mean that if the person visists the site again the next day that it's not going to count them again because they still have the cookie on their pc?

Share this post


Link to post
Share on other sites
Nope. Session cookies only exist for the duration of the session. If your sessions are set to expire after twenty minutes, then it will expire 20 minutes after the latest visit to the site. Each visit to the site resets the timeout.

Share this post


Link to post
Share on other sites
When the user visits, get his IP via:
$ip = getenv("REMOTE_ADDR");


If the IP doesn't exist in your MySQL table, add it to the table. The number of rows in the table will now represent the number of unique hits you've received.

For more information about inserting into a MySQL table, check PHP.net out.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!