0

At first I had my setcookie() as:

$school = "Some Value";
setcookie("default_school", $school, time()+3600);
var_dump($_COOKIE);

That didn't work, so I did some searching, and found that I probably just needed to set a path, so I changed it to:

$school = "Some Value";
setcookie("default_school", $school, time()+3600, "/", ".mydomain.com");
var_dump($_COOKIE);

That still didn't work so I did some more investigating, and determined that my issue must just me setting the cookie too late. So I then started checking my if my headers were set in certain parts of my code, which needs to return bool(false).

var_dump(headers_sent());

First the top of my index.php, then at the top of my header.php, but still the same result; they both return true?

I feel like this should be so simple, and I'm getting nowhere. A push in the right direction would be highly appreciated.

Thanks, Isaac

EDIT: Just tried putting it before the init hook, and it returned false. I then put the setcookie function there, and still no luck.

1 Answer 1

5

If you’re doing this via a theme, add this line to your functions.php file:

function set_user_cookie() {
    $school = "Some Value";
    setcookie('default_school', $school, time()+3600);
}
add_action( 'init', 'set_user_cookie');

To display information add this line

echo $_COOKIE['default_school']; 
1
  • The path and domain does not need to be set?
    – Damainman
    Oct 27, 2013 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.