horrighs

Well-Known Member
Mar 8, 2003
75
0
156
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
 

horrighs

Well-Known Member
Mar 8, 2003
75
0
156
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
 

asmithjr

Well-Known Member
Jun 13, 2003
516
8
168
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:[email protected][url]www.[/url]$domain:2082/frontend/x/subdomain/doad
            ddomain.html?domain=$subdomain&rootdomain=$domain", "r");
        $output = fopen ("http://$login:[email protected][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 [email protected]$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
    }
?>
 

joachim

Registered
Jun 10, 2004
2
0
151
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
 

Toph

Registered
Feb 1, 2005
2
0
151
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:[email protected]$domain:2082/frontend/x2/subdomain/doadddomain.html?domain=$subdomain&rootdomain=$domain", "r");

//this code adds the ftp account
        $output = fopen ("http://$login:[email protected]$domain:2082/frontend/x2/ftp/doaddftp.html?login=$subdomain&password=$ftppass&homedir=$subdomain", "r");

//this code adds the redirection
        $output = fopen ("http://$login:[email protected]$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 [email protected]$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. [email protected] AIM skafrk82
 
Last edited:

davemehta

Member
Jul 13, 2005
8
0
151
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:[email protected]$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:[email protected]$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:[email protected]$domain:2082/frontend/x/subdomain/saveredirect.html?domain=".$adminSubdomain."_$domain&url=http%3A%2F%2Fwww.$domain:2082", "r");
fclose($output);
$output = fopen ("http://$login:[email protected]$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?
 

davemehta

Member
Jul 13, 2005
8
0
151
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?
 

fwwebs

Well-Known Member
Feb 16, 2004
328
0
166
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.

I havent figured out a way to check it the domain exists.
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.