2011-09-28 109 views
1

我有我自己的網站,它只在會話中存儲登錄信息。 我需要使用CURL來提交表單,但訣竅是您需要先登錄,然後才能使用表單訪問頁面。所以我試圖創建兩個請求,但它似乎沒有按預期工作,即使我登錄頁面上的登錄表單仍然顯示我已註銷。PHP curl身份驗證問題

//add data to be posted 
$post_string1 = "username=test&password=1234"; 
$post_string2 = "value1=555"; 

//create cURL connection 
$curl_connection1 = curl_init("http://".$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']), '/\\')."/login.php"); 
$curl_connection2 = curl_init("http://".$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']), '/\\')."/formpage.php"); 
//create the multiple cURL handle 
//set options 
curl_setopt($curl_connection1, CURLOPT_POST, 1); 
curl_setopt($curl_connection1, CURLOPT_POSTFIELDS, $post_string1); 
curl_setopt ($curl_connection1, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($curl_connection1, CURLOPT_CONNECTTIMEOUT, 5); 
curl_setopt($curl_connection1, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); 
curl_setopt($curl_connection1, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl_connection1, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl_connection1, CURLOPT_FOLLOWLOCATION, 1); 

curl_setopt($curl_connection2, CURLOPT_POST, 1); 
curl_setopt($curl_connection2, CURLOPT_POSTFIELDS, $post_string2); 
curl_setopt ($curl_connection2, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($curl_connection2, CURLOPT_CONNECTTIMEOUT, 5); 
curl_setopt($curl_connection2, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); 
curl_setopt($curl_connection2, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl_connection2, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl_connection2, CURLOPT_FOLLOWLOCATION, 1); 


$mh = curl_multi_init(); 
curl_multi_add_handle($mh,$curl_connection1); 
curl_multi_add_handle($mh,$curl_connection2); 

$active = null; 

do { 
    $mrc = curl_multi_exec($mh, $active); 
} while ($mrc == CURLM_CALL_MULTI_PERFORM); 

while ($active && $mrc == CURLM_OK) { 
    if (curl_multi_select($mh) != -1) { 
     do { 
      $mrc = curl_multi_exec($mh, $active); 
     } while ($mrc == CURLM_CALL_MULTI_PERFORM); 
    } 
} 

//close the handles 
curl_multi_remove_handle($mh, $ch1); 
curl_multi_remove_handle($mh, $ch2); 
curl_multi_close($mh); 

//show information regarding the request 
print_r(curl_getinfo($curl_connection1)); 
echo curl_errno($curl_connection1) . '-' . curl_error($curl_connection1); 
//show information regarding the request 
print_r(curl_getinfo($curl_connection2)); 
echo curl_errno($curl_connection2) . '-' . curl_error($curl_connection2); 

Cookie.txt可寫,包含會話,這裏的響應:

Array 
(
    [url] => http://mysite.com/login.php 
    [content_type] => text/html; charset=UTF-8 
    [http_code] => 200 
    [header_size] => 762 
    [request_size] => 404 
    [filetime] => -1 
    [ssl_verify_result] => 0 
    [redirect_count] => 1 
    [total_time] => 0.270866 
    [namelookup_time] => 2.9E-05 
    [connect_time] => 0.000118 
    [pretransfer_time] => 0.000193 
    [size_upload] => 0 
    [size_download] => 132955 
    [speed_download] => 490851 
    [speed_upload] => 0 
    [download_content_length] => 0 
    [upload_content_length] => 0 
    [starttransfer_time] => 0.250872 
    [redirect_time] => 0.01623 
) 
0-Array 
(
    [url] => http://mysite.com/formpage.php?login=destroyed 
    [content_type] => text/html; charset=UTF-8 
    [http_code] => 200 
    [header_size] => 786 
    [request_size] => 415 
    [filetime] => -1 
    [ssl_verify_result] => 0 
    [redirect_count] => 1 
    [total_time] => 0.056873 
    [namelookup_time] => 2.6E-05 
    [connect_time] => 9.9E-05 
    [pretransfer_time] => 0.000198 
    [size_upload] => 0 
    [size_download] => 9790 
    [speed_download] => 172137 
    [speed_upload] => 0 
    [download_content_length] => 1 
    [upload_content_length] => 0 
    [starttransfer_time] => 0.013818 
    [redirect_time] => 0.042138 

回答

1

不要使用兩條平行的捲曲處理這一點。根據網絡條件,您的SECOND curl請求可能會在登錄請求之前首先到達服務器。

同樣,CURL在請求執行時讀取cookie文件。在從服務器返回任何登錄cookie之前,您的兩個請求都會基本同時啓動。所以這兩個請求都將處於「未登錄」狀態。

使用一個單一的捲曲處理,然後對每個調用順序>

$ch = new CURL(); 
... set up login ... 
... do login ... 
... set options for second post 
... do second post ... 

這樣的順序保證了登錄請求(曲奇)將完成,並提供啓動第二請求之前。