2015-06-19 32 views
-1

我正在開發自動登錄到網站的php curl。但是,我有問題,這個網站需要發送_csrf_token到服務器。 我的代碼如下。首先,該程序被稱爲addLoginData($ users),然後是getToken()和getHTTPContent()。用PHP CURL的CRSF TOKEN不起作用

我不知道爲什麼代碼不起作用。

public function addLoginData($users) 
{ 
    foreach($users as $user) 
    { 

     $login_arr = array(
      /*'commit' => 'Login', 
      'nickname' => $user['username'], 
      'password' => $user['password'], 
      'save_cookie' => '1'*/ 
      '_csrf_token' => $this->getToken(), 
      'action'  => 'login', 
      'commit' => 'Einloggen', 
      'invisibility' => 0, 
      'nickname' => $user['username'], 
      'online_status' => 0, 
      'password' => $user['password'], 
      'referer' => '@homepage_guest', 
      'remember_me' => 1 
     );   
     array_push($this->loginArr, $login_arr); 
    } 
} 
public function getToken() 
{ 
    $content = $this->getHTTPContent($this->loginURL,$this->rootDomain);   
    $token = ''; 
    if(!empty($content)) {   
     $html = str_get_html($content); 
     if($html->find("input[name=_csrf_token]",0)) { 
      foreach($html->find("input[name=_csrf_token]") as $span) {   
       $token = $span->value; 
      } 
     } 
    }  
    return $token; 
} 
protected function getHTTPContent($url, $referer, $cookiePath=null, $postContent=null, $get_info = FALSE, $header = null) 
{ 
    $ch = curl_init(); 
    if($this->command['proxy_type'] != 3 && !empty($this->proxy_ip) && !empty($this->proxy_port) && !empty($this->proxy_type)){ 
     curl_setopt($ch, CURLOPT_PROXY, $this->proxy_ip); 
     curl_setopt($ch, CURLOPT_PROXYPORT, $this->proxy_port); 
     curl_setopt($ch, CURLOPT_PROXYTYPE, $this->proxy_type); 
    } 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_REFERER, $referer); 
    curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
    // curl_setopt($ch, CURLOP T_CAINFO, dirname(__FILE__)."/cacert.pem"); 
    $this->savelog("=>".$ch); 
    print_r($ch); 
    if($header !== null) { 
     curl_setopt($ch,CURLOPT_HTTPHEADER, $header); 
    } 


    if($cookiePath !== null) 
    { 
     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath); 
     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath); 
    } 

    if($postContent !== null || $this->nullPost == 1) 
    { 

     curl_setopt($ch, CURLOPT_POST, 1); 

     if($this->nullPost == 0) { 
      curl_setopt($ch, CURLOPT_POSTFIELDS, (($this->_special_post == 1) ? $postContent : http_build_query($postContent))); 
      $this->_special_post = 0; 
      if($this->_special_post == 1){ 
       echo 'Special Search'; 
      } 
      var_dump($postContent); 
     } 
     $this->nullPost = 0; 
    } 

    $content = curl_exec($ch); 
    $header = curl_getinfo($ch); 

    curl_close($ch); 
    echo '<p>URL : ', $url,'</p>'; 
    echo '<p><textarea style="width:600px; height:400px;">',$content,'</textarea></p>'; 

    if(empty($content)) { 
     $this->savelog('No Response from url : '.$url.'/Proxy : '.$this->proxy_ip.':'.$this->proxy_port); botutil::setNoResponse($this->commandID, TRUE, $this); 
    } else { 
     botutil::setNoResponse($this->commandID, FALSE, $this); 
    } 

    if($get_info === TRUE) { 
     return array(
      'header' => $header, 
      'content' => $content 
     ); 
    } else { 
     return $content; 
    } 
} 
+1

你說的*它不工作意味着地球上的什麼*?任何錯誤?空白頁?任何日誌錯誤?請具體說明。 – D4V1D

+0

你怎麼稱呼這些方法?你能否顯示主代碼? –

+0

這意味着當我傳遞參數'_csrf_token'=> $ this-> getToken(), 'action'=>'login', 'commit'=>'Einloggen', 'invisibility'=> 0, 'nickname'=> $ user ['username'], 'online_status'=> 0, 'password'=> $ user ['password'], 'referer'=>' @homepage_guest', 'remember_me'=> 1 – user41065

回答

0
@Tufan Barış Yıldırım, the login function is below. 
public function login() 
{ 
    $this->userAgent = botutil::getAgentString();  
    $this->currentUser = 0; 
    $username = $this->loginArr[$this->currentUser][$this->usernameField]; 
    $cookiePath = $this->getCookiePath($username); 
    $this->user_name = $username; 

    if(!($this->isLoggedIn($username))) 
    { 
     $this->savelog("This profile: ".$username." does not log in."); 
     // count try to login 
     for($count_login=1; $count_login<=$this->loginRetry; $count_login++) 
     { 
      if($this->command["proxy_type"] == 1){ 

       if($this->tor_new_identity($this->proxy_ip,$this->proxy_control_port,'bot')){ 
        $this->savelog("New Tor Identity request completed."); 
       }else{ 
        $this->savelog("New Tor Identity request failed."); 
       } 

      } 

      $this->savelog("Logging in."); 

      // Log 
      $content = $this->getHTTPContent($this->loginActionURL, $this->rootDomain, $cookiePath, $this->loginArr[$this->currentUser]); 
      if(!empty($content)) { 
       file_put_contents("login/".$username."-".date("YmdHis").".html",$content); 
      } 


      if(empty($content)) 
      { 

       $this->savelog("No response from server."); 
       $this->loginRetry++; 
      } 
      else if(!($this->isLoggedIn($username))) 
      { 

       $this->savelog("Log in failed with profile: ".$username); 
       $this->savelog("Log in failed $count_login times."); 

       if($count_login>($this->loginRetry-1)) 
       { 
        $this->savelog("User ".$username." tried to login ".$count_login." times. This username would be deleted."); 
        DBConnect::execute_q("UPDATE user_profiles SET status='false' WHERE site_id=".$this->siteID." AND username='".$this->loginArr[$this->currentUser]['data']['User'][$this->usernameField]."'"); 
        $this->command['profile_banned'] = TRUE; 
        return false; 
       } 
       else 
       { 
        $sleep_time = 120; // 2 mins 
        $this->_session_id = NULL; 
        $this->savelog("Sleep after log in failed for ". $this->secondToTextTime($sleep_time)); 
        $this->sleep($sleep_time); 
       } 
      } else { 
       botutil::profileCount($this->getSiteID(), $username); 
       return true; 
      } 
     } 
    } 
    else 
    { 
     return true; 
    } 
} 

public function logout() 
{ 
    $username = $this->loginArr[$this->currentUser][$this->usernameField]; 
    $cookiePath = $this->getCookiePath($username); 
    $this->savelog("Logging out."); 
    $content = $this->getHTTPContent($this->logoutURL. time(), $this->rootDomain, $cookiePath); 
    return true; 
}