2013-06-20 22 views
0

我的PHP腳本執行下列操作檢查,如果遠程網頁已更改

  1. 它登錄到使用CURL,它的偉大工程,登錄名和密碼的遠程服務器。 echo $result;顯示登錄的頁面內容。
  2. 其次它做的是訪問一個內部頁面(需要一個人登錄),並檢查頁面是否使用CURL進行遠程更改(更新)。

我得到的錯誤是在Part 2 of the code below"Access denied",我覺得餅乾是不是整個會話持續性?問題是我應該在代碼中做什麼改變。

<? 
//Part 1 
$username="username; 
$password="pwd"; 
$url="http://abc.com/home?q=login&destination=filmmaker%2Fhome"; 
$cookie="cookie.txt"; 

$postdata = "name=".$username."&pass=".$password."&form_id=user_login&edit-name=".$username."&remember_me=1"; 



$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
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_COOKIEFILE, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result; 

//Part 2 

$curl = curl_init("http://abc.com/filmmaker/home"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_NOBODY, true); 
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_FILETIME, true); 
$result = curl_exec($curl); 

if ($result === false) { 
    die (curl_error($curl)); 
} 

$timestamp = curl_getinfo($curl, CURLINFO_FILETIME); 
if ($timestamp != -1) { //otherwise unknown 
    echo date("Y-m-d H:i:s", $timestamp); //etc 
} 
curl_close($ch); 


?> 
+0

2部分不使用cookie文件。 – Barmar

+0

@Barmar那麼我如何使用第2部分中的Cookie文件? – user580950

回答

1

第2部分需求:

curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie); 

這將發送已保存在CURLOPT_COOKIEJAR選項餅乾第1部分中

+0

我試過同樣的錯誤,訪問被拒絕 – user580950

+0

我更新了有問題的代碼 – user580950

+0

您是否檢查過'cookie.txt'文件以查看它是否正在寫入?也許有一個權限問題。 – Barmar