Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need a no reload hit counter 1

Status
Not open for further replies.

iostream71

Programmer
Mar 13, 2002
229
US
looking for a good, simple-to-use hit counter that doesn't process reloads. thanx =)
 
I don't know of one, but I have an idea of how you might develop one.

Use a session variable that records the last value of $_SERVER["HTTP_REFERER"]. If that value differs from the one reported by the current value, then I think you can be assured the user did not refresh. If it differs, increment the counter then set the value of the "last referer" session variable to the current referer.

Keep in mind, however, that some browsers will not sent the referer information to the server. In Opera, for instance, the user can configure whether that information should be set. ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
well, since i don't know much about php at all, i'll have to research "session variables"
 
Point your browser to

But here's the basics.

At the beginning of any script where you might need session variables, invoke the function session_start(). That will start the session handling system.

Tell PHP what variable or variables it should track. session_register ("last_http_referer"), for example. You only have to issue this command once anywhere in your site. From then on, any time you issue session_start() at the beginning of a script, the variable will be available.

From the issuance of sessaion_register ("last_http_referer"), you can access the contents of that variable either through $_SESSION["last_http_referer") or $HTTP_SESSION_VARS["last_http_referer"]. [NOTE: Both variables are available if you are using PHP version 4.1.0 or newer. Otherwise, only $HTTP_SESSION_VARS is available.]

In any script, the current referer is in either $_SERVER["HTTP_REFERER"] or $HTTP_SERVER_VARS["HTTP_REFERER"]. [Again, $_SERVER is available only in PHP version 4.1.0 or newer] ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
got a question:

if someone types the url in directly, i'm not going to get a referrer. how do you deal with that?
 
If they already have a cookie, and http_referer is already set, even if to blank, discard the increment.

If the user does not nave a session cookie already set, then accept the count.

It might also be useful to know when the last time a user hit the page was. You could store that in your session, too. You don't want to increment if you know the user was last there 15 seconds ago, but you might if the last time were three days ago. ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
i thought the session variables were chunked after the browser was closed...how can they persist for multiple days?
i know...too many question =P
 
They can last for as long as you wish them too. It's just that session cookies by default configuration last until the browser is closed.

The PHP configuration directive in php.ini "session.cookie_lifetime" says how long a cookie should last. In the default PHP configuration, this is set to 0, which means "until the browser is shut down". You can give that another value representing the number of seconds the cookie should be valid.

You can also override the value in php.ini by using the session_set_cookie_params() function. (documentation:

I'd recommend using session_set_cookie_params(). It means writing more code, by changing the overall configuration of PHP by less.
______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
i tried this test script, but it doesn't work (the else never registers)

<?php
session_start();
if (!session_is_registered('count')) {
session_register('count');
$HTTP_SESSION_VARS['count'] = 1;
} else {
$HTTP_SESSION_VARS['count']++ ;
}
?>

count is <?php echo $HTTP_SESSION_VARS['count']; ?>

 
Odd. I'm using PHP 4.2.1 with Apache 1.3.22 on RedHat Linux 7.3. When I hit the page using Opera, and I get a count of how many times I've hit the page. And if I restart my browser, it resets back to 1.

This is a dumb question, but are cookies turned on in your browser? ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
yeap, i thought of that too. they are allowed. is that syntax pretty much correct?
 
here's a link to a test page:


and html used for it is:

<html>
<head>
<title>none</title>
</head>
<body>
<?php
session_start();
if (!session_is_registered('count')) {
session_register('count');
$HTTP_SESSION_VARS['count'] = 1;
}else {
$HTTP_SESSION_VARS['count']++ ;
}
?>

count is <?php echo $HTTP_SESSION_VARS['count']; ?>

</body>
</html>
 
You are sending HTML before your session_start. Try moving that line to the beginning of you script.

______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
awesome, that was the problem. the only thing i don't like is browsers can prompt on setting cookies, which is a bit distracting
 
The cookie-set notification is usually a user-defined setting.

Also, you might want to look at your php.ini file. You should have received a warning from PHP about the fact that you are trying to send headers after you have already begun to set content.

Look at display_errors, display_startup_errors, and log_errors. ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
how do i veiw those settings....i'm on a hosted server

also, sometimes the the cookie doesn't get destroyed when i close the browser, is there a way i can manually destroy it after, say, x minutes?
 
To find out those setting, you'll probably have to chat with your sysadmin. They're in a file called php.ini, but where they might be on the server, and whether you'll have access, I don't know. On a hosted server, they may not be willing to turn on all the error stuff for you.

Every time session_start is called, PHP may activate garbage collection. I say &quot;may&quot; because there is a setting in PHP.ini which tells PHP the probability that it should perform garbage collection. Then there is a setting that tells PHP how old a session store should be before it deletes it. The settings are called session.gc_probability and session.gc_maxlifetime, respectively. ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top