![](/img/trans.png)
[英]PHP LOGIN script Working on Xampp localhost but not on Cpanel
[英]Script for secure cPanel login with PHP
從最近開始,cPanel更改了登錄方式。
登錄前,URL為: https:// accessurl:2083 /
登錄后: https:// accessurl:2083 / cpsessXXXX / frontend / x3 / index.html?post_login = 89711792346495
您會注意到url中嵌入了cpsessXXXX 。
訪問AWSTATS的頁面是: https:// accessurl:2083 / cpsessXXXX / awstats.pl?config = 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);
當我單步執行代碼時, $ store的值為FALSE,這意味着登錄過程失敗。
我在網上找到的關於類似問題的唯一參考是3月28日條目中的http://blog.mcfang.com/ 。
我希望cookies.txt包含cpsessXXXX信息,但是沒有創建文件。
謝謝你的幫助
您需要將安全性令牌引用回cPanel才能被腳本接受。 根據cPanel文檔中有關安全性令牌的文檔
請看以下示例:
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期望返回的正確令牌。
您可以通過添加以下簡單行的標頭發送“授權”行:
$header[0] = "Authorization: Basic " . base64_encode($cp_user.":".$cp_pwd) . "\n\r";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
因此,您可以直接向資源$ myResource發送請求,而無需Cookie和其他任何內容。 只是:
$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);
您應該考慮使用CPanel的XML API 。 在Github上,您可以找到xmlapi-php類 ,它可以很好地工作,並且可以幫助我保持代碼的簡單易行!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.