The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

webmail login using cURL

Discussion in 'cPanel Developers' started by larwilliams, Jun 14, 2009.

  1. larwilliams

    larwilliams Active Member

    Joined:
    Apr 14, 2008
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    1
    Hi,

    I am attempting to use the following code to login to webmail using cURL and PHP. It is able to login (I am able to get it to display the logged in content), but it doesn't do the redirect to the webmail page, instead just spitting back the html itself it seems.

    PHP:
    <?php
    error_reporting
    (E_ALL);

    // get user name and password from $_POST
    $cpanelemail $_POST['emailaddress'];
    $cpanelemailpass $_POST['pass'];

    if (
    $cpanelemail != '' || $cpanelemailpass != '') {
    // build cPanel webmail URL
    $post2 = array();
    $post2['user'] = $cpanelemail;
    $post2['pass'] = $cpanelemailpass;
    $post2['goto_uri'] = '/';
    $post['login_theme'] = 'cpanel';

    //print_r($post2);
    $url 'https://xxx.site.com:2096/login/';
    //$cookie="cpanel_cookie.txt";

    $ch curl_init();
    curl_setopt($chCURLOPT_POSTFIELDS$post2);
    curl_setopt($chCURLOPT_COOKIEJAR'Ocookie.txt');
    curl_setopt($chCURLOPT_URL$url);
    curl_setopt($chCURLOPT_HEADER1);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);
    curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
    curl_setopt($chCURLOPT_SSL_VERIFYHOST0);

    //$content = curl_exec($ch);
    curl_exec($ch);
    //curl_close($ch);

    //print $content;
    }
    else {
    // empty variables / no login details provided
    print header("Location: cpanel_login.php?failed=2");
    }

    //exit;
    ?>
     
  2. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
  3. MattDees

    MattDees cPanel Product Owner
    Staff Member

    Joined:
    Apr 29, 2005
    Messages:
    417
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    On your server are you using cookie auth or HTTP auth?

    Most cPanel servers (and by default) use HTTP auth (which is the pop up box that asks for user name and pass) in that case, you can use fairly generic code to login to the webmail page like what's described in the following article Using cURL and libcurl with PHP no need to go to /login/ at all.

    After running this, when printing the output of curl_exec does it show "Login Attempt Failed" at all?
     
  4. larwilliams

    larwilliams Active Member

    Joined:
    Apr 14, 2008
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    1
    You guys are both awesome. I was inspired by David's suggestion of LogMeIn.pm to do something similar in PHP (using my current code, getting the session name and then passing it to a new Location: header).

    The problem was that cURL wasn't handling relative paths properly (completely ignoring them) and was simply returning the logged in page instead.

    Thanks guys!
     
Loading...

Share This Page