php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33627 Setcookie problems
Submitted: 2005-07-10 05:35 UTC Modified: 2005-07-12 12:22 UTC
From: weathermon at optusnet dot com dot au Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.4.0 OS: Linux 2.4.18-3smp #1
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: weathermon at optusnet dot com dot au
New email:
PHP Version: OS:

 

 [2005-07-10 05:35 UTC] weathermon at optusnet dot com dot au
Description:
------------
The submit bug report doesn't have my PHP version in there, I am using 4.3.2.

When I use setcookie, it seems to only be setting the cookie every 2nd time I load the page for some reason.  I am getting the value from $_GET and storing that in the cookie.  

Just for debugging purposes, I am printing the thumbview parameter and the cookie value each time it's run.

Basically I want the cookie to be set to the thumbview's value each time I change it.  I have no idea why it gets set only when I refresh the page a second time.

Reproduce code:
---------------
<?php
  if ($_GET["thumbview"]!="") {
    $sdata = $_GET["thumbview"];

    if (!setcookie ("dirlisting", $sdata, time()+3600)) {
      print ("cookie NOT set!");
    } else {
      print ("cookie set!");
    }
  }

  print "<br>Thumbview: ".$sdata;
  print "<br>Cookie Value: ".$_COOKIE["dirlisting"];
?>

Expected result:
----------------
test.php?thumbview=0

cookie set!
Thumbview: 0
Cookie Value: 0

test.php?thumbview=1

cookie set!
Thumbview: 1
Cookie Value: 1

Actual result:
--------------
test.php?thumbview=0

cookie set!
Thumbview: 0
Cookie Value: 0

test.php?thumbview=1

cookie set!
Thumbview: 1
Cookie Value: 0

I refresh the page again and this time it sets the cookie:

test.php?thumbview=1

cookie set!
Thumbview: 1
Cookie Value: 1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-10 09:03 UTC] tony2001@php.net
Thank you for taking the time to report a problem with PHP.
Unfortunately you are not using a current version of PHP -- 
the problem might already be fixed. Please download a new
PHP version from http://www.php.net/downloads.php

If you are able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
Again, thank you for your continued support of PHP.


 [2005-07-12 12:17 UTC] weathermon at optusnet dot com dot au
I have upgraded to version 4.4.0 of php and still no go. 

test.php?thumbview=0

cookie set!
Thumbview: 0
Cookie Value: 0

test.php?thumbview=1

cookie set!
Thumbview: 1
Cookie Value: 0

I refresh the page again and this time it sets the cookie:

test.php?thumbview=1

cookie set!
Thumbview: 1
Cookie Value: 1



What I noticed was that when I ran the last test.php?thumbview=1, I didn't have to even put in ?thumbview=1, I just typed in test.php and this is what it said:

Thumbview: 
Cookie Value: 1

So, my understanding is that it's setting the cookie, however if I try and read it in the same session, it retrieves the data it saved before for some reason... and when I just run the test.php again without any parameters, it reads the saved cookie properly...

When I executed this from when the Cookie was set to 1 before, this is what I got:

test.php?thumbview=blah

cookie set!
Thumbview: 1
Cookie Value: 1


test.php

Thumbview: 
Cookie Value: blah


So yeah... it definitely seems to be saving the cookie.. but when I try and read it in the same session, it's retrieving the old data from somewhere still, and will only read the new data on the next refresh
 [2005-07-12 12:22 UTC] tony2001@php.net
http://php.net/setcookie :
Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 23:00:01 2026 UTC