Access Wordpress session from cake PHP site

2019-04-02 16:11发布

This is quite difficulty for me. Please help. I've a website created with cake PHP in root domain (say mysite.com) and a blog in a directory called "blog" (mysite.com/blog). I have a register/login feature in PHP site. So what I want here is, whenever a member login in the main site using the login form, whenever he open the blog (mysite.com/blog), he shoulb be as logged in as the same user logged in the main PHP site. In the same way, whenever a new user sign-up with the registration form in the main PHP site, he should become an user in the wordpress blog.

Thanks.

2条回答
聊天终结者
2楼-- · 2019-04-02 16:48

To login the user automatically for wordpress, check out:

http://codex.wordpress.org/Function_Reference/wp_signon

To create a user take a look at:

http://codex.wordpress.org/Function_Reference/wp_create_user

查看更多
姐就是有狂的资本
3楼-- · 2019-04-02 17:02

In your main site, you have to include WP's functions by using wp_signon

define('WP_USE_THEMES', false); //this disables the theme
require('/blog/wp-blog-header.php'); //includes wordpress functions

And then, you can auto-login a user in WP by using

$credentials = array();
$credentials['user_login'] = 'example';
$credentials['user_password'] = 'plaintextpw';
$credentials['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) ) {
//login failed
echo $user->get_error_message();
}

This should work fine, I hope this helps you.

查看更多
登录 后发表回答