Newbie question - Visits counter
Posted: Mon Sep 11, 2006 3:26 pm
Hi,
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:
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?
Thanks.
Earl
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:
Code: Select all
<?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>Thanks.
Earl