Forum Moderators: coopster

Message Too Old, No Replies

Cookie times Cookie = secret!

Multiplying numeric values of cookie counters

         

JAB Creations

7:25 am on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A thought struck me that I could use a cookie counter I have to make material on my site appear after the visitor visited X number of pages.

Then I took that idea further. What if there were two counters? One would count the general number of visits like if you visited, closed your browser and came back Thursday the session counter would be set to 2. Now the page counter merely counts every page you browser regardless of how often you close the browser.

I use frames on my site (no trash talk unless you can offer an actual live example to replace my setup) and so I could just use the session counter to be set by the frameset. Next each page would have the page counter.

Here is the current code (thanks to mcibor and ironik)...

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

Now lets just assume the frameset counter is named "sessions". How do I use PHP to ...

sessions * pages

I also want to say "If this number is equal to or greater {}.

JAB Creations

7:16 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



.bump.

mcibor

8:48 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jab!
to check if a number is greater or equal than just:
<?php
$a = 5;
$b = 4;
if($a >= $b) echo "a = $a is greater than b = $b";?>

To do the session counter is a bit trickier. I suppose you want to check just one user, not the whole lot. Then you'll need or two cookies or a cookie and a session. With two cookies: temp and session_count

mcibor

8:48 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
if(!isset($_COOKIE["temp"])) {
setcookie("temp", "1", 0, "/"); //start of the session, time=0 means the cookie is valid until the browser window closes
if(!isset($_COOKIE["session_count")) setcookie("session_count", "1", time()+2592000, "/");//first appearance
else setcookie("session_count", (int)$_COOKIE["session_count] + 1, time()+2592000, "/");//increase number of session visits
}//no else, because it's a session cookie, not a every page visit counter
?>

I would also store that info on the server (eg database), because then you will know which pages are visited the most and how many visits do you have. Also store date of the visit - it may become important.

Best regards
Michal Cibor
Thanks for appreciation!

JAB Creations

5:28 am on Apr 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Michal,
I'm still a bit new to PHP ... but if I am correct this code you have posted code through which a temp cookie is created. When the browser is closed and the visitor returns the temp cookie is created again and the other cookie counts the temporary cookie. Each time they return the temporary cookie is detected by the session_count cookie.

If I guessed right you rock! :-D If not you still rock for helping me out ... did I read the code correctly?

Ok thats before I tried the script out... I have a couple errors here...

Parse error: parse error, unexpected ')', expecting ']' in sessions.php on line 4

Then I changed...

if(!isset($_COOKIE["session_count"))

to...

if(!isset($_COOKIE["session_count"])

and got ...

Parse error: parse error, unexpected T_STRING in sessions.php on line 4

I then added another )...

if(!isset($_COOKIE["session_count"]))

but then recieved this message...

Parse error: parse error, unexpected $ in sessions.php on line 7

That's my attempt at a little debugging. A lot of thanks for your help (and a little hope for some continued for debugging!) :-D

John

mcibor

8:22 pm on Apr 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I missed out a ) and a ". The code below should work:
<?php
if(!isset($_COOKIE["temp"]))
{
setcookie("temp", "1", 0, "/"); //start of the session, time=0 means the cookie is valid until the browser window closes
if(!isset($_COOKIE["session_count"])) setcookie("session_count", "1", time()+2592000, "/");//first appearance
else setcookie("session_count", (int)$_COOKIE["session_count"] + 1, time()+2592000, "/");//increase number of session visits
}//no else, because it's a session cookie, not a every page visit counter
?>

And yes, you understood the code perfectly. Sorry for the mistakes
Michal Cibor