• I have tried everything but I know I’m missing something simple.
    Trying to set a cookie when someone goes to a page. I am using the functions.php file for the theme for that specific page. I have multiple themes on my site.
    Using code:
    function set_new_cookie() {
    setcookie(‘SESSIONKEY’, ‘W8FREEBTG’, 3600, ‘/’, ‘/les.com’);
    }
    add_action( ‘init’, ‘set_new_cookie’);

    Anyone see why my cookie is not being set. This code is at the bottom of the file. If I refresh the page shouldn’t the cookie show in the Cookie manager?

Viewing 1 replies (of 1 total)
  • Ran a quick test with your code..

    Works:
    setcookie('SESSIONKEY', 'W8FREEBTG');

    Doesnt work:
    setcookie('SESSIONKEY001', 'W8FREEBTG', 3600);

    Works:
    setcookie('SESSIONKEY002', 'W8FREEBTG', time() + 3600);

    The 3rd parameter “expiration date” is a Unix time stamp. Unix time started 1.1.1970. So 3600 seconds sets your cookie expiry date on the January 1st 1970. Means: for PHP it is already expired.

    http://msdn.microsoft.com/en-us/library/vstudio/ms178195%28v=vs.100%29.aspx –> “To assign a past expiration date on a cookie”

Viewing 1 replies (of 1 total)

The topic ‘Set a simple cookie in functions.php’ is closed to new replies.