简体   繁体   English

通过functions.php的Wordpress Cookies在重新加载页面之前不会读取cookie吗?

[英]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. 我正在使用wordpress并在functions.php中创建了一个cookie。

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? 有没有解决方法,以便在创建cookie时可以读取它?

Here is my current code at functions.php: 这是我当前在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?" 我想将搜索条件保存到cookie中,以防止用户尝试退出页面并收到“您要重新提交吗?”的问题。 message, so I set my cookies like the following, which allows me to use them on the first page load: 消息,因此我将cookie设置如下,从而使我可以在第一页加载时使用它们:

// 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. 使用PHP创建cookie时,它也不会设置$ _COOKIE中的超级全局变量。 The cookie header has been sent and the cookie is set however. cookie标头已发送,但已设置cookie。

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. 另一方面,通过浏览器在其初始页面请求中发送的任何cookie,在页面加载时设置$ _COOKIE超级全局变量。

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. 因此,当您设置cookie并将其发送到浏览器并保存在客户端时,浏览器发送到服务器的下一个请求也将发送此先前保存的cookie,这将设置$ _COOKIE超级全局。

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. 如果要保存对cookie服务器端所做的更改,则可以创建一个存储原始cookie的类,然后在通过该类的cookie更改方法对cookie进行更改时对其进行修改,然后使用同一类检索任何cookie数据,也可以在使用PHP设置实际cookie的同时自行修改$ _COOKIE super global。

Try setting the cookie from the file index.php. 尝试从文件index.php设置cookie。 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. 这是您启动网站时运行的第一个文件,因此cookie应当在您到达主页时可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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