在注销之前和管理员登录后设置cookie [英] set cookie before log out and after admin login

查看:115
本文介绍了在注销之前和管理员登录后设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是wordpress的新手。



我对此进行了很多研究,但没有成功。我想在注销之前将cookie值设置为NULL,并在管理员登录后将cookie设置为自定义值。 wp_set_auth_cookie



setcookie( cookieName,``,time()+(1 * 60),true);



echo< script> console.log('Cookie必须设置null')< / script>;



但是即使登录注销后也没有获得成功的cookie值。

解决方案

从不编辑Wordpress核心文件!如果您有一天要更新Wordpress(并且要修复安全性问题),所有的修改都将丢失。



您需要做的是与 wp_login 一起使用和 wp_logout 操作。您使用 add_action ,因此它们将在这些事件上触发。这些功能和 add_action 调用发生在主题的 functions.php 文件中。



因此,在 functions.php 中,添加此代码可在登录时设置Cookie(仅适用于管理员):

  add_action('wp_login','add_custom_cookie_admin'); 
函数add_custom_cookie_admin(){
if(is_admin()){
setcookie('your_cookie_name','cookie value',time()+ 86400,‘/’); //在一天中过期
}
}

请注意,正如diziaq所指出的在他的答案中,您使用了错误的 setcookie 函数。



并在注销时删除Cookie:

  add_action('wp_logout','remove_custom_cookie_admin'); 
函数remove_custom_cookie_admin(){
setcookie('your_cookie_name',’,time()-3600);
}

与diziaq指出的一样,您需要删除Cookie-您可以请将其设置为 NULL


Hi I am new to wordpress.

I had researched lot about this but didn't succeed. I want to set cookie value NULL before logout and custom value after admin logged in.

So I put below code in wp-includes/puggable.php => in the function wp_set_auth_cookie

setcookie("cookieName", '', time() + (1 * 60) , true);

echo "<script>console.log('Cookie must set null')</script>";

But didn't get succeed cookie value remain same even after login-logout.

解决方案

NEVER edit Wordpress core files! If you update Wordpress one day (and you'll want to, as to fix the security issues), all your modifications will be lost.

What you need to do is to work with the wp_login and wp_logout actions. You attach on both of those actions functions with add_action, so they will be triggered on those events. Those functions and the add_action calls take place inside the functions.php file of your theme.

So inside functions.php, add this to set the cookie on login (for admin only):

add_action('wp_login', 'add_custom_cookie_admin');
function add_custom_cookie_admin() {
  if(is_admin()) {
    setcookie('your_cookie_name', 'cookie value', time() + 86400, '/'); // expire in a day
  }
}

Note that as diziaq pointed out in his answer, you was using wrong the setcookie function.

And to delete the cookie on log out:

add_action('wp_logout', 'remove_custom_cookie_admin');
function remove_custom_cookie_admin() {
  setcookie('your_cookie_name', '', time() - 3600);
}

Same as diziaq pointed out, you need to remove the cookie - you can't set it to NULL.

这篇关于在注销之前和管理员登录后设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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