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.

Creating Subdomains using PHP

Discussion in 'cPanel Developers' started by Dan Ashbridge, Mar 1, 2017.

Tags:
  1. Dan Ashbridge

    Dan Ashbridge Registered

    Joined:
    Mar 1, 2017
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Shropshire
    cPanel Access Level:
    Root Administrator
    Hi,

    I have created a PHP script that automatically creates a Subdomain in cPanel. My original plan was for the script to run without any input from the user running the script in question to create a Subdomain.

    However, the problem I have is that when the script runs, the user is redirected to a page that says: HTTP error 401 - Invalid Security Token and requires the user to then login using a cPanel Username and Password.

    How can I get round this?

    Here is what I echo out BEFORE being redirected to the cPanel Login page.

    HTTP/1.1 301 Moved Server: cpsrvd Connection: close Content-length: 227 Location: https://REMOVED IP ADDRESS:2083/frontend/paper_lantern/subdomain/doadddomain.html?rootdomain=REMOVED IP ADDRESS&domain=test&dir=public_html/ Content-type: text/html; charset="utf-8" Cache-Control: no-cache, no-store, must-revalidate, private Pragma: no-cache

    I hope that someone can help resolve this issue.

    Thanks.
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    37,204
    Likes Received:
    1,296
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
  3. Dan Ashbridge

    Dan Ashbridge Registered

    Joined:
    Mar 1, 2017
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Shropshire
    cPanel Access Level:
    Root Administrator
    Hello, the PHP is below:
    Code:
    $cpanel_user = 'REMOVED';
    $cpanel_pass = 'REMOVED';
    $cpanel_skin = 'paper_lantern';
    $cpanel_host = 'REMOVED';
    $dir = 'public_html/';
    
    $sock = fsockopen($cpanel_host, 2082);
    
    if(!$sock) {
        print('Socket error');
        exit();
    }
    
    $pass = base64_encode("$cpanel_user:$cpanel_pass");
    $in = "GET /frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$cpanel_host&domain=$subdomain&dir=$dir\r\n";
    $in .= "HTTP/1.0\r\n";
    $in .= "Host:$cpanel_host\r\n";
    $in .= "Authorization: Basic $pass\r\n";
    $in .= "\r\n";
    
    fputs($sock, $in);
    
    while (!feof($sock)) {
        $result .= fgets ($sock, 128);
    }
    
    echo $result;
    
     
    #3 Dan Ashbridge, Mar 1, 2017
    Last edited by a moderator: Mar 1, 2017
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    37,204
    Likes Received:
    1,296
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
Loading...

Share This Page