我已经看到吨如何做到这一点的帖子,但没有任何我已经尝试似乎对我的情况工作。我期待着它的发展而疯狂。使用POST数据登录wordpress
我想从一个不同的网址登录到一个WordPress的网站A,所以当用户登录到WordPress的网站B他们会自动登录到WordPress的网站A.注意:这些网站是在同一台服务器只是不同的网址。
我已经试过CURL并且已经正常工作(发送和接收数据),但它看起来并不像Cookie被正确存储,并且从未登录过我的网站。我的密码我刚刚摆脱它做安全在这里在这里更详细所以它张贴
是哪个我是从(站点B)
add_filter('wp_authenticate', 'send_login', 100, 3);
function send_login($username, $password) {
// this filter is called on the log in page
// make sure we have a username before we move forward
if (!empty($username)) {
//send login information to other sites
$fields = array('username' => $username , 'password' => $password);
echo "<br /> pwd: ". $fields['password'];
$response = do_post_request('http://www.wordpressSiteA.com/wp-content/plugins/login-api/login.php' , $fields);
echo $response;
exit; // i have this for testing purposes so i dont have to keep logging in and out to test
return $user;
}
return $user;
}
function send_data_to_sister_sites($url , $fields) {
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.urlencode($value).'&'; }
rtrim($fields_string,'&');
$cookie = "cookie.txt";
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURL_COOKIEFILE, '');
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
然后发送卷曲的代码这里是网站A的login.php文件我发送卷曲太登录我也经常遇到这种脚本从站点A这样称它的用户
require_once("../../../wp-blog-header.php");
//check security of this request and check fields are sent properly
if (isset ($_REQUEST['username']) && isset($_REQUEST['password'])) {
$username = $_REQUEST['username'];
$userinfo = get_user_by('login', $username);
if ($userinfo) {
//parse data and decrypt fields
$password = $_REQUEST['password'];
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = false;
//log in user
wp_signon($creds, true);
wp_set_auth_cookie($userinfo->ID);
wp_set_current_user($userinfo->ID);
// global $current_user;
//get_currentuserinfo();
// echo 'name: ' . $current_user->user_login . '<br />';
if (is_wp_error($user))
echo $user->get_error_message();
echo "Success";
} else {
//no user found exit false
echo "no user found";
}
} else { echo "no paramters exist"; }
并能正常工作,用户获取登录。
www.wordpressSiteA.com/wp-content/plugins/login-api/login.php?username=username &密码=密码
但是上卷曲的请求似乎没有任何保存。任何人都可以想出一些方法来帮助我朝正确的方向发展。我很亲密!
谢谢!
您是否可以控制两个域? – 2012-07-14 07:44:44
是的,我愿意。他们都是我的网站。 – Pengume 2012-07-14 08:06:36
我已经厌倦了类似的方法,因为这个答案http://stackoverflow.com/a/1486474/839628但我想我错过了这个工作的难题。我不明白为什么它不应该,因为脚本登录是在我想登录的网站上运行.. – Pengume 2012-07-14 08:10:50