2012-10-02 61 views
0

我有以下PHP腳本應該得到一個頁面的內容,這是一個登錄表單後面。PHP Curl登錄重定向不起作用

它在我的本地主機服務器上工作正常,但它不能在主機上運行(GoFreeServe)。我得到一個空的答覆。 ($ postResult爲空)。

腳本:

$login_url = 'http://senioren.voetbal.nl/clubs_comp/mijn-teams/competitie/173299/OWIOS-9-zaterdag?destination=clubs_comp%2Fmijn-teams%2Fcompetitie%2F173299%2FOWIOS-9-zaterdag'; 
$data = file_get_contents("http://voetbal.nl/login/menu/data"); 
     $parts = explode('name=\"form_build_id\" id=\"',$data,2); 
$post_data = 'name=****&pass=****&op=Inloggen&form_build_id='.substr($parts[1],0,37).'&form_id=login_block_login_form'; 
$ch = curl_init(); 
$agent = $_SERVER["HTTP_USER_AGENT"]; 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_REFERER, $login_url); 
curl_setopt($ch, CURLOPT_URL, $login_url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); 

$postResult = curl_exec($ch); 

curl_getinfo本地主機

[url] => http://senioren.voetbal.nl/clubs_comp/mijn-teams/competitie/173299/OWIOS-9-zaterdag 
    [content_type] => text/html; charset=utf-8 
    [http_code] => 200 
    [header_size] => 557 
    [request_size] => 503 
    [filetime] => -1 
    [ssl_verify_result] => 0 
    [redirect_count] => 0 
    [total_time] => 2.668 
    [namelookup_time] => 0 
    [connect_time] => 0.016 
    [pretransfer_time] => 0.016 
    [size_upload] => 57 
    [size_download] => 173015 
    [speed_download] => 64848 
    [speed_upload] => 21 
    [download_content_length] => 173015 
    [upload_content_length] => 57 
    [starttransfer_time] => 1.014 
    [redirect_time] => 0 
    [certinfo] => Array 
     (
     ) 

    [redirect_url] => 

curl_getinfo主機

Array 
(
    [url] => http://senioren.voetbal.nl/clubs_comp/mijn-teams/competitie/173299/OWIOS-9-zaterdag 
    [content_type] => text/html; charset=utf-8 
    [http_code] => 302 
    [header_size] => 1125 
    [request_size] => 361 
    [filetime] => -1 
    [ssl_verify_result] => 0 
    [redirect_count] => 0 
    [total_time] => 1.001361 
    [namelookup_time] => 0.000638 
    [connect_time] => 0.094666 
    [pretransfer_time] => 0.094712 
    [size_upload] => 0 
    [size_download] => 0 
    [speed_download] => 0 
    [speed_upload] => 0 
    [download_content_length] => 0 
    [upload_content_length] => 0 
    [starttransfer_time] => 1.001199 
    [redirect_time] => 0 
) 

的主機不在safe_mode設置並具有捲曲安裝。此外cookie.txt被chmodded 777 來自主機的PHPINFO可以在這裏找到:http://phpinfo.byethost11.com/index.php

你知道爲什麼我得到的200 HTTP_CODE在我的本地和302主機上的HTTP_CODE?

謝謝!

編輯: 打開錯誤報告(!!!@#$)後的頁面返回以下錯誤:

Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /test2.php on line 39 

回答

1

我解決它首先檢查是否open_basedir設置。 如果是這樣的話:腳本首先執行curl_exec()來設置cookie,然後再次執行它以獲取隱藏頁面。像運算參數和form_build_id是根本不使用:P

代碼:

if (ini_get('open_basedir') == '') { 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 

    $postResult = curl_exec($ch); 
    curl_close($ch); 
} else { 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
    curl_exec($ch); 
    $postResult = curl_exec($ch); 
} 
0

嘗試添加FOLLOWLOCATION選項,以便它遵循它得到重定向:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
+0

謝謝,但我已經試過了。不幸的是,它不工作。 ( – user1255553

+1

奇怪,只是有一天我看到了類似的東西。想知道是什麼導致它:) –

0

form_build_idCURLOPT_REFERER缺少你想送點東西像

name=user&pass=pass&op=Inloggen&form_build_id=form-fc3c69bda9d4a9a4a53e70f836ece383&form_id=login_block_login_form 
+0

謝謝,但這並不工作:(不是很奇怪,它從我的本地主機完美工作嗎?我已更新在開幕帖子中的代碼。 – user1255553