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!
I'm a newbie in PHP and programmingin general, just started learning a couple days ago so please don't be mad if this question is too basic. I'm trying to create a page that will count the number of times a visitor has seen a page and store it in a cookie. I wrote this code:
<?php
if (empty($_COOKIE['counter'])) setcookie('counter', 1, time()+60*60*24*365);
else {
$_COOKIE['counter'] = $_COOKIE['counter'] + 1;
}
?>
<html>
<head>
<title>Visits counter</title>
</head>
<body>
<p>You have visited this page <?php if (empty($_COOKIE['counter'])) echo "1"; else echo $_COOKIE['counter']; ?> times</p>
</body>
</html>
But it doesn't work properly. It adds 1 time but then it stops. So the first time you load this page it shows 1, the second time 2, but all subsequent times it only shows 2. Why doesn't it work?
yeah...you definately have to find a different alternative....one thing though, I would have the following function in place so you get unique visitors counted only:
if (!isset($_COOKIE['counter'])) {
.. cookie code goes here ..
}
Remember that this only lasts as long as his session is stored or as long as the cookie is stored. If they delete their cookie than there is another hit for you.
Sorry I can't help out anymore but I am a newb as well.