dongtun3259
2009-04-07 04:08
浏览 53
已采纳

在PHP中使用CURL或fsockopen进行Wordpress自动登录

Client wants to click a link and auto login to Wordpress backend admin section.

I tried using fsockopen, code below. Didn't work.

$post_data['user_login'] = 'admin';
$post_data['user_pass'] = 'password';
$post_data['wp-submit'] = 'Log In';
$post_data['redirect_to'] = 'http://example.com/wp-admin/';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//we also need to add a question mark at the beginning of the string
$post_string = '?' . $post_string;

$data_length = strlen($post_string);

$connection = fsockopen('www.example.com', 80);

fputs($connection, "POST /wp-login.php HTTP/1.1
");
fputs($connection, "Host: www.example.com 
");
fputs($connection, "Content-Type: application/x-www-form-urlencoded
");
fputs($connection, "Content-Length: $data_length
");
fputs($connection, "Connection: close

");
fputs($connection, $post_string);


fclose($connection);

also tried CURL

$ch = curl_init('http://example.com/wp-login.php');

$post_data['user_login'] = 'admin';
$post_data['user_pass'] = 'password';
$post_data['wp-submit'] = 'Log In';
$post_data['redirect_to'] = 'http://example.com/wp-admin/';
//$post_data['testcookie'] = '0';
//$post_data['rememberme'] = 'forever';

foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);




curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_exec ($ch);
curl_close ($ch); 

Anyone have an Idea of how to make this work?

Its a linux OS. Running php5.

I have done this before with javascript just submitting a form with all hidden inputs on page load. Client does not want javascript

图片转代码服务由CSDN问答提供 功能建议

客户想要点击一个链接并自动登录到Wordpress后端管理部分。 < p>我尝试使用fsockopen,代码如下。 没有用。

  $ post_data ['user_login'] ='admin'; 
 $ post_data ['user_pass'] ='password'; 
 $ post_data [  'wp-submit'] ='登录'; 
 $ post_data ['redirect_to'] ='http://example.com/wp-admin/';

//traverse数组并准备发布数据 (key1 = value1)
foreach($ post_data as $ key =&gt; $ value){
 $ post_items [] = $ key。  '='。  $ value; 
} 
 
 //使用implode()创建要发布的最终字符串
 $ post_string = implode('&amp;',$ post_items); 
 
 //我们还需要添加 字符串开头的问号
 $ post_string ='?'  。  $ post_string; 
 
 $ data_length = strlen($ post_string); 
 
 $ connection = fsockopen('www.example.com',80); 
 
fputs($ connection,“POST / wp-login  .php HTTP / 1.1 
 
“); 
fputs($ connection,”Host:www.example.com 
 
“); 
fputs($ connection,”Content-Type:application / x-www-  form-urlencoded 
 
“); 
fputs($ connection,”Content-Length:$ data_length 
 
“); 
fputs($ connection,”Connection:close 
 
 
 
“)  ; 
fputs($ connection,$ post_string); 
 
 
 nclclose($ connection); 
   
 
 

也尝试了CURL

  $ ch = curl_init('http://example.com/wp-login.php'); 
 
 $ post_data ['user_login'] ='admin'; 
 $ post_data ['  user_pass'] ='密码'; 
 $ post_data ['wp-submit'] ='登录'; 
 $ post_data ['redirect_to'] ='http://example.com/wp-admin/';  
 // $ post_data ['testcookie'] ='0'; 
 // $ post_data ['rememberme'] ='forever'; 
 
foreach($ post_data as $ key =&gt; $ value){\  n $ post_items [] = $ key。  '='。  $ value; 
} 
 
 //使用implode()创建要发布的最终字符串
 $ post_string = implode('&amp;',$ post_items); 
 
 
 
 
 ncurl_setopt(  $ ch,CURLOPT_POST,1); 
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ post_string); 
curl_exec($ ch); 
curl_close($ ch);  
   
 
 

任何人都知道如何使这项工作?

它是一个Linux操作系统。 运行php5。

我之前使用javascript只是在页面加载时提交包含所有隐藏输入的表单。 客户端不需要javascript

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzh1988 2009-04-07 11:12
    已采纳

    This worked for me:

    $username="admin";
    $password="admin";
    $url="http://www.yourdomain.com/";
    $cookie="cookie.txt";
    
    $postdata = "log=". $username ."&pwd=". $password ."&wp-submit=Log%20In&redirect_to=". $url ."wp-admin/&testcookie=1";
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url . "wp-login.php");
    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_COOKIEJAR, $cookie);
    curl_setopt ($ch, CURLOPT_REFERER, $url . "wp-admin/");
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt ($ch, CURLOPT_POST, 1);
    $result = curl_exec ($ch);
    curl_close($ch);
    echo $result;
    exit;
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题