WordPress通过检查来决定您是否登录AUTH_COOKIE 和LOGGED_IN_COOKIE. 正如您所注意到的,这些cookies设置在相同的位置,比如说,A 您的站点所在的域。将相同的Cookie添加到第二个B 域将使您的用户在两个A 和B 域。当然,从域设置cookieA 第二次B 域将是一个巨大的安全漏洞,所以您必须从域发送cookie值A 到域B 并在域中设置这些cookieB.
这就是我们要做的:
读取CookieAUTH_COOKIE 和LOGGED_IN_COOKIE 在域上A发送CookieAUTH_COOKIE 和LOGGED_IN_COOKIE 来自域A 到域B设置CookieAUTH_COOKIE 和LOGGED_IN_COOKIE 在域上B要读取Cookie,我们必须使用两个过滤器set_auth_cookie 和set_logged_in_cookie. 在域上设置CookieB 用户浏览器必须位于站点上B 所以我们需要从域重定向用户A 到域B 具有cookie值。使用重定向GET 参数不是选项,Cookie是安全敏感的,我们必须使用POST 要求为了重定向用户并通过POST发送Cookie数据,我们可以创建简单的html表单,url指向域B. 重定向用户后,我们可以在域上设置cookieB 并将用户返回到域A.
我为我的实现创建了工作代码。
/**
* DOMAIN A PART PLUGIN
*/
class WPSE_287556_Send_Cookies {
/**
* Domain which user have to be redirected
*
* @var array
*/
private $domainB = \'example.com\';
/**
* Array of cookies to send
*
* @var array
*/
private $cookies = array();
/**
* WPSE_287556_Send_Cookies constructor.
*/
public function __construct()
{
/**
* Define plugin related hooks
*/
$this->define_hooks();
}
/**
* Save auth and logged in cookies to array
*/
public function save_cookie( $cookie, $expire, $expiration, $user_id, $scheme, $token ) {
$this->cookies[] = $data = array(
\'cookie\' => $cookie,
\'expire\' => $expire,
\'scheme\' => $scheme,
);
}
/**
* Display redirect post form
*
* We should not redirect user with cookies in get parameters because this is
* no safe. We also can not redirect user with post parameters. We can create
* html post form and submit it with js.
*/
public function display_redirect_form( $redirect_to, $requested_redirect_to, $user ) {
if( is_array( $this->cookies ) && !empty( $this->cookies ) ):
$url = ( is_ssl() ) ? \'https://\' : \'http://\' . $this->domainB . \'/\';
?>
<form action="<?php echo esc_url( $url ); ?>" method="post" style="display: none;" id="post_redirect_form">
<input type="hidden" name="action" value="set_cookies" >
<?php foreach($this->cookies as $index => $cookie): ?>
<input type="hidden" name="cookies[<?php esc_attr_e( $index ); ?>][cookie]" value="<?php esc_attr_e( $cookie[\'cookie\'] ); ?>" >
<input type="hidden" name="cookies[<?php esc_attr_e( $index ); ?>][expire]" value="<?php esc_attr_e( $cookie[\'expire\'] ); ?>" >
<input type="hidden" name="cookies[<?php esc_attr_e( $index ); ?>][scheme]" value="<?php esc_attr_e( $cookie[\'scheme\'] ); ?>" >
<?php endforeach; ?>
<input type="hidden" name="redirect_to" value="<?php esc_attr_e( $redirect_to ); ?>" >
</form>
<script> document.getElementById(\'post_redirect_form\').submit(); </script>
<?php exit; ?>
<?php endif;
return $redirect_to;
}
/**
* Define plugin related hooks
*/
private function define_hooks() {
/**
* Save cookies hook
*/
add_action( \'set_auth_cookie\', array($this, \'save_cookie\'), 10, 6 );
add_action( \'set_logged_in_cookie\', array($this, \'save_cookie\'), 10, 6 );
/**
* Display redirect post form
*
* This filter is used to modify redirect url after login. There is no
* better place to modify page content after user login. Additionally
* we have access to $redirect_to url which we can use later.
*/
add_filter(\'login_redirect\', array( $this, \'display_redirect_form\' ), 10, 3);
}
}
new WPSE_287556_Send_Cookies();
/**
* END OF DOMAIN A PART PLUGIN
*/
/**
* DOMAIN B PART PLUGIN
*/
class WPSE_287556_Set_Cookies {
/**
* WPSE_287556_Set_Cookies constructor.
*/
public function __construct()
{
/**
* Define plugin related hooks
*/
$this->define_hooks();
}
/**
* Set auth and logged in cookies
*/
public function set_cookies() {
// Check if request is "set auth cookie" request
if( $_SERVER[\'REQUEST_METHOD\'] === \'POST\' && isset( $_POST[\'action\'] ) && $_POST[\'action\'] === \'set_cookies\' ) {
$args = array(
\'redirect_to\' => FILTER_SANITIZE_URL,
\'cookies\' => array(
\'filter\' => FILTER_SANITIZE_STRING,
\'flags\' => FILTER_REQUIRE_ARRAY,
),
);
// Read and filter all post params
$post = filter_input_array(INPUT_POST, $args);
$redirect_to = $post[\'redirect_to\'];
$cookies = $post[\'cookies\'];
foreach( $cookies as $cookie_params ){
$scheme = $cookie_params[\'scheme\'];
$cookie = $cookie_params[\'cookie\'];
$expire = (int) $cookie_params[\'expire\'];
// Decide which cookie to set
switch( $scheme ) {
case \'logged_in\':
// Set logged in cookie, most of the code is from wp_set_auth_cookie function
setcookie( LOGGED_IN_COOKIE, $cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
if ( COOKIEPATH != SITECOOKIEPATH )
setcookie(LOGGED_IN_COOKIE, $cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
break;
case \'secure_auth\':
case \'auth\':
// Set auth cookie, most of the code is from wp_set_auth_cookie function
if ( $scheme === \'secure_auth\' ) {
$auth_cookie_name = SECURE_AUTH_COOKIE;
} else {
$auth_cookie_name = AUTH_COOKIE;
}
setcookie($auth_cookie_name, $cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, is_ssl(), true);
setcookie($auth_cookie_name, $cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl(), true);
break;
}
}
// Redirect user to previous site
header( \'Location: \' . esc_url( $redirect_to ) );
exit;
}
}
/**
* Define plugin related hooks
*/
private function define_hooks() {
/**
* Set cookies from request
*/
add_action( \'init\', array($this, \'set_cookies\'));
}
}
new WPSE_287556_Set_Cookies();
/**
* END OF DOMAIN B PART PLUGIN
*/