简体   繁体   English

如何在 Wordpress 中设置 cookie?

[英]How do I set a cookie in Wordpress?

I'm having a problem with a cookie not serving.我遇到了 cookie 无法提供的问题。 We're adding this cookie as a workaround to stop the blocking of 3rd party cookies in both Safari and Firefox browsers.我们添加此 cookie 作为一种解决方法,以阻止 Safari 和 Firefox 浏览器中的第 3 方 cookie。

The below code is what I have included in our Wordpress child theme functions.php file (currently in place on our production server.下面的代码是我包含在我们的 Wordpress 子主题 functions.php 文件中的代码(目前在我们的生产服务器上。

Could someone please review and advise if you feel any changes are required.如果您认为需要进行任何更改,请有人查看并提出建议。 Thanking you in advance.提前谢谢你。

function set_my_cookie() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$user_agent = strtolower($user_agent);
$user_agent = ' ' . $user_agent;
$browser    = '';

if (strpos($user_agent, 'opera') || strpos($user_agent, 'opr/')) $browser = 'Opera';
else if (strpos($user_agent, 'edge')) $browser = 'Edge';
else if (strpos($user_agent, 'chrome')) $browser = 'Chrome';
else if (strpos($user_agent, 'safari')) $browser = 'Safari';
else if (strpos($user_agent, 'firefox')) $browser = 'Firefox';
else if (strpos($user_agent, 'msie') || strpos($user_agent, 'trident/7')) $browser = 'Internet Explorer';

if ($browser == 'Safari') {
$urlparts = parse_url(home_url());
$domain   = '.' . $urlparts['host'];
$time   = strtotime("+1 year");

if (!isset($_COOKIE['kppid'])) {
$kppid = uniqid('sf', true);
$kppid = str_ireplace('.', '', $kppid);

setcookie('x', $x $time, '', $domain);
} else {
$kppid = $_COOKIE['kppid'];
setcookie('x', $x, $time, '', $domain);
}
}
}

You have to create a init hook for cookie, see您必须为 cookie 创建一个 init 钩子,请参阅

add_action( 'init', 'my_setcookie' );
function my_setcookie() {
  setcookie( $name, $value, 24 * DAYS_IN_SECONDS, COOKIE_PATH, COOKIE_DOMAIN );
}

Then the cookies can be get by using setcookie function.然后可以使用 setcookie 函数获取 cookie。

echo "My cookie: " . $_COOKIE[$name];

By default, WordPress uses cookies to manage logged-in user sessions and authentication.默认情况下,WordPress 使用 cookie 来管理登录的用户会话和身份验证。 It also uses cookies to remember a user's name and email address if they fill out a comment form.如果用户填写评论表,它还使用 cookie 来记住用户的姓名和电子邮件地址。

However, many WordPress plugins on your website may also set their own cookies.但是,您网站上的许多 WordPress 插件也可能设置自己的 cookie。 For example, OptinMonster allows you to show different email optin forms to new vs returning visitors, and it does that by using cookies.例如,OptinMonster 允许您向新访问者和回访者显示不同的电子邮件选择表单,它通过使用 cookie 来实现这一点。

If you are using third party services on your website like Google Analytics or Google AdSense, then they may also set cookies on your website.如果您在您的网站上使用第三方服务,如 Google Analytics 或 Google AdSense,那么他们也可能在您的网站上设置 cookie。

So, you can also add cookies to your own WordPress site in same manner.因此,您也可以以相同的方式将 cookie 添加到您自己的 WordPress 网站。

you will need to add code to your theme's functions.php file or a site-specific plugin.您需要将代码添加到主题的functions.php 文件或特定于站点的插件中。 If you haven't done this before, then please take a look at how to copy and paste code snippets in WordPress .如果您以前没有这样做过,那么请查看如何在 WordPress 中复制和粘贴代码片段

First we will use the setcookie() function in PHP.首先我们将使用 PHP 中的setcookie()函数。 This function accepts the following parameters.该函数接受以下参数。

  • Cookie name饼干名称
  • Cookie value饼干值
  • Expire (Optional: sets a time period after which cookie expires)过期(可选:设置 cookie 过期的时间段)
  • Path (Optional, by default it will use the site's root)路径(可选,默认情况下它将使用站点的根目录)
  • Domain (Optional, by default uses your website's domain)域(可选,默认使用您网站的域)
  • Secure (Optional, If true then only transfers cookie data via HTTPS)安全(可选,如果为 true,则仅通过 HTTPS 传输 cookie 数据)
  • httponly (Optional, when set true the cookie is only accessible via HTTP and cannot be used by scripts) httponly(可选,当设置为 true 时,cookie 只能通过 HTTP 访问,不能被脚本使用)

Now let's add a code snippet to your WordPress site.现在让我们向您的 WordPress 网站添加一个代码片段。 This code stores the exact timestamp when a user visited your website in a cookie.此代码在 cookie 中存储用户访问您网站时的确切时间戳。

function wpb_cookies_tutorial1() { 
$visit_time = date('F j, Y  g:i a');
if(!isset($_COOKIE[$wpb_visit_time])) {
// set a cookie for 1 year
setcookie('wpb_visit_time', $current_time, time()+31556926);
}
} 

You can now visit your website and then check your browser cookies.您现在可以访问您的网站,然后检查您的浏览器 cookie。 You will find a cookie with the name wpb_visit_time .您会找到一个名为wpb_visit_time的 cookie。

Feel free to modify the code to make it more useful for your website.随意修改代码以使其对您的网站更有用。

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

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