如何使用自动登录脚本(PHP)进入特定的cPanel页面

如何使用自动登录脚本(PHP)进入特定的cPanel页面,php,cpanel,whm,autologin,Php,Cpanel,Whm,Autologin,我有一个PHP脚本,可以自动将用户登录到cPanel,一切都很好!但是,我不想登陆cPanel的主页,而是想进入一个关于cPanel的页面,比如电子邮件帐户 以下是工作代码: 使用LogMeIn: class LogMeIn { // The available services with their HTTPS ports private static $servicePorts = array('cpanel' => 2083, 'whm' => 2087, 'webmail'

我有一个PHP脚本,可以自动将用户登录到cPanel,一切都很好!但是,我不想登陆cPanel的主页,而是想进入一个关于cPanel的页面,比如电子邮件帐户

以下是工作代码:

使用LogMeIn:

class LogMeIn {
// The available services with their HTTPS ports
private static $servicePorts = array('cpanel' => 2083, 'whm' => 2087, 'webmail' => 2096);
public static function getLoggedInUrl($user, $pass, $hostname, $service, $goto = '/') {
    // If no valid service has been given, default to cPanel
    $port = isset(self::$servicePorts[$service]) ? self::$servicePorts[$service] : 2083;
    $ch = curl_init();
    $fields = array('user' => $user, 'pass' => $pass, 'goto_uri' => $goto);
    // Sets the POST URL to something like: https://example.com:2083/login/
    curl_setopt($ch, CURLOPT_URL, 'https://' . $hostname . ':' . $port . '/login/');
    curl_setopt($ch, CURLOPT_POST, true);
    // Turn our array of fields into a url encoded query string i.e.: ?user=foo&pass=bar
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
    // RFC 2616 14.10 compliance
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection' => 'close'));
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Execute POST query returning both the response headers and content into $page
    $page = curl_exec($ch);
    curl_close($ch);
    $session = $token = array();
    // Find the session cookie in the page headers
    if(!preg_match('/session=([^\;]+)/', $page, $session)) {
        // This will also fail if the login authentication failed. No need to explicitly check for it.
        return false;
    }
    // Find the cPanel session token in the page content
    if(!preg_match('|<META HTTP-EQUIV="refresh"[^>]+URL=/(cpsess\d+)/|i', $page, $token)) {
        return false;
    }
    // Append the goto_uri to the query string if it's been manually set
    $extra = $goto == '/' ? '' : '&goto_uri=' . urlencode($goto);
    return 'https://' . $hostname . ':' . $port . '/' . $token[1] . '/login/?session=' . $session[1] . $extra;
    }
}
classlogmein{
//可用服务及其HTTPS端口
私有静态$servicePorts=array('cpanel'=>2083,'whm'=>2087,'webmail'=>2096);
公共静态函数getLoggedInUrl($user、$pass、$hostname、$service、$goto='/')){
//如果未提供有效服务,则默认为cPanel
$port=isset(self::$servicePorts[$service])?self::$servicePorts[$service]:2083;
$ch=curl_init();
$fields=array('user'=>$user,'pass'=>$pass,'goto_uri'=>$goto);
//将帖子URL设置为类似以下内容:https://example.com:2083/login/
curl_setopt($ch,CURLOPT_URL,'https://'.$hostname'.:'.$port'./login/');
curl_setopt($ch,CURLOPT_POST,true);
//将字段数组转换为url编码的查询字符串,即:?user=foo&pass=bar
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($fields));
//RFC 2616 14.10合规性
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Connection'=>'close'));
curl_setopt($ch,CURLOPT_头,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//执行POST查询,将响应标题和内容返回到$page中
$page=curl\u exec($ch);
卷曲关闭($ch);
$session=$token=array();
//在页面标题中查找会话cookie
如果(!preg_match('/session=([^\;]+)/',$page,$session)){
//如果登录身份验证失败,此操作也将失败。无需显式检查。
返回false;
}
//在页面内容中查找cPanel会话令牌
如果(!preg_match('|]+URL=/(cpss\d+/| i',$page,$token)){
返回false;
}
//如果已手动设置,则将goto_uri追加到查询字符串
$extra=$goto=='/'?'':'&goto_uri='.urlencode($goto);
返回“https://”。$hostname.:'。$port.'/'。$token[1]./login/?session='。$session[1].$extra;
}
}
URL GitHub:

Fórum cPanel的响应:

使用LogMeIn:

class LogMeIn {
// The available services with their HTTPS ports
private static $servicePorts = array('cpanel' => 2083, 'whm' => 2087, 'webmail' => 2096);
public static function getLoggedInUrl($user, $pass, $hostname, $service, $goto = '/') {
    // If no valid service has been given, default to cPanel
    $port = isset(self::$servicePorts[$service]) ? self::$servicePorts[$service] : 2083;
    $ch = curl_init();
    $fields = array('user' => $user, 'pass' => $pass, 'goto_uri' => $goto);
    // Sets the POST URL to something like: https://example.com:2083/login/
    curl_setopt($ch, CURLOPT_URL, 'https://' . $hostname . ':' . $port . '/login/');
    curl_setopt($ch, CURLOPT_POST, true);
    // Turn our array of fields into a url encoded query string i.e.: ?user=foo&pass=bar
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
    // RFC 2616 14.10 compliance
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection' => 'close'));
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Execute POST query returning both the response headers and content into $page
    $page = curl_exec($ch);
    curl_close($ch);
    $session = $token = array();
    // Find the session cookie in the page headers
    if(!preg_match('/session=([^\;]+)/', $page, $session)) {
        // This will also fail if the login authentication failed. No need to explicitly check for it.
        return false;
    }
    // Find the cPanel session token in the page content
    if(!preg_match('|<META HTTP-EQUIV="refresh"[^>]+URL=/(cpsess\d+)/|i', $page, $token)) {
        return false;
    }
    // Append the goto_uri to the query string if it's been manually set
    $extra = $goto == '/' ? '' : '&goto_uri=' . urlencode($goto);
    return 'https://' . $hostname . ':' . $port . '/' . $token[1] . '/login/?session=' . $session[1] . $extra;
    }
}
classlogmein{
//可用服务及其HTTPS端口
私有静态$servicePorts=array('cpanel'=>2083,'whm'=>2087,'webmail'=>2096);
公共静态函数getLoggedInUrl($user、$pass、$hostname、$service、$goto='/')){
//如果未提供有效服务,则默认为cPanel
$port=isset(self::$servicePorts[$service])?self::$servicePorts[$service]:2083;
$ch=curl_init();
$fields=array('user'=>$user,'pass'=>$pass,'goto_uri'=>$goto);
//将帖子URL设置为类似以下内容:https://example.com:2083/login/
curl_setopt($ch,CURLOPT_URL,'https://'.$hostname'.:'.$port'./login/');
curl_setopt($ch,CURLOPT_POST,true);
//将字段数组转换为url编码的查询字符串,即:?user=foo&pass=bar
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($fields));
//RFC 2616 14.10合规性
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Connection'=>'close'));
curl_setopt($ch,CURLOPT_头,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//执行POST查询,将响应标题和内容返回到$page中
$page=curl\u exec($ch);
卷曲关闭($ch);
$session=$token=array();
//在页面标题中查找会话cookie
如果(!preg_match('/session=([^\;]+)/',$page,$session)){
//如果登录身份验证失败,此操作也将失败。无需显式检查。
返回false;
}
//在页面内容中查找cPanel会话令牌
如果(!preg_match('|]+URL=/(cpss\d+/| i',$page,$token)){
返回false;
}
//如果已手动设置,则将goto_uri追加到查询字符串
$extra=$goto=='/'?'':'&goto_uri='.urlencode($goto);
返回“https://”。$hostname.:'。$port.'/'。$token[1]./login/?session='。$session[1].$extra;
}
}
URL GitHub:


Fórum cPanel回复:

hai。这是我现在搜索的任务,我希望你得到正确的答案,那么你能为我们提供代码吗?海。这是我现在搜索的任务,我希望你得到正确的答案,那么你能为我们提供代码吗?