telling if the page has been refreshed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
staypufinpc
Forum Newbie
Posts: 12
Joined: Thu May 01, 2003 7:59 pm

telling if the page has been refreshed

Post by staypufinpc »

Hi all,
I'm trying to keep track of statistics on a few pages to see how many times different users (and different types of users) are accessing certain pages, but I have no way of telling if a user is actually coming to the page for the first time or just refreshing the page. Obviously, I don't want to report any stats for a user who is simply refreshing the page. Ideally, checking $HTPP_REFERER against $PHP_SELF is a good idea, but $HTTP_REFERER is unreliable (even on the machine I'm testing it on). Any suggestions?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

How about using a session? SessionIDs can help you keep track of that until it times out (default is, I think, 3 hours).
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Code: Select all

<?
session_start();
$_SESSION['times']++;
if ($_SESSION['times'] > 1){
	echo "You've been here before. This is your ".$_SESSION['times']."th visit.";
} else {
	echo "This is your first visit. Try to refresh.";
}
?>
staypufinpc
Forum Newbie
Posts: 12
Joined: Thu May 01, 2003 7:59 pm

thanks

Post by staypufinpc »

Thanks, that gave me the right idea. Here's what I did:

Most of my pages are generated by some sort of function, so simply including a counter wouldn't work, because the pages bring up different records from a database, which means the counter would only mark the first record they look at (which wouldn't be true stats). Instead, I just made a variable out of the page's url, which included the id number for each record, and the username. I compare that at the beginning of the stat function to see if it matches the last url visited and, if it does, don't report the stat. Otherwise, I report it and get the true number of visits to each page by each user. Thanks for your insight.
Post Reply