简体   繁体   English

如何在我的网站上阅读wordpress cookies?

[英]How to read wordpress cookies in my website?

我想将我的网站与wordpress集成。我想知道如何阅读wordpress cookies,以便我不需要在我的网站再次验证用户。我尝试在我的网站中包含wordpress的头文件,但后来我是无法连接到我的网站的数据库。其中两个是不同的。我可以设置其他参数的cookie,如用户级别等?我的网站是用PHP编写的。

In your website, include that code at the top of each files : 在您的网站中,在每个文件的顶部包含该代码:

<?php 
define('WP_USE_THEMES', false);
require('./blog/wp-blog-header.php');
?>

...assuming your blog is in ./blog/ . ...假设您的博客位于./blog/

It includes the whole wordpress stack. 它包括整个wordpress堆栈。 You will have access to all wordpress functions in your code. 您可以访问代码中的所有wordpress函数。 With that you can easily check if user is logged in, roles and capabilities, but also retrieve posts or so. 有了它,您可以轻松检查用户是否已登录,角色和功能,还可以检索帖子等。

Then in you code, to check user : 然后在你的代码中,检查用户:

if (is_user_logged_in()) { ... } 

Codex : is_user_logged_in() Codex:is_user_logged_in()

You can also include a logout link : 您还可以包含退出链接:

<a href="<?php bloginfo("url"); ?>/wp-login.php?action=logout/">Logout</a>

If your blog and your site are not on the same domain or subdomain, you have to customize cookie domain in wp-config.php 如果您的博客和您的网站不在同一个域或子域中,则必须在wp-config.php自定义cookie域

define('COOKIE_DOMAIN', '.domain.com'); // Share cookie on all subdomains

EDIT 编辑

If you really just want to read the wordpress cookies (which is a good choice for performance) : the cookie name is stored in a constant AUTH_COOKIE. 如果你真的只想阅读wordpress cookies(这是一个很好的性能选择):cookie名称存储在一个常量AUTH_COOKIE中。

AUTH_COOKIE is defined in /wp-includes/default-constants.php -> line 171 as AUTH_COOKIE在/wp-includes/default-constants.php -> line 171定义为

"wordpress_" + md5( get_site_option(siteurl) )

You have to retrieve or recompute AUTH_COOKIE then read $_COOKIE[AUTH_COOKIE]. 您必须检索或重新计算AUTH_COOKIE然后读取$ _COOKIE [AUTH_COOKIE]。

To parse it, look at wp_parse_auth_cookie() in wp-includes/pluggable.php @line 585 (indeed the format is simple user|expiration|hmac so split the chain by | and get the first element) 要解析它,请查看wp-includes/pluggable.php @line 585中的wp_parse_auth_cookie() (实际上格式为简单user|expiration|hmac因此将链拆分为|并获取第一个元素)

To make cookies work in Wordpress you need to set it special way like so in functions.php 要使Cookie在Wordpress中运行,您需要在functions.php中将其设置为特殊方式

function set_newuser_cookie() {
    if (!isset($_COOKIE['sitename_newvisitor'])) {
        setcookie('sitename_newvisitor', 1, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false);
    }
}
add_action( 'init', 'set_newuser_cookie');

So the important point is setcookie('sitename_newvisitor', 1, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false); 所以重要的一点是setcookie('sitename_newvisitor', 1, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false);

To get it 为拿到它,为实现它

if (isset($_COOKIE['sitename_newvisitor'])) {
     echo 'Welcome back!';
}
else {
     echo 'Hello new visitor!';
}

Note : You can change cookie name 'sitename_newvisitor', value, timeout, COOKIEPATH and COOKIE_DOMAIN to fit your needs 注意 :您可以更改cookie名称'sitename_newvisitor',值,超时,COOKIEPATH和COOKIE_DOMAIN以满足您的需求

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

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