如何在Wordpress中设置,获取和销毁Cookie? [英] How can I set, get and destroy cookies in Wordpress?

查看:165
本文介绍了如何在Wordpress中设置,获取和销毁Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置,获取和销毁Wordpress中的Cookie?

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

我在网上冲浪,但我无法得到明确的想法,请帮助我找到如何。 / p>

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

推荐答案

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

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

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

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.php for example), eg

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

在PHP中检索Cookie更容易。只需从 $ _ COOKIE 超级全局名称获取它们,例如

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

$cookieValue = $_COOKIE['my_cookie'];

取消设置Cookie需要设置过去的过期日期,例如

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

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






在一个jQuery cookie插件(看作为jQuery已经是Wordpress的一部分)。尝试 http://plugins.jquery.com/project/Cookie

这篇关于如何在Wordpress中设置,获取和销毁Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆