Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Cookies and PHP

  1. #1
    New Coder
    Join Date
    Feb 2008
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Cookies and PHP

    How come when I have the following code:

    Code:
    $countere = '1';
    setcookie('counter',$counter,time() + (720)); // 86400 = 1 day
    The time doesn't ever get set? I have verified this through my browser and by closing my browser. If it matters I am using firefox to see this.

    The cookie says it expires: At end of session

    I am then reading the cookie information like:

    Code:
     <?PHP
    
    $counter = $_COOKIE["counter"];
    echo $counter;
    $newNumber = $counter +1;
    setcookie("counter", "$newNumber");
    echo $newNumber;
    if ($newNumber >6)
    {
    
    	echo "Sorry you need to wait 5 more minutes before logging in";
    	setcookie("counter", "$newNumber");
    }
    ?>

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,297
    Thanks
    10
    Thanked 282 Times in 281 Posts
    I'm not sure what the algorithm is you're using, but did you really want the "e" here:

    Code:
    $countere = '1';
    setcookie('counter',$counter,time() + (720)); // 86400 = 1 day
    Dave

  • #3
    New Coder
    Join Date
    Feb 2008
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks I removed the spare 'e' and was hoping that might fix the issue but it didn't.

  • #4
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,297
    Thanks
    10
    Thanked 282 Times in 281 Posts
    You might want to check the return values from setcookie(), it returns success or failure of the call. Specifically the docs say it must be called before any output to the browser, so I'm guessing the calls that you make after the echo's aren't working.

    Dave

  • #5
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    setcookie needs at least value set if you want to set time expiry too....

    eg;

    PHP Code:
    setcookie("cookiename","somevalue",time() + 60*60*24*30); 
    will set a cookie named cookiename with a value of somevalue expiring in 30 days


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •