2009-10-18 80 views
1

我通過CURL驗證登錄是否正常。我有一個變量用於顯示返回的HTML,並且它正在返回我的用戶控制面板,就像我已經登錄一樣。CURL身份驗證正在丟失?

驗證通過後,我想與網站中另一頁上的窗體進行通信變量;但由於某種原因,該頁面的HTML正在返回未經驗證的頭部版本(就像原始驗證從未發生過一樣)。

我有一個包含777權限的cookies.txt文件,並嘗試過當我進行身份驗證時顯示的同一頁面的內容,就好像我正在丟失任何關聯的會話/ cookie數據。

這裏是我的curl.class文件 -

<? 

class Curl { 

    public $cookieJar = ""; 

    // Make sure the cookies.txt file is read/write permissions 
    public function __construct($cookieJarFile = 'cookies.txt') { 
     $this->cookieJar = $cookieJarFile; 
    } 

    function setup() { 
     $header = array(); 
     $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
     $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
     $header[] = "Cache-Control: max-age=0"; 
     $header[] = "Connection: keep-alive"; 
     $header[] = "Keep-Alive: 300"; 
     $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
     $header[] = "Accept-Language: en-us,en;q=0.5"; 
     $header[] = "Pragma: "; // browsers keep this blank. 

     curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7'); 
     curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header); 
     curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieJar); 
     curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieJar); 
     curl_setopt($this->curl, CURLOPT_AUTOREFERER, true); 
     curl_setopt($this->curl, CURLOPT_COOKIESESSION, true); 
     curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); 
     curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); 
    } 

    function get($url) { 
     $this->curl = curl_init($url); 
     $this->setup(); 

     return $this->request(); 
    } 

    function getAll($reg, $str) { 
     preg_match_all($reg, $str, $matches); 
     return $matches[1]; 
    } 

    function postForm($url, $fields, $referer = '') { 
     $this->curl = curl_init($url); 
     $this->setup(); 
     curl_setopt($this->curl, CURLOPT_URL, $url); 
     curl_setopt($this->curl, CURLOPT_POST, 1); 
     curl_setopt($this->curl, CURLOPT_REFERER, $referer); 
     curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields); 
     return $this->request(); 
    } 

    function getInfo($info) { 
     $info = ($info == 'lasturl') ? curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL) : curl_getinfo($this->curl, $info); 
     return $info; 
    } 

    function request() { 
     return curl_exec($this->curl); 
    } 
} 
?> 

這裏是我的curl.php文件 -

<? 
include('curl.class.php'); // This path would change to where you store the file 
$curl = new Curl(); 

$url = "http://www.site.com/public/member/signin"; 
$fields = "MAX_FILE_SIZE=50000000&dado_form_3=1&member[email]=email&member[password]=pass&x=16&y=5&member[persistent]=true"; 

// Calling URL 
$referer = "http://www.site.com/public/member/signin"; 

$html = $curl->postForm($url, $fields, $referer); 

echo($html); 
?> 
<hr style="clear:both;"/> 
<? 

$html = $curl->postForm('http://www.site.com/index.php','nid=443&sid=733005&tab=post&eval=yes&ad=&MAX_FILE_SIZE=10000000&ip=63.225.235.30','http://www.site.com/public/member/signin'); 

echo $html; // This will show you the HTML of the current page you and logged into 
?> 

任何想法?

回答

0

錯誤,請告訴我們服務器正在使用的認證方案。並非所有計劃都使用Cookie。

1

與往常一樣,在執行HTTP腳本編寫時,應該先使用LiveHTTPHeaders或類似的方法記錄手動會話,然後在編寫curl內容時儘可能地模仿它。另外(不幸的是)命令行工具curl提供比PHP綁定更好的調試和跟蹤選項,這使得它成爲一個更好的工具,可以準確計算出您需要做的事情,一旦您將其轉換爲一個PHP程序。

有關詳細信息,請參見http://curl.haxx.se/docs/httpscripting.html

+0

適合初學者的絕佳資源 – codingbiz