Forum Moderators: coopster

Message Too Old, No Replies

PHP Cookie page counts, event on #of pages

         

JAB Creations

8:10 pm on Apr 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am curious if I can make a cookie that just adds +1 everytime a page is loaded that talks to the cookie. This way I could count the pages. For my own personal amusement I want to put sounds on for certain numbers of pages. I'm learning a lot by fooling around on my own site but it's very helpful in learning these basic but important aspects nonetheless.

Here is how I currently set my bandwidth cookie...

<?php setcookie("bandwidth","4",time()+2592000,"/");?>

mcibor

8:45 pm on Apr 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you just need to read the cookie, increment it and set it:

<?php
if(!isset($_COOKIE["bandwidth"]) setcookie("bandwidth", "1", time() + 2592000, "/"); //first appearance
else
{
$cookie = (int)$_COOKIE["bandwidth"] + 1; //increase the transformed to (int) cookie
setcookie("bandwidth", $cookie, time()+2592000, "/"); //set the increased cookie
}?>

I think this should do, however I haven't tested it myself.

Best wishes
Michal Cibor

JAB Creations

11:11 pm on Apr 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thats a different cookie, not the one I wanna use. But I will test that out regardless...

JAB Creations

1:18 am on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



unexpected T_STRING on line 2

ironik

2:37 am on Apr 14, 2005 (gmt 0)

10+ Year Member



Should be:


<?php
if(!isset($_COOKIE["bandwidth"]))
{
setcookie("bandwidth", "1", time() + 2592000, "/"); //first appearance
} else {
$cookie = (int)$_COOKIE["bandwidth"] + 1; //increase the transformed to (int) cookie
setcookie("bandwidth", $cookie, time()+2592000, "/"); //set the increased cookie
}?>

mcibor

12:23 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I see now my mistake. There should be one bracket more:

if(!isset($_COOKIE["bandwidth"])) setcookie("bandwidth", "1", time() + 2592000, "/"); //first appearance

It doesn't matter if there's {} after if or not, as there is only one command.

Best regards!
Michal Cibor

JAB Creations

3:21 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Schweet! It works! Thanks! :-)