php 如何在 Wordpress 中设置 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11788355/
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 02:06:09  来源:igfitidea点击:

How to set a cookie in Wordpress

phpwordpresscookies

提问by user1269625

I'm trying to set a cookie in wordpress. I have my cookie set like this :

我正在尝试在 wordpress 中设置一个 cookie。我的 cookie 设置如下:

<?php setcookie('test', 'test', 0, '/', '/');  ?>

in header.php of my theme, but when I go to my browser to view my website I get this error

在我的主题的 header.php 中,但是当我转到浏览器查看我的网站时出现此错误

Warning: Cannot modify header information - headers already sent by (output started at /home/content/19/9468119/html/wp-content/themes/twentyeleven/header.php:27) in /home/content/19/9468119/html/wp-content/themes/twentyeleven/header.php on line 201

and also my cookie doesnt set. How do I set a cookie in wordpress?

而且我的 cookie 也没有设置。如何在wordpress中设置cookie?

I have also tried this

我也试过这个

 function set_new_cookie() {
    setcookie('test', 'test', 0, '/', '/');
}
add_action( 'init', 'set_new_cookie');

采纳答案by Jerome Ansia

You have to set them before anything is outputted

您必须在输出任何内容之前设置它们

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

看看那里:如何在 Wordpress 中设置、获取和销毁 cookie?

If you are using a theme in function.php

如果您在 function.php 中使用主题

function set_new_cookie() {
    //setting your cookies there
}
add_action( 'init', 'set_new_cookie');

Your expiration date is 0so you cookies will be deleted right away look at the php doc:

您的到期日期为 0,因此您的 cookie 将立即被删除,请查看 php 文档:

EDIT: From php.net:

编辑:来自 php.net:

If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

如果设置为 0 或省略,cookie 将在会话结束时过期(当浏览器关闭时)。

http://php.net/manual/en/function.setcookie.php

http://php.net/manual/en/function.setcookie.php

You have to set it like this for example :

例如,您必须像这样设置它:

setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */

回答by Reza Mamun

  1. Setting a Cookie: The below example will set the cookie that expired for one hour (60*60 seconds)since it set with COOKIEPATHand COOKIE_DOMAINwas defined by WordPress according to your site path and domain.

    setcookie( 'my-cookie-name', 'my-cookie-value', time() + 3600, COOKIEPATH, COOKIE_DOMAIN );
    
  2. Getting a Cookie: Getting a cookie can be done by using variable $_COOKIE which contains an associative array.

    $myCookie = isset( $_COOKIE['my-cookie-name'] ) ? $_COOKIE['my-cookie-name'] : 'Not Set!!';
    
  3. Delete or Unset a Cookie: It is same as the above instruction #1, just with a negative time to expire the cookie;

    setcookie( 'my-cookie-name', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN );
    
  1. 设置 Cookie:下面的示例将设置过期一小时(60*60 秒)的 cookie,因为它设置了COOKIEPATH并且COOKIE_DOMAIN是由 WordPress 根据您的站点路径和域定义的。

    setcookie( 'my-cookie-name', 'my-cookie-value', time() + 3600, COOKIEPATH, COOKIE_DOMAIN );
    
  2. 获取 Cookie:可以通过使用包含关联数组的变量 $_COOKIE 来获取 cookie。

    $myCookie = isset( $_COOKIE['my-cookie-name'] ) ? $_COOKIE['my-cookie-name'] : 'Not Set!!';
    
  3. 删除或取消设置cookie:与上述指令#1 相同,只是cookie 过期时间为负数;

    setcookie( 'my-cookie-name', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN );