我正在尝试从单独的web应用程序中为wordpress站点提供一种SSO/编程登录解决方案。我对wordpress没有太多经验,所以我认为我缺少了一些东西或者配置不正确。
首先,这是我的测试代码,当我直接点击它时,它工作得很好www.mywordpresssite.com/test_login.php
test\\u登录。php:
<?php
require __DIR__ . \'/wp-load.php\';
$username = \'myuserid\';
$user = get_user_by(\'login\', $username);
if (!is_wp_error( $user )) {
wp_clear_auth_cookie();
wp_set_current_user($user->ID);
wp_set_auth_cookie($user->ID, TRUE);
$redirect_to = user_admin_url();
wp_safe_redirect( $redirect_to );
}
exit();
上面相应地设置cookie并登录“myuserid”用户没有问题。它重定向到user\\u admin\\u url(),这对我的测试很好,但没有必要,因为我的目的只是转到主页,因为一旦这样做起作用,整个网站将被锁定,以供未登录的用户使用。
现在使用与test\\u登录相同的概念。php我已经创建了prod\\u登录名。php应该通过来自web应用程序的cURL请求应用相同的逻辑,该web应用程序通过身份验证头传递CRED。
prod\\u登录。php:
<?php
require __DIR__ . \'/wp-load.php\';
$response = array(\'http_code\' => 500, \'message\' => \'Failed to update response\');
if ($_SERVER[\'PHP_AUTH_PW\'] !== \'password123\') {
$response[\'http_code\'] = 401;
$response[\'message\'] = \'Unauthorized login attempt\';
echo json_encode($response);
die();
}
$username = $_SERVER[\'PHP_AUTH_USER\'];
$user = get_user_by(\'login\', $username);
// Redirect URL //
if (!is_wp_error( $user )) {
wp_clear_auth_cookie();
wp_set_current_user($user->ID);
wp_set_auth_cookie($user->ID, TRUE);
//$redirect_to = user_admin_url();
//wp_safe_redirect( $redirect_to ); //commented out as unnecessary
$response[\'http_code\'] = 200;
$response[\'message\'] = \'Login Successful\';
$response[\'user\'] = $user;
echo json_encode($response);
} else {
$response[\'http_code\'] = 403;
$response[\'message\'] = \'Invalid User\';
echo json_encode($response);
}
exit();
现在,我的卷曲请求似乎运行良好,我始终得到
http_code === 200 响应,用户对象将在cURL响应中正确返回。然而,在从web应用重定向到wordpress站点后,cookies没有设置,我也没有实际登录。
web app cURL请求:
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, \'myuserid\' . \':\' . \'password123\');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(\'Content-Type:application/json\'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,\'http://mywordpresssite.com/prod_login.php\');
$server_output = curl_exec($ch);
if (curl_errno($ch)) {
// handle failed cURL
}
$decoded_response = json_decode($server_output);
if ($decoded_response->http_code === 200) {
// cURL response indicated successful login
// I can consistently get to this point and it will do the redirect but I will not be logged in nor will the cookies be set
header(\'Location: mywordpresssite.com\'); // go to the wp homepage now that we should be logged in
}
我不知道我做错了什么。我认为这与曲奇被吹走或在卷曲和重定向之间没有保存有关。任何关于如何防止这种情况发生或我所做的错误的见解都将受到赞赏。这甚至可以用卷曲来实现吗?
我还尝试了许多add_filter(‘auth_cookie_expiration’,... 和apply_filters(‘auth_cookie_expiration’,... 那些在互联网上可用的都没有用。