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.

automate subdomain add

Discussion in 'General Discussion' started by horrighs, May 17, 2004.

  1. horrighs

    horrighs Well-Known Member

    Joined:
    Mar 8, 2003
    Messages:
    75
    Likes Received:
    0
    Trophy Points:
    6
    Is there any way i can use a php or perl to add Subdomains to my account in cpanel? this is for an enduser so they dont have access to the WHM just cpanel.

    Thanks
     
  2. kris1351

    kris1351 Well-Known Member

    Joined:
    Apr 18, 2003
    Messages:
    963
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Lewisville, Tx
    Yea, click Sub-Domains in cpanel and add the sub-domain.
     
  3. horrighs

    horrighs Well-Known Member

    Joined:
    Mar 8, 2003
    Messages:
    75
    Likes Received:
    0
    Trophy Points:
    6
    no i ment can i do it via php or perl not via the Cpanel itself. A customer want to automate adding new sub-domains.

    Steve
     
  4. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    Try this, it works for me.

    Code:
    <?php
        // This script will add a subdomain and ftp account
        // Just change $login, $pass, and $domain - You shouldn't have to touch
        // anything else (unless you want to change how it looks)
         
        if (isset($create)) {
            $login = "cpanellogin";
            $pass = "cpanelpass";
            $domain = "domainname.com";
            $output = fopen ("http://$login:$pass@[url]www.[/url]$domain:2082/frontend/x/subdomain/doad
                ddomain.html?domain=$subdomain&rootdomain=$domain", "r");
            $output = fopen ("http://$login:$pass@[url]www.[/url]$domain:2082/frontend/x/ftp/doaddftp.h
                tml?login=$subdomain&password=$ftppass&homedir=$subdomain", "r");
            print ("Subdomain Created.<br>\n");
            print ("You can now access it at <a href=\"http://$subdomain.$domain\">[url]http://[/url]$s
                ubdomain.$domain</a><br>\n");
            print ("You also have ftp access. Log in at <a href=\"ftp://ftp.$domain\">[url]ftp://[/url]
                ftp.$domain</a> with the username $subdomain@$domain and the password $ftppass\n")
            ;
        } else {
        ?>
      <html>
      <head>
      <title>Automatic Subdomain Creator</title>
      </head>
      <body bgcolor="#FFFFFF" text="#000000">
      <form method="post" action="<?php print($_SERVER['PHP_SELF']); ?>">
         Subdomain: <input name="subdomain" type="text"><br>
         Password: <input name="ftppass" type="password"><br>
      <input name="create" type=submit value="Create">
      </form>
        <?php
        }
    ?>
    
     
  5. horrighs

    horrighs Well-Known Member

    Joined:
    Mar 8, 2003
    Messages:
    75
    Likes Received:
    0
    Trophy Points:
    6
    great thanks!
     
  6. xMichio

    xMichio Member

    Joined:
    May 4, 2004
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    1
    Any know how to add a subdomain, and then set up a redirection from my own html form?
     
  7. joachim

    joachim Registered

    Joined:
    Jun 10, 2004
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Wonderful... Now, what if the sub-domain already exists? Is there a variable returned that would let me give a friendly error message to the user and ask him/her to try again with a different name?

    And I'm guessing it's possible to use the same approach as the one shown above on other things in the cPanel to automate stuff from a script...?

    Thanks for helping! :)

    -Joachim Dyndale
     
  8. Toph

    Toph Registered

    Joined:
    Feb 1, 2005
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Any answers to the last 2 questions??

    Thanks!
    Toph
     
  9. Toph

    Toph Registered

    Joined:
    Feb 1, 2005
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Well with a little looking around I actually figured out how to add the redirection to it.

    here is the code with the redirection.

    Code:
    <?php
        // This script will add a subdomain and ftp account
        // Just change $login, $pass, and $domain - You shouldn't have to touch
        // anything else (unless you want to change how it looks)
        // Toph: I just added code so you can also add redirection to the subdomain and added some comments so you know whats going on
         
        if (isset($create)) {
    
    //EDIT THIS INFO
            $login = "username";
            $pass = "password";
            $domain = "urldomain";
    
    //DONT EDIT BELOW HERE UNLESS YOU KNOW WHAT YOUR DOING
    
    
    //this code adds the subdomain
            $output = fopen ("http://$login:$pass@www.$domain:2082/frontend/x2/subdomain/doadddomain.html?domain=$subdomain&rootdomain=$domain", "r");
    
    //this code adds the ftp account
            $output = fopen ("http://$login:$pass@www.$domain:2082/frontend/x2/ftp/doaddftp.html?login=$subdomain&password=$ftppass&homedir=$subdomain", "r");
    
    //this code adds the redirection
            $output = fopen ("http://$login:$pass@www.$domain:2082/frontend/x2/subdomain/saveredirect.html?domain=$subdomain_$domain&amp;url=$redirecturl", "r");
    
    //this is just the text that shows what happened in the end
            print ("Subdomain Created.<br>\n");
            print ("You can now access it at <a href=\"http://$subdomain.$domain\">http://$subdomain.$domain</a><br>\n");
            print ("You also have ftp access. Log in at <a href=\"ftp://ftp.$domain\">ftp://ftp.$domain</a> with the username $subdomain@$domain and the password $ftppass\n")
            ;
        } else {
        ?>
    
      <html>
      <head>
      <title>Automatic Subdomain Creator</title>
      </head>
      <body bgcolor="#FFFFFF" text="#000000">
    <!--this is the form that does it all-->
      <form method="post" action="<?php print($_SERVER['PHP_SELF']); ?>">
         Subdomain: <input name="subdomain" type="text"><br>
         Password: <input name="ftppass" type="password"><br>
    	 Redirect: <input name="redirecturl" type"text"><br>
      <input name="create" type=submit value="Create">
      </form>
        <?php
        }
    ?>
    I havent figured out a way to check it the domain exists. but an idea, if you have a database you could store the subdomain in it when you add it. then check it when the form is submitted. i just learned all this stuff myself so if anyone needs any help figuring all this stuff out let me know. pher@nerpher.com AIM skafrk82
     
    #9 Toph, Feb 1, 2005
    Last edited: Feb 1, 2005
  10. davemehta

    davemehta Member

    Joined:
    Jul 13, 2005
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    I used this as my code:

    Code:
    <?php
    
    $login = "myLogin";
    $pass = "myPassword";
    $domain = "myDomain.com";
    $adminSubdomain = "admin";
    $mailSubdomain = "mail";
    
    // Create the new admin and mail subdomains and then set the redirect to ports 2082 and 2095 respectively. Allows users to easily access their cpanel and webmail
    
    ini_set('user_agent','MSIE 4\.0b2;'); // I found some info that usernames and passwords like to be compliant with 1.0 or something so this just sets our useragent to 1.0. I don't think this matters.
    // Add admin and mail subdomain
    $output = fopen ("http://$login:$pass@www.$domain:2082/frontend/x/subdomain/doadddomain.html?domain=$adminSubdomain&rootdomain=$domain", "r");
    fclose($output); // close the file to be sure. I don't think this is really necessary but whatev.
    $output = fopen ("http://$login:$pass@www.$domain:2082/frontend/x/subdomain/doadddomain.html?domain=$mailSubdomain&rootdomain=$domain", "r");
    fclose($output); // close the file to be sure. I don't think this is really necessary but whatev.
    sleep(25); 	// If I don't sleep then the redirects won't be added. I think it's because we need to give the server time
    			// time to make the directories and such - then add the redirect info!
    			
    // Add redirects for both new subdomains
    $output = fopen ("http://$login:$pass@www.$domain:2082/frontend/x/subdomain/saveredirect.html?domain=".$adminSubdomain."_$domain&url=http%3A%2F%2Fwww.$domain:2082", "r");
    fclose($output);
    $output = fopen ("http://$login:$pass@www.$domain:2082/frontend/x/subdomain/saveredirect.html?domain=".$mailSubdomain."_$domain&url=http%3A%2F%2Fwww.$domain:2095", "r");
    fclose($output);
    
    print "subdomains created";
    ?> 
    
    The subdomains get created fine. The redirects are what's giving me the problem. I put the sleep statement in there because it seemed the if the redirects weren't added the first time I ran the script, the second time they were added. So I thought that meant that maybe cpanel wasn't done adding the subdomains before the script was trying to add the redirect info, could I be right about this?

    It still doesn't work all the time though. Sometimes it adds the redirects, other times not!

    Any ideas?
     
  11. davemehta

    davemehta Member

    Joined:
    Jul 13, 2005
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    1. When I execute the 'create domain' line in my php file, does it run to completion and then execute the next statement, or since it is just 'reading' the webpage file - does it just start the command by opening the page but it may take x number of seconds for cpanel to actually complete the account creation?

    I am wondering this because if cpanel is busy creating the account and then it tells it to setup the redirection that could cause a problem because the account may not evenbe created completely yet! This is why I added the sleep statement, but even with a sleep of 30 seconds it still doesn't always create the subdomains and redirect them correctly.

    2. Any ideas why this wouldn't work everytime?
     
  12. fwwebs

    fwwebs Well-Known Member

    Joined:
    Feb 16, 2004
    Messages:
    329
    Likes Received:
    0
    Trophy Points:
    16
    Check the cP Account Creator script, located at http://scripts.freemans-web.com . It's designed to create sub-domains, as well as other functions. I've never encountered a problem with the script skipping a step but it uses a slightly different method for creation.

    The creation script above uses the cPanel output to verify whether an email user exists, you might be able to modify the code.

    Look at the add_email.php file for the code.
     
Loading...

Share This Page