简体   繁体   中英

Wordpress Cookies via functions.php doesn't read cookie until page re-load?

I am using wordpress and have created a cookie in functions.php.

My problem is that this cookie is not read by the template/page when it's created but on it's 2nd page load.

Is there a workaround to this so I can read the cookie when it's created?

Here is my current code at functions.php:

<?php
function my_cookie() {
$myvalue = 'hello'; 
if (!isset($_COOKIE['myCookie'])) {
  setcookie("myCookie", $myvalue, time()+3600, "/", ".mydomain.com");
global $myglobal;
$_COOKIE['myCookie'] = $myglobal;

}
}
add_action( 'init', 'my_cookie');
?>

I like to save my search criteria to cookies to help prevent issues when a user tries to back out of a page and gets a "Would you like to resubmit?" message, so I set my cookies like the following, which allows me to use them on the first page load:

// Set cookie of posted values
add_action( 'init', 'set_new_cookie' );

function set_new_cookie()
{
    $COOKIE_NAME = 'my_cookie_name';

    $name     = $COOKIE_NAME;
    $expire   = LIFETIME;
    $path     = COOKIEPATH;
    $domain   = COOKIEDOMAIN;
    $secure   = false;
    $httponly = false;

    if ( $_POST )
    {
        $POST = array(
            'variable1' => $_POST['variable1'],
            'variable2' => $_POST['variable2'],
            'variable3' => $_POST['variable3'],
            'variable4' => $_POST['variable4']
        );

        foreach( $POST AS $key => $value )
        {
            // Set the cookie
            setcookie( $name."[$key]", $value, $expire, $path, $domain, $secure, $httponly );

            // Set the $_COOKIE global to have access to value on first page load
            $_COOKIE[$name][$key] = $value;
        }
    }
}

You can then have imediate access to:

print_r( @$_COOKIE['my_cookie_name'] );

This is by design. When using PHP to create a cookie, it does not also set the super global found in $_COOKIE. The cookie header has been sent and the cookie is set however.

The $_COOKIE super global on the other hand is set on page load by any cookies sent from the browser in its initial page request.

So when you set a cookie and it is sent to the browser and saved on the client side, the next request the browser sends to the server will also send along this previously saved cookie, which will set the $_COOKIE super global.

If you want to save changes made to the cookies server side, you can either create a class which stores the original cookies, then modifies them when you make changes to cookies through the class's cookie changing methods, then use the same class to retrieve any cookie data, or you can modify the $_COOKIE super global yourself at the same time you set the actual cookie with PHP.

Try setting the cookie from the file index.php. This is the first file that runs when you start your site and so the cookie should then be available by the time you get to the main page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM