2014-02-22 2 views
1

curl을 사용하여 ssl 웹 사이트에 원격 로그인하고 프레임 워크 codeigniter를 사용했습니다. 로그인 할 때 때때로 로그인 성공하지만 언젠가는 세션이 만료되었습니다. 나는이 문제를 해결하는 방법을 모른다.curl 원격 로그인 웹 사이트를 사용할 때 PHP 세션이 만료되었습니다.

이것은 내 코드입니다.

public function index() 
{ 
    if ($this->input->server('REQUEST_METHOD') == 'POST') { 

     $username = $this->input->post('username'); 
     $password = $this->input->post('password'); 
     //username and password of account 

     //set the directory for the cookie using defined document root var 


     //login form action url 
     $url="https://www.voipchief.com/login/"; 
     $cookie_file_path = "/tmp/cookies.txt"; 
     // begin script 
     $ch = curl_init(); 

     // extra headers 
     $headers[] = "Accept: */*"; 
     $headers[] = "Connection: Keep-Alive"; 

     // basic curl options for all requests 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_HEADER, 0); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);   
     curl_setopt($ch, CURLOPT_USERAGENT, 
      "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7"); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 

     // set first URL 
     curl_setopt($ch, CURLOPT_URL, $url); 

     // execute session to get cookies and required form inputs 
     $content = curl_exec($ch); 

     $fields='<form action="https://www.voipchief.com/login" method="post" class="form-detail">'; 
     $fields.= $this->Get_form('<form action="https://www.voipchief.com/login" method="post" class="form-detail">', '<form', $content); 
     $fields.='</form>'; 

     $test = $this->getInputs($fields); 

     $test['login[username]'] = $username; 
     $test['login[password]'] = $password; 
     $test['page_referrer']  = 'login'; 
     $test['login[remember_me]'] =1; 


     // set postfields using what we extracted from the form 
     $POSTFIELDS = http_build_query($test); 

     // change URL to login URL 
     curl_setopt($ch, CURLOPT_URL, $url); 

     // set post options 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS); 

     // perform login 
     $result = curl_exec($ch); 

     print $result; 

    } 
    $this->load->view('welcome_message'); 

} 

function getInputs($form) 
{ 
    $inputs = array(); 

    $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches); 

    if ($elements > 0) { 
     for($i = 0; $i < $elements; $i++) { 
      $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]); 

      if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) { 
       $name = $name[1]; 
       $value = ''; 

       if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) { 
        $value = $value[1]; 
       } 

       $inputs[$name] = $value; 
      } 
     } 
    } 

    return $inputs; 
} 
function Get_form($filter1, $filter2, $html) 
{ 
    $content = ''; 
    $filter = '#(?<=' . $filter1 . ').*(?=' . $filter2 . ')#imsU'; 
    $a = preg_match($filter, $html, $co_non); 
    if($a) $content = $co_non[0]; 
    return $content; 
} 

}

답변

0

첫 번째 요청 후 컬을 닫습니다. 그렇지 않으면 쿠키를 파일에 저장하지 않습니다. 즉, CURLOPT_COOKIEFILE은 빈 파일을 가져옵니다.

curl_close($ch); 

그런 다음 다른 컬 인스턴스를 열어 로그인 호출을 수행하십시오. 이번에는 로그인 요청에 다음을 사용하고 있는지 확인하십시오.

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
+0

T 시도했지만 작동하지 않습니다. –

관련 문제