Community Forums
Connect with us on LinkedIn
Community Notice
+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    cPanel Partner NOC cPanel Partner NOC Badge asofrank's Avatar
    Join Date
    Dec 2008
    Posts
    34

    Default Help with WHM Login script (PHP)

    I'm creating a login script for WHM that would allow techs to login without having to enter a root password everytime they access it. I am having a bit of trouble on how I should perform the authentication. I am using the curl method in another script for executing queries against the cPanel API (using the access hash) but that just doesnt seem right for this since its the users computer I want to authenticate, not the script itself.

    Is there a better method for doing this that I am overlooking?

    Heres the code I have so far:

    Code:
    <?php
    
    include('config.php');
    
    if (!isset($_REQUEST['server'])) {
        print "You didn't specify a server.";
        exit(1);
    } else {
        $server = $_REQUEST['server'];
    }
    
    $db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
    mysql_select_db("$dbName", $db) or die ("Couldn't select the database.");
    $sql = "SELECT * FROM servers WHERE srv_name = '$server'";
    $data = mysql_query($sql);
    if (!mysql_num_rows($data)) {
        print "Invalid server name.";
        exit(1);
    } else {
        $r = mysql_fetch_array($data);
        $serverip = $r['srv_ip'];
        $serverhash = $r['srv_hash'];
        // Do Auth stuff here
        $head = "Location: https://$serverip:2087";
        header($head);
    }
    
    ?>
    Last edited by asofrank; 07-27-2010 at 11:30 AM.
    Frank Laszlo
    Independent Developer

  2. #2
    cPanel Partner NOC cPanel Partner NOC Badge asofrank's Avatar
    Join Date
    Dec 2008
    Posts
    34

    Default

    Update:

    I was able to get logged in using the root credentials by passing the information in the headers, but I would like to use the access key if possible.

    Code:
    $username="root";
    $password="password";
    $uphash = base64_encode($username.":".$password);
    header('Authorization: Basic " . $uphash . "\n\r");
    header('Location: https://$servername:2087');
    Frank Laszlo
    Independent Developer

  3. #3
    Integration Developer cPanelDavidN's Avatar
    Join Date
    Dec 2009
    Location
    Houston, TX
    Posts
    496

    Default

    Hi Frank,

    You should be able to just use 'WHM' instead of 'Basic' in your Authorization header

    This is a snippet from the xml-api PHP Client Class (which I HIGHLY recommend you take a look at):

    Code:
    // $this->auth may contain a remote access hash OR a password,
    //  depending on $this->auth_type;
    // $this->user should contain 'root' for 'hash', or any valid user for 'pass'
    // 'hash' will use 'WHM' auth and 'pass' will use 'Basic' auth in headers
    
    if ( $this->auth_type == 'hash' ) {
        $authstr = 'Authorization: WHM ' . $this->user . ':' . $this->auth . "\r\n";
    } elseif ($this->auth_type == 'pass' ) {
        $authstr = 'Authorization: Basic ' . base64_encode($this->user .':'. $this->auth) . "\r\n";
    } else {
        throw new Exception('invalid auth_type set');
    }
    Best Regards,
    -DavidN
    Last edited by cPanelDavidN; 07-28-2010 at 07:10 AM.
    David Neimeyer
    Integration Developer

    sdk.cpanel.net
    APIs: XML-API API1 & API2
    Check Out: Developer Downloads Integration Blog
    Need Support? Support Ticket Developer Forum Feature Request

  4. #4
    cPanel Partner NOC cPanel Partner NOC Badge asofrank's Avatar
    Join Date
    Dec 2008
    Posts
    34

    Default

    Thanks David, I got it working. I was forgetting to pass the username along with the access hash in the header.

    Cheers.
    Frank Laszlo
    Independent Developer

Similar Threads & Tags
Similar threads

  1. Something blocks php script to login to cpanel
    By trecords in forum cPanel and WHM Discussions
    Replies: 1
    Last Post: 04-16-2010, 08:19 AM
  2. Whm/Cpanel Login Script
    By william321 in forum New User Questions
    Replies: 3
    Last Post: 11-26-2009, 05:20 PM
  3. Someone or virus spam WHM login root and script disable us
    By natong in forum cPanel and WHM Discussions
    Replies: 4
    Last Post: 05-01-2009, 05:32 PM
  4. php Script to login on Horde and Neomail
    By Radio_Head in forum cPanel and WHM Discussions
    Replies: 31
    Last Post: 10-30-2005, 12:18 PM
  5. php login script
    By ScottSF in forum cPanel and WHM Discussions
    Replies: 1
    Last Post: 02-20-2004, 09:25 AM
Linkedin       Facebook       Twitter       RSS       Flickr       YouTube