2012-12-03 58 views
1

最近以來,cPanel的改變,它記錄的方式腳本用於安全的cPanel登錄使用PHP

登錄之前,網址是:https://開頭accessurl:2083/

登錄後:https://開頭accessurl:2083/cpsessXXXX /前端/ X3/index.html的post_login = 89711792346495

你會注意到cpsessXXXX嵌入網址?

並訪問AWSTATS的頁面是:https://開頭accessurl:2083/cpsessXXXX/awstats.pl配置= DOMAIN_NAME & SSL = & LANG = EN

我曾嘗試下面的PHP代碼

$username = 'xxx'; 
$password = 'xxx'; 
$loginUrl = 'https://<accessurl>'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $loginUrl); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password); 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_PORT,2083); 
$store = curl_exec($ch); 
curl_close($ch); 

當我通過代碼,$店的值是假的,這意味着在登錄過程失敗。

我在網上發現的類似問題的唯一參考是 在http://blog.mcfang.com/在3月28日條目。

我希望cookies.txt有cpsessXXXX信息,但沒有創建文件。

感謝所有幫助

回答

4

您需要引用安全令牌回的cPanel爲了讓你的腳本被接受。按照文件上的cPanel documentation for Security tokens

看看下面的例子:

function createSession() { // Example details 
$ip = "127.0.0.1"; 
$cp_user = "username"; 
$cp_pwd = "password"; 
$url = "http://$ip:2082/login"; 
$cookies = "/path/to/storage/for/cookies.txt"; 

// Create new curl handle 
$ch=curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); // Save cookies to 
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$cp_user&pass=$cp_pwd"); 
curl_setopt($ch, CURLOPT_TIMEOUT, 100020); 

// Execute the curl handle and fetch info then close streams. 
$f = curl_exec($ch); 
$h = curl_getinfo($ch); 
curl_close($ch); 

// If we had no issues then try to fetch the cpsess 
if ($f == true and strpos($h['url'],"cpsess")) 
{ 
    // Get the cpsess part of the url 
    $pattern="/.*?(\/cpsess.*?)\/.*?/is"; 
    $preg_res=preg_match($pattern,$h['url'],$cpsess); 
} 

// If we have a session then return it otherwise return empty string 
return (isset($cpsess[1])) ? $cpsess[1] : ""; 
} 

cpsess被用於追加的URL正確令牌的cPanel希望回來。

+0

明智的答案。謝謝。我改變了端口到2083年,改變了登錄細節,它完美運作。 – user643862

+0

這樣做的全部目的是自動檢索AWSTATS。一切工作正常,除了返回的HTML基本上說:「您必須設置AWStats UseFramesWhenCGI參數爲0 才能看到您的報告。」 由於這是一個共享服務器,我無法更改UseFramesWhenCGI = 0的AWSTATS conf文件。 有沒有辦法通過設置這個PHP curl?或者,我怎樣才能使用curl仍然收到一個框架頁面? – user643862

+0

答案是(對我來說:-))只是看看** awstats.pl的相關框架?月= 11年= 2012&配置= $域&LANG = EN &&更新= 1&framename = mainright ** – user643862

1

您可以通過在頭加入這個簡單的行發送了「授權」行:

$header[0] = "Authorization: Basic " . base64_encode($cp_user.":".$cp_pwd) . "\n\r"; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 

這樣你就可以直接發送請求到資源$ myResource,沒有cookies和其他任何東西。 簡單:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $myResource); 
$header[0] = "Authorization: Basic " . base64_encode($cp_user.":".$cp_pwd) . "\n\r"; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_PORT,2083); 
$store = curl_exec($ch); 
curl_close($ch); 

你應該考慮使用XML API of CPanel。在Github上,你可以找到xmlapi-php class,它工作的很好,並幫助我保持代碼簡單和易於更新!