Forum Moderators: coopster

Message Too Old, No Replies

Need help with a cookie

         

Simone100

10:41 pm on May 22, 2007 (gmt 0)

10+ Year Member



Hello, this is supposed to show file1.txt, then file2.txt and so on on page load or page refresh. But it only shows the first 2 txt files. Can someone help me find out why?
I would think that as long as the cookie is still the same if (isset($_COOKIE['e'])) {
setcookie("e", $a++, time()+60*60*24*30);}
that it would go to the next text file but its not. Please let me know, thank you very much.

<?php
$max_files = 12;
$a = 0;
if($a == 0) {setcookie("e", $a, time()+60*60*24*30);} /* expire 30 days*/
if (isset($_COOKIE['e'])) {
setcookie("e", $a++, time()+60*60*24*30);}
if ($a == $max_files)
$a;
$include_file = "file" . $a . ".txt";
include ($include_file);
?>

barns101

7:31 am on May 23, 2007 (gmt 0)

10+ Year Member



You set $a as 0 at the very top of the page and so setcookie("e", $a++, time()+60*60*24*30); can't increase past 1.

You logic when setting the cookie also seemed to be incorrect.

Try this instead and adapt it for your specific needs (it doesn't stop at 12, by the way):


<?php
if (!isset($_COOKIE['e'])) $a = 0;
if($a == 0) setcookie("e", $a, time()+60*60*24*30); /* expire 30 days*/
if (isset($_COOKIE['e']))
{
$a = $_COOKIE['e']+1;
setcookie("e", $a, time()+60*60*24*30);
}
echo "file" . $a . ".txt";
?>

whoisgregg

1:25 pm on May 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A bit more readable code using the ternary operator [php.net]:

<?php 
$a = (isset($_COOKIE['e']))? intval($_COOKIE['e'])+1 : 1;
setcookie("e", $a, time()+60*60*24*30);
echo "file" . $a . ".txt";
?>

Simone100

8:02 pm on May 23, 2007 (gmt 0)

10+ Year Member



Those are great you guys. The reason I had the 0 was to get it to start over again when it reached a certain number. So how do I get it to start over? These aren't working.

<?php
$max_files = 12;
$b = 0;
if (!isset($_COOKIE['e'])) $a = 0;
if($a == 0) setcookie("e", $a, time()+60*60*24*30); /* expire 30 days*/
if (isset($_COOKIE['e']))
{
$a = $_COOKIE['e']+1;
setcookie("e", $a, time()+60*60*24*30);
}
if ($a == $max_files)
$b;
$include_file = "file" . $a . ".txt";
include ($include_file);
?>

<?php
$max_files = 12;
$b = 0;
$a = (isset($_COOKIE['e']))? intval($_COOKIE['e'])+1 : 1;
setcookie("e", $a, time()+60*60*24*30);
if ($a == $max_files)
$b;
$include_file = "file" . $a . ".txt";
include ($include_file);
?>

whoisgregg

9:21 pm on May 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can add multiple conditions inside the first expression. The code below loops from 1...6 then restarts at 1.

<?php 
$maximum = 6;
$a = (isset($_COOKIE['e']) && intval($_COOKIE['e']) < $maximum)? intval($_COOKIE['e'])+1 : 1;
setcookie("e", $a, time()+60*60*24*30);
echo "file" . $a . ".txt";
?>

Simone100

11:20 pm on May 23, 2007 (gmt 0)

10+ Year Member



That'll do it thanks a lot. I always get the code started but its hard for me to finish sometimes. Maybe someday I'll have to take a class. Thanks Gregg and barnes.