简体   繁体   English

检查wordpress用户是否通过外部脚本登录

[英]Check if wordpress user is logged in via external script

I am trying to check if a user has logged into WordPress via an external script. 我正在尝试检查用户是否已通过外部脚本登录WordPress。

Wordpress is located in the /blog/ directory. Wordpress位于/ blog /目录中。 The external script is on the homepage (/index.php) 外部脚本位于主页(/index.php)上

The code below returns an empty array when I am logged in. 当我登录时,下面的代码返回一个空数组。

define( 'WP_USE_THEMES', false );   
require_once $_SERVER['DOCUMENT_ROOT'] . 'blog/wp-load.php'; 
if ( !function_exists( 'is_user_logged_in' ) ) { 
    require_once    $_SERVER['DOCUMENT_ROOT'] . 'blog/wp-includes/pluggable.php'; 
} 

    $current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
    // Not logged in.
} else {
    // Logged in.
    echo 'logged in';
}       

echo 'Username: ' . $current_user->user_login . '<br />';
    echo 'User email: ' . $current_user->user_email . '<br />';
    echo 'User first name: ' . $current_user->user_firstname . '<br />';
    echo 'User last name: ' . $current_user->user_lastname . '<br />';
    echo 'User display name: ' . $current_user->display_name . '<br />';
    echo 'User ID: ' . $current_user->ID . '<br />';

However this returns the following empty values: 但是,这将返回以下空值:

Username: 
User email: 
User first name: 
User last name: 
User display name: 
User ID: 0

After a bit of research, I have also tried the following in the config file which did not help. 经过一些研究,我还在配置文件中尝试了以下操作,但没有帮助。

define('COOKIE_DOMAIN', '.abc.com'); // your main domain
define('COOKIEPATH', '/');
define('COOKIEHASH', md5('abc.com')); // notice absence of a '.' in front

Any ideas why it is not returning anything? 有什么想法为什么它不返回任何东西?

Following script works fine. 以下脚本工作正常。

include('/var/www/html/wp2/wp-load.php');
//var_dump($_COOKIE);

global $current_user;
$current_user = wp_get_current_user();
var_dump($current_user->ID);
var_dump($current_user->display_name);

you should put the script on your WordPress root folder , then only WordPress can access the $_COOKIE and let you log in 您应该将脚本放在WordPress根文件夹中 ,然后只有WordPress可以访问$ _COOKIE并允许您登录

More info for debugging: First, try to print $_COOKIE 有关调试的更多信息:首先,尝试打印$_COOKIE

var_dump($_COOKIE);

And confirm your script can access the WordPress $_COOKIE 并确认您的脚本可以访问WordPress $ _COOKIE

it will look something like this 它看起来像这样

array (size=5)
    'wordpress_test_cookie' => string 'WP Cookie check' (length=15)
    'wordpress_logged_in_xxx' => string 'xx|xx|xxx|xxx' (length=125)
    'wp-settings-time-1' => string '1513059194' (length=10)
    'io' => string 'xxx-xx' (length=20)

I managed to get this working. 我设法使这个工作。 Make sure this is at the top of wp-config.php 确保这是wp-config.php的顶部

define('COOKIE_DOMAIN', '.xxxfff.com'); 
define('COOKIEHASH', md5('xxxfff.com')); 

define('ADMIN_COOKIE_PATH', '/');
define('COOKIEPATH', '/');
define('SITECOOKIEPATH', '/');

Front end script 前端脚本

require_once $_SERVER['DOCUMENT_ROOT'] . 'blog/wp-load.php'; 
global $current_user;
$current_user = wp_get_current_user();
//var_dump($current_user->ID);
//var_dump($current_user->display_name);
//var_dump($_COOKIE);


if ( !function_exists( 'is_user_logged_in' ) ) { 
    require_once    $_SERVER['DOCUMENT_ROOT'] . 'blog/wp-includes/pluggable.php'; 
} 

    $current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
    // Not logged in.
    echo 'not logged in';
} else {
    // Logged in.
    echo 'logged in';
}       

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

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