SubDomain - How do you create one via PHP?


#1

Hi all

I have a new site hosted with DreamHost and my intention is to give users their own subdomain on my site, when they first sign up. For example, lets say my site is:

www.mysite.com

A user signs up as “fred”. They would now have access to the following subdomain on my site:

fred.mysite.com

At the moment, when I create the directory for the subdomain at the right location via a PHP script… example:

/home/username/fred.mysite.com/

… it doesn’t actually work as a subdomain. If I go to the Control Panel and manually add the subdomain there, it creates the subdomain directory in the same location and works fine. The problem with this though is that I can’t manually do this for every user. I need to automate the process via a PHP script.

So in short, how do I create a subdomain via a PHP script, and have it recognised by DreamHost’s name servers, without having to use the Control Panel?

Thanks in advance.

Regards
stensi


#2

Trying to emulate Panel functions via PHP or anything else is extremely challenging. As of yet, I’ve not heard of anybody have success with that. All the cookie-setting and sessions make it a seemingly impossible (so far) task.

-Scott


#3

Thanks for the response Scott.

It’ll be very unfortunate if it’s not possible as I’ll have to take DreamHost up on their 97 day money back gaurantee… which I’d rather not have to do.

MediaTemple, who I have as another host, allow the creation of subdomains by simply creating the directory in the correct location. It then takes effect as a subdomain within minutes. Very easy process.

Does anyone else have any information on this?


#4

Scott gave you accurate information.

–rlparker


#5

Actually, Forum User Boldamunk has
worked out the hard part, with this example code that uses PHP to add an email forwarding address directly using curl to manipulate the Control Panel
.

I threw a couple of trivial modifications at that code, and have successfully added domains/subdomains via PHP using the same method. This is only a “proof of concept”, but it works (under PHP5) and should give you what you need to code an “automated process” if you wish to do so (absolutely no security considerations were covered with this code, etc.).

Note that this does not use an established API, and could be “borken” by a change in the DH Control Panel operation. Additionally, to use the code you will need to “view source” on the “Add a Domain” page of the Control Panel, or use another tool, to determine the numeric usid code assigned to your named user(s) as listed in the “select box” presented in the panel for the FTP/Run CGI as user.

It also sets the new domain up with the “defalut” DH settings for PHP version, www usagage, fastcgi usage, and mod_security usage, and the “standard” DH home/user/sub.yourdomain.tld path (which is different than the home/user/yourdomain.tld/sub.yourdomain.tld/ path that you seem to want) - you will have to tweak as necessary if you want other options

Any way, here is some working code that will create a domain/subdomain via PHP:

[code]<?
// Set your DH Control Panel user and password here
$USER = “yourWebID”;
$PASS = “yourpassword”;
// Set the usid of the FTP user/run CGI as User here - view the form source from the panel to obtain the
//numberic usid numbers of your named users
$usid = “123456”;
// Note: I am using this script to create an new Domain/subdomain via the panel
$new_subdomain = “whatever.yourdomain.tld”; # new Domain/subdomain

function show_curl_errors ($chan) {

Displays cURL error messages when needed.

if (curl_errno($chan) != 0) {
print “Last cURL error : “.curl_errno($chan).”
\n”;
print “Last cURL error : “.curl_error($chan).”
\n”;
}
}

I’m using two cookie jars, one for before the login, one for after. Not sure if this is neccessary however

$cookie_jar1 = tempnam($_SERVER[‘DOCUMENT_ROOT’]."/admin",‘cookie1’);
$cookie_jar2 = tempnam($_SERVER[‘DOCUMENT_ROOT’]."/admin",‘cookie2’);

standard browser header

$header_array[‘Host’] = “panel.dreamhost.com”;
$header_array[‘Accept’] = “text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,/;q=0.5”;
$header_array[‘Accept-Language’] = “en-us,en;q=0.5”;
$header_array[‘Accept-Encoding’] = “gzip,deflate”;
$header_array[‘Accept-Charset’] = “ISO-8859-1,utf-8;q=0.7,*;q=0.7”;
$header_array[‘Keep-Alive’] = “300”;
$header_array[‘Connection’] = “keep-alive”;
$user_agent = “Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7”;

start session on index/login page

$url = “https://panel.dreamhost.com/”;
$ref_url = “”;

$c = curl_init($url);
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($c, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($c, CURLOPT_USERAGENT,$user_agent);
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar1);
$page = curl_exec($c);
show_curl_errors ($c);
curl_close($c);

Submit login request

$ref_url = $url;
$url = “https://panel.dreamhost.com/index.cgi”;
$param_string = “Nscmd=Nlogin&username=$USER&password=$PASS”;

$c = curl_init($url);
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_REFERER, $ref_url);
curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($c, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($c, CURLOPT_USERAGENT,$user_agent);

curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar1);
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar2);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $param_string);
$page = curl_exec($c);
#show_curl_errors ($c);
curl_close($c);
#print “Login Page\n”;
unlink($cookie_jar1);

Resubmit the index page request - often get a blank response from the login page. This makes sure the session is legit

$ref_url = $url;
$url = “https://panel.dreamhost.com/index.cgi”;

$c = curl_init($url);
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_REFERER, $ref_url);
curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($c, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($c, CURLOPT_USERAGENT,$user_agent);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar2);
curl_setopt($c, CURLOPT_POST, 0);
$page = curl_exec($c);
#show_curl_errors ($c);
curl_close($c);
#print “ReGet Login Page\n”;

ask for the “Add a New Domain/Subdomain” page.

#$ref_url = $url;
$url = “https://panel.dreamhost.com/index.cgi?tree=domain.manage&current_step=Index&next_step=ShowAddhttp&domain=”;

$c = curl_init($url);
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_REFERER, $ref_url);
curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($c, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($c, CURLOPT_USERAGENT,$user_agent);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar2);
curl_setopt($c, CURLOPT_POST, 0);
$page = curl_exec($c);
#show_curl_errors ($c);
curl_close($c);
#print “Add Domain/SubDomain Page\n”;

submit the domain/subdomain page using the variables we have previously set up.

Note: I’m only setting up addresses that are forwarded to an existing address.

$ref_url = $url;
$url = "https://panel.dreamhost.com/index.cgi?";
$param_string = “tree=domain.manage&current_step=ShowAddhttp&next_step=AddHttp&tree=domain.manage&domain_type=cgi&domain=$new_subdomain&php_version=5&mod_security_on=1&usid=$usid&relpath=$new_subdomain&www_redir=0”;

$c = curl_init($url);
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_REFERER, $ref_url);
curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($c, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($c, CURLOPT_USERAGENT,$user_agent);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar2);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $param_string);
$page = curl_exec($c);
#show_curl_errors ($c);
curl_close($c);

print “If the Domain/Subdomain was set up correctly, $new_subdomain will appear in Control Panel”;

unlink($cookie_jar2);

exit;
?>

[/code]–rlparker


#6

Hello,

very thank’s by the code, but I try to use and the script execute without error and don’t create the subdomain. Do you have any idea about this ?

Thanks and regards,
Guilherme Nunes


#7

Old thread, new forum, new links. RLParker was referring to this thread.