Create subdomain dynamically in server using php and cPanel. PHP code to create subdomains in server

1.Introduction  2.Create Subdomain code 3.Delete Subdomain code  4.Download Source Code  5.Comments

Introduction

Using this code you can create subdomains in the live server using php under cPanel control panel. There are two functions in this code. One is to create subdomains and the other is to delete a subdomain. Consider for example a business is in a need to create users and subdomains for each users. in this case we need to create subdomains dynamically using php when we add a new user. and if we want to delete the user we need to delete his subdomain as well.

You can create a subdirectory and code this function to create subdomains into that directory and not in the root directory of the server. for example all files will reside in public_html folder. in that you will have other folders like /images or /css and so on. if you create subdomains in this root folder then managing other folders will become a mess so you can create a folder under /public_html like /public_html/subdomains and inside that you can create subdomains using this script. Storing subdomain folders anywhere will not affect the way we access subdomains. You can create a subdomain folder anywhere and still you can access your subdomain as subdomain.sitename.com

A subdomain is nothing but a folder in the root folder or a folder in any other place in your server domain space under the root folder which will be pointed by a url which you give as name.

Create subdomain code in php

Here is the function to create a subdomain in php.

function create_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain) {

//	$buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain;

	$buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain . "&dir=public_html/subdomains/" . $subDomain;

	$openSocket = fsockopen('localhost',2082);
	if(!$openSocket) {
		return "Socket error";
		exit();
	}

	$authString = $cPanelUser . ":" . $cPanelPass;
	$authPass = base64_encode($authString);
	$buildHeaders  = "GET " . $buildRequest ."\r\n";
	$buildHeaders .= "HTTP/1.0\r\n";
	$buildHeaders .= "Host:localhost\r\n";
	$buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
	$buildHeaders .= "\r\n";

	fputs($openSocket, $buildHeaders);
	while(!feof($openSocket)) {
	fgets($openSocket,128);
	}
	fclose($openSocket);

	$newDomain = "http://" . $subDomain . "." . $rootDomain . "/";

//	return "Created subdomain $newDomain";

}
In the above code you can see that i have specified a folder name in public_html where all the subdomains will be created. $subDomain . "&dir=public_html/subdomains/". So all subdomains will be created inside the folder subdomains. You can change this name according to yoru preference.

Delete Subdomain Code in php

function delete_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain)
{
	$buildRequest = "/frontend/x3/subdomain/dodeldomain.html?domain=" . $subDomain . "_" . $rootDomain;

	$openSocket = fsockopen('localhost',2082);
	if(!$openSocket) {
		return "Socket error";
		exit();
	}

	$authString = $cPanelUser . ":" . $cPanelPass;
	$authPass = base64_encode($authString);
	$buildHeaders  = "GET " . $buildRequest ."\r\n";
	$buildHeaders .= "HTTP/1.0\r\n";
	$buildHeaders .= "Host:localhost\r\n";
	$buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
	$buildHeaders .= "\r\n";

	fputs($openSocket, $buildHeaders);
	while(!feof($openSocket)) {
	fgets($openSocket,128);
	}
	fclose($openSocket);
	
	$passToShell = "rm -rf /home/" . $cPanelUser . "/public_html/subdomains/" . $subDomain;
	system($passToShell);
}

The above code is for deleting a subdomain. It is just like calling the function for creating the subdomain where you will provide the subdomain name to be deleted and the user name of the control panel and the password of the control panel and then the root domain name.

have a look at this rough example

//assume that your document root is like the following
/home/servers/user1/public_html/
and you have created a folder in the name of subfolders in root folder and its path will be this /home/servers/user1/public_html/subdomains/
now if you have created a subdomain in the name of music then a folder is created in the /subdomains folder which would be like /home/servers/user1/public_html/subdomains/music
like this if you have created a subdomain in the name of the user then again a folder is created inside /subdomains like /home/servers/user1/public_html/subdomains/username/ where username is the name of the subdomain a user has given when creating their profile for example. and then you can access your subdomain like music.sitename.com or username.sitename.com which will automatically point to the respective subfolders.

Download Source Code

The zip file contains a php functions for creating a subdomain and deleting a sub domain dynamically
Click here to download subdomain creation php code

That is all folks. Enjoy.

By -

Comments, Suggestions, Objections, ...