使用PHP进行安全cPanel登录的脚本

使用PHP进行安全cPanel登录的脚本,php,curl,cpanel,Php,Curl,Cpanel,最近以来,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=&lan

最近以来,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);
$username='xxx';
$password='xxx';
$loginUrl='https://在3月28日的条目中

我希望cookies.txt会有cpsessXXXX信息,但没有创建任何文件


感谢您的帮助

您需要将安全令牌引用回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] : "";
} 

CPSS用于将cPanel期望返回的正确令牌附加到URL。

您需要将安全令牌引用回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] : "";
} 
CPSS用于将cPanel期望返回的正确标记附加到URL。

您可以通过添加以下简单行的标题发送“授权”行:

$header[0] = "Authorization: Basic " . base64_encode($cp_user.":".$cp_pwd) . "\n\r";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
因此,您可以直接向资源$myResource发送请求,而不需要cookie和其他任何东西。 简单地说:

你应该考虑使用。在Github上,您可以找到,它非常有效,帮助我保持代码简单且易于更新

您可以通过添加以下简单行的标题发送“授权”行:

$header[0] = "Authorization: Basic " . base64_encode($cp_user.":".$cp_pwd) . "\n\r";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
因此,您可以直接向资源$myResource发送请求,而不需要cookie和其他任何东西。 简单地说:


你应该考虑使用。在Github上,您可以找到,它非常有效,帮助我保持代码简单且易于更新

回答得很好。谢谢我将端口更改为2083,更改了登录详细信息,它工作得非常好。这项工作的全部目的是自动检索AWSTATS。除了返回的HTML基本上说“必须将AWStats UseFramesWhenCGI参数设置为0才能查看报告”之外,一切都正常工作。由于这是一个共享服务器,我无法更改UseFramesWhenCGI=0的AWStats conf文件。有没有办法通过PHP curl设置这个?或者,如何使用curl仍然接收框架集页面?答案是(对我来说:-)只需查看相关的框架,即awstats.pl?month=11&year=2012&config=$domain&lang=en&&update=1&framename=mainRights。谢谢我将端口更改为2083,更改了登录详细信息,它工作得非常好。这项工作的全部目的是自动检索AWSTATS。除了返回的HTML基本上说“必须将AWStats UseFramesWhenCGI参数设置为0才能查看报告”之外,一切都正常工作。由于这是一个共享服务器,我无法更改UseFramesWhenCGI=0的AWStats conf文件。有没有办法通过PHP curl设置这个?或者,如何使用curl仍然可以接收框架集页面?答案是(对我来说:-)只需查看相关的框架,即awstats.pl?month=11&year=2012&config=$domain&lang=en&&update=1&framename=mainright