Question

I have a issue setting a cookie with PHP.

private function setCookie()
{

    if(isset($_COOKIE['billForm']))
    {
        setcookie("billForm", "", time()-3600);

        $cookie = $this->group . ',' . $this->month . ',' . $this->year;
        setcookie("billForm", $cookie, time()+3600);

    } else {

        $cookie = $this->group . ',' . $this->month . ',' . $this->year;
        setcookie("billForm", $cookie, time()+3600);

    }

}

I have controlled that the else statement is being run. I also validated the $cookie, tried to set it $cookie = 'foo'; aswell. I have looked in two different browsers chrome and firefox and with different cookie browsers but not a trace of the cookie. I also tried on a different computer.

Also nothing in the nginx error.log

Any suggestions?

No correct solution

OTHER TIPS

You should try to set the domain of the cookie

setcookie("billForm", $cookie, time()+3600, 'path_on_server', 'yourdomain.com');

On my local , i get a conflict with the name of the function , appache said the function setCookie() redeclare setcookie() . Solved by rename it as setMyCookie()
Are you sure about your datas $this->group . ',' . $this->month . ',' . $this->year; ?
For me it's working ( but i replace your data by hardcoded string )

Here is some tested code, based on your code, that sends, shows and deletes the 'biilForm' cookie as required.

Runtime Env: PHP 5.3.18, Apache (XAMPP) on windows XP as 'localhost'.

Hope it helps.

My typical output in the browser:

did cookie "billForm" arrive? 
string 'notset,march,2014, newCookie: 1393862228' (length=40)
cookie "billForm" sent...

Code:

<?php
// Q22149976


/*
 *  I need a test class to run your code...
 */
class CookieTest {

private $group   = 'notset';
private $month   = 'march';
private $year    = 2014;

public function __construct($group = 'notset', $month = 'march', $year = 2014)
{
  $this->group = $group;
  $this->month = $month;
  $this->year  = $year;
}

public function setCookie() // name not important
{
    if(isset($_COOKIE['billForm']))
    {
        setcookie("billForm", "", time()-3600);

        $cookie = $this->group . ',' . $this->month . ',' . $this->year . ', newCookie: '. time();
        setcookie("billForm", $cookie, time()+3600);

    } else {

        $cookie = $this->group . ',' . $this->month . ',' . $this->year;
        setcookie("billForm", $cookie, time()+3600);
    }

}

} // CookieTest end -------------------------------------

echo 'did cookie "billForm" arrive? <br/>';

if (isset($_COOKIE['billForm'])) {
   var_dump($_COOKIE['billForm']); // you can see what comes in!
}
else {
  echo '<br />billform Cookie: IS MISSING!<br />';
}

/*
 * send the cookie...
 */

$cookieTest = new CookieTest();

if (isset($_POST['delcookie'])) {
    setcookie("billForm", "", time() - 24 * 60 * 60);
    echo 'cookie "billForm" deleted...<br />';
}
else {
    $cookieTest->setCookie();
    echo 'cookie "billForm" sent...<br />';
}

?>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Q22149976 - Cookie issues</title>
  </head>

  <body>
    <div class="main" id="main">

        <strong><?php echo 'Cookie testing..'?></strong><br/>

        <form method="POST" action="">
          delete cookie: <input type ="checkbox" name="delcookie" value="delcookie">

          <br /><input type="submit" value="GO"/>
        </form>
    </div>
  </body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top