2012-12-03 59 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,它工作的很好,并帮助我保持代码简单和易于更新!