Ben wrote:
Hi all,
This has been asked in the past in different ways. I'm still a bit
confused about setting up counters. I'm quite new to php. I've read
few tutorials about writing counters using cookie and sessions. It
sounds quite straight-forward. But what I'm unsure about is- if the
browser is set to accept cookies manually and the user blocks the
cookie, what happens? I think in that case, the counter is not
incremented because the value is not returned by the browser. Then we
are likely to get inaccurate counters. Is there a way to get
"accurate" counters?
Thanx
Ben
Hi Ben,
I think the articles did confuse you a bit. :-)
If you use a cookie for a visitorcounter, that counting only happens for
THAT visitor. But cookies can be important, read on.
You need to store somehow on the server how many visitors you have.
This can be done very easily by PHP using a plain simple file, or a
database.
If You are new to databases, I advise you to use a plain file, it is much
easier.
You just increase the counter every time a visitor calls a page on your
site.
This is often done by using a image that calls a PHP-script.
What about cookies?
What I describe above is not actually a visitorcounter: It is a
pagehitcounter.
If you want to count the number of unique visitors, you could use a cookie
for that.
This is how you do that:
1) If the countscript (possible the one that returns an image) is called, it
checks for the existance of a certain name set in the cookie.
eg "visitedalready"
1a) It is there: do nothing
1b) It is not there: set it in the cookie, and increase your counter by 1.
If cookies are disabled you could switch to SESSION-storage of that
"visitedalready".
The advantage is that Session will be build even if cookies are disabled.
This is done by url-rewriting, something like this:
phpscript.php?SESSIONID=asghdfhgasfdghasfdhgasfd
Hope that helps a bit.
Regards,
Erwin Moller