0

This is in a page called headersessioncookie.php

<?php
  session_start();
    if ( ! isset (  $_SESSION['loggedin'] ) ) {
      $_SESSION['loggedin'] = FALSE;
    }

  $expiry = time()+60*60*9000;
  setcookie('cookie[loggedin]', '', $expiry, "", "", "", TRUE);

  if ( ! isset (  $_COOKIE['cookie[loggedin]'] ) ) {
    $_COOKIE['cookie[loggedin]'] = FALSE;
  }

?>

This is in a page called test.php

<?php
  require_once('headersessioncookie.php'); //start session and cookie

  $_SESSION['loggedin'] = TRUE;
  $_COOKIE['cookie[loggedin]'] = TRUE;

?>

When I run test.php and then run this page below called test1.php ...

<?php
  require_once('headersessioncookie.php'); //start session and cookie

  echo "sessionvalue" . $_SESSION['loggedin'] . '<br>';
  echo "cookievalue" . $_COOKIE['cookie[loggedin]'] . '<br>';

?>

... I get

sessionvalue1
cookievalue

Why don't I get...

sessionvalue1
cookievalue1

...??

thanks_in_advance
  • 2,603
  • 6
  • 28
  • 44
  • parameter 6 is a boolean and your using it as a string so change your sting" " to true or false! Also if your only calling test1.php then your cookie doesnt get set to 1! – Rizier123 Oct 07 '14 at 08:38

3 Answers3

1

The superglobal variable $_COOKIE only contains the cookie values. If you modify this value won't affect to the cookie because you need to sent the headers to the browser to do so.

If you need to modify it you have to use the method setCookie because this will sent the headers with the new value.

Note Remember that the $_COOKIE only will be updated after use setCookie when you refresh the page.

Miguel
  • 1,361
  • 1
  • 13
  • 24
  • If the first time I do setcookie('cookie[loggedin]', '', $expiry, "", "", TRUE); and then in future I only do something like setcookie('cookie[loggedin]', 'cookievalue'); ... then will $expiry and secure=TRUE automatically get filled in from the first statement? – thanks_in_advance Oct 07 '14 at 09:02
  • Everytime you call setcookie you will send via headers the cookie. That means old cookies with the replace with the new one. I guess if you want to keep $expire and secure=TRUE you should sent it again – Miguel Oct 07 '14 at 12:31
1

So this should work:

File: headersessioncookie.php

<?php

    //Session
    session_start();

    if ( !isset($_SESSION['loggedin']) )
        $_SESSION['loggedin'] = FALSE;

    //Cookie
    $expiry = time()+60*60*9000;

    if ( !isset($_COOKIE['cookieloggedin']) )
        setcookie('cookieloggedin', '', $expiry, "", "", true);

?>

File: test.php

<?php
  require_once('headersessioncookie.php'); //start session and cookie

  $_SESSION['loggedin'] = TRUE;
  setcookie('cookieloggedin', '1', $expiry, "", "", true);

?>

File: test1.php

<?php
  require_once('headersessioncookie.php'); //start session and cookie

  echo "sessionvalue" . $_SESSION['loggedin'] . '<br>';
  echo "cookievalue" . $_COOKIE['cookieloggedin'] . '<br>';

?>

Please notice also:

-How to update a cookie: https://stackoverflow.com/a/6487597/3933332

-Is a Cookie Case Sensitive: https://stackoverflow.com/a/11312272/3933332

Community
  • 1
  • 1
Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

Answering my own question. Turns out there were 3 major problems with my code.

1) I was trying to set the cookie value by doing this:

$_COOKIE['cookie[loggedin]'] = FALSE;

Turns out one needs to use setcookie() to set the cookie value. Assigning a new value to $_COOKIE will change the value of that variable (within the scope of the same page), but it won't change the value inside the cookie (outside the scope of that page, calling $_COOKIE will yield the value stored in the cookie).

2) The following is incorrect

echo "cookievalue" . $_COOKIE['cookie[loggedin]'] . '<br>';

Instead it should be

echo "cookievalue" . $_COOKIE['cookie']['loggedin'] . '<br>';

3) Cookie necessarily has to be passed a string value. I was trying to pass a value = FALSE which is not a string. Instead, I could have correctly passed a value = 'FALSE'

thanks_in_advance
  • 2,603
  • 6
  • 28
  • 44