Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

Create Subdomain Dynamically Using PHP Script

Hello Friends,

I was working with one project in which I need to create sub domain dynamically using PHP script. So when user signup and pay for service, I need t0 create subdomain dynamically and setup everything on subdomain using PHP Script. I dont want my client to get an email saying that Please create Subdomain and follow below instruction to make sub domain working.

Instead, I write a code to create Subdomain dynamically using PHP Code and script for transfer/copy files and put into subdomain. Here is script to Create Subdomain dynamically using PHP Script. It will create sub domain in your cpanel.

function create_subdomain($sub_domain_name,$cpanel_username,$cpanel_password,$main_domain) {
$build_request = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $main_domain . "&domain=" . $sub_domain_name . "&dir=public_html/" . $sub_domain_name;
$open_socket = fsockopen('localhost',2082);
if(!$open_socket) {
return "Socket Error";
$auth_string = $cpanel_username . ":" . $cpanel_password;
$auth_pass = base64_encode($auth_string);
$build_headers = "GET " . $build_request ."\r\n";
$build_headers .= "HTTP/1.0\r\n";
$build_headers .= "Host:localhost\r\n";
$build_headers .= "Authorization: Basic " . $auth_pass . "\r\n";
$build_headers .= "\r\n";
fputs($open_socket, $build_headers);
while(!feof($open_socket)) {
$sub_domain_url = "http://" . $sub_domain_name . "." . $main_domain . "/";
return $sub_domain_url;

$sub_domain_name => Your Sub Domain Name Only. For e.g. mobile
$cpanel_username => Your Cpanel Username
$cpanel_password => Your Cpanel Password
$main_domain => Main Domain Name. For e.g. www.programmingfacts.com

So at the end, your domain will be http://mobile.programmingfacts.com.

In Next Post, I will write about Delete subdomain Dynamically using PHP Script.

Create Subdomain using PHP Script, Create Subdomain Dynamically, How to create Subdomain PHP Script, Create Subdomain PHP Example, Create Subdomain Hostgator PHP Script, Create Subdomain Hostmosntor PHP Script, Create Subdomain BlueHost PHP Script, Create Subdomain Dynamically PHP Script


No Comments Yet.

Leave a Reply

Your email address will not be published.