试图从外部登录wordpress

时间:2015-08-21 22:56:18

标签: php wordpress curl

我从安装在子目录中的脚本发送curl帖子到root中安装的wordpress安装:

        $fields_string = 'username='.urlencode($my_username);

        //open connection
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL, $my_url);
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

        //execute post
        $result = curl_exec($ch);

        //close connection
        curl_close($ch);

WP安装中的接收文件包含:

require( dirname( __FILE__ ) . '/../wp-load.php' );

// GET THE USERNAME FROM THE POST
$username = $_POST['username'];

// GET USER INFO. WE NEED THE ID!   
$user = get_user_by( 'login', $username );

// SET THE COOKIE!
wp_set_auth_cookie( $user->ID, false, '' ); 

但它不起作用。我可以在我的日志中看到帖子命中第二个脚本,但是当我在浏览器中打开一个新选项卡并访问博客时它会被注销。有什么想法吗?

谢谢

1 个答案:

答案 0 :(得分:1)

Try this code to login into wordpress...    

 $username='admin';
 $password='dummyPassword';
 $postdata = 'log='. $username .'&pwd='. $password .'&wp-submit=Log%20In&redirect_to='. $adminUrl;
 $ch = curl_init();
 curl_setopt ($ch, CURLOPT_URL,$adminUrl2);
 curl_setopt ($ch, CURLOPT_HEADER, 1);
 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 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_FOLLOWLOCATION, 1);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt ($ch, CURLOPT_REFERER, $adminUrl1);
 curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
 curl_setopt ($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_COOKIESESSION, true);
 $result = curl_exec ($ch);
 $end = strpos($result, 'Content-Type');
 $start = strpos($result, 'Set-Cookie');
 $parts = split('Set-Cookie: ',substr($result, $start, $end-$start));
 $cookies = array();
 foreach ($parts as $co) {
    $cd = split(';',$co);
    if (!empty($cd[0]))
    $cookies[] = $cd[0];
 }

 curl_setopt ($ch, CURLOPT_COOKIE, implode(';',$cookies));
 curl_setopt ($ch, CURLOPT_HEADER, 0);
 $resultadmin = curl_exec ($ch);
 curl_close ($ch);
 $domadmin = new DOMDocument;
 $domadmin->loadHTML($resultadmin);