php 如何在 WordPress 中设置、获取和销毁 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6183162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:35:32  来源:igfitidea点击:

How can I set, get and destroy cookies in WordPress?

phpwordpresscookies

提问by Ravichandran Jothi

How can I set, get and destroy cookies in WordPress?

如何在 WordPress 中设置、获取和销毁 cookie?

I surfed the web but I can't get clear ideas, please help me find how.

我在网上冲浪,但我无法得到明确的想法,请帮助我找到方法。

回答by Phil

You can either retrieve and manipulate cookies on the server side using PHP or client side, using JavaScript.

您可以使用 PHP 在服务器端检索和操作 cookie,也可以使用 JavaScript 在客户端检索和操作 cookie。

In PHP, you set cookies using setcookie(). Note that this must be done before any output is sent to the browser which can be quite the challenge in Wordpress. You're pretty much limited to some of the early running hooks which you can set via a plugin or theme file (functions.phpfor example), eg

在 PHP 中,您可以使用setcookie(). 请注意,这必须在任何输出发送到浏览器之前完成,这在 Wordpress 中可能是一个很大的挑战。您几乎只能使用一些早期运行的钩子,您可以通过插件或主题文件(functions.php例如)设置这些钩子,例如

add_action('init', function() {
    if (!isset($_COOKIE['my_cookie'])) {
        setcookie('my_cookie', 'some default value', strtotime('+1 day'));
    }
});

Retrieving cookies in PHP is much easier. Simply get them by name from the $_COOKIEsuper global, eg

在 PHP 中检索 cookie 要容易得多。只需从$_COOKIE超级全局中按名称获取它们,例如

$cookieValue = $_COOKIE['my_cookie'];

Unsetting a cookie requires setting one with an expiration date in the past, something like

取消设置 cookie 需要设置一个过去的过期日期,例如

setcookie('my_cookie', null, strtotime('-1 day'));


For JavaScript, I'd recommend having a look at one of the jQuery cookie plugins (seeing as jQuery is already part of Wordpress). Try http://plugins.jquery.com/project/Cookie

对于 JavaScript,我建议您查看其中一个 jQuery cookie 插件(因为 jQuery 已经是 Wordpress 的一部分)。试试http://plugins.jquery.com/project/Cookie

回答by Smruti Ranjan

Try this code inside function.phpto play with Cookies in WordPress

在里面试试这个代码function.php,在 WordPress 中使用 Cookies

Set a Cookie in WordPress

在 WordPress 中设置 Cookie

add_action( 'init', 'my_setcookie' );
function my_setcookie() {
setcookie( 'my-name', 'my-value', time() + 3600, COOKIEPATH, COOKIE_DOMAIN   );
}

Get a Cookie in WordPress

在 WordPress 中获取 Cookie

add_action( 'wp_head', 'my_getcookie' );
function my_getcookie() {
$alert = isset( $_COOKIE['my-name'] ) ? $_COOKIE['my-name'] : 'not set';
 echo "<script type='text/javascript'>alert('$alert')</script>";
}

Delete or Unset a Cookie in WordPress

在 WordPress 中删除或取消设置 Cookie

add_action( 'init', 'my_deletecookie' );
function my_deletecookie() {
setcookie( 'my-name', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN );
}

回答by Gendrith

To setthe cookie on wordpress I used the $domain value. With it I get to use the cookie value across the entire site.

为了在 wordpress 上设置cookie,我使用了 $domain 值。有了它,我就可以在整个站点中使用 cookie 值。

$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;

setcookie("cookie_name", 'cookie_value', 0, '/', $domain);

To unset

取消设置

setcookie("cookie_name", '', time()-1000, '/');