How to create subdomains using PHP

In this article am going to explain you how to create subdomain in PHP. Usually you need to login to your website cPanel interface in order to create a sub domain. If you want to create a sub domain dynamically this will be the best option for you. In this post we are going to create subdomain using PHP function.

How to Create Subdomain in PHP

You can view demo and download how to Create Edit in Place or Inline Text Editor:


Setup Basic Details:

First we need to store cPanel login credentials in a variable.

//Set the name for your New Sub Domain
$subDomainname="Your Subdomain Name";

//cPanel Username
$cPanelUserName="Your cPanel Username";

//cPanel Password
$cPanelPassName="Your cPanel Password";

//Main Domain Name
$rootDomainName="Your Main Domain Name";

Next we are going to create a function where we are requesting cPanel to create subdomain. In order to create the subdomain first we need to get access to create subdomain. For this we purpose we will send the cPanel login details to server in our application.

Create Subdomain

//Function to create subdomain
function create_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain) {

	//Generate URL for access the subdomain creation in cPanel through PHP
	$buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain . "&dir=public_html/subdomains/" . $subDomain;

	//Open the socket
	$openSocket = fsockopen('localhost',2082);
	if(!$openSocket) {
		//SHow error
		return "Socket error";

	//Login Details
	$authString = $cPanelUser . ":" . $cPanelPass;

	//Encrypt the Login Details 
	$authPass = base64_encode($authString);

	//Request to Server using GET method
	$buildHeaders  = "GET " . $buildRequest ."";

	$buildHeaders .= "HTTP/1.0";
	//Define Host
	$buildHeaders .= "Host:localhost";

	//Request Authorization
	$buildHeaders .= "Authorization: Basic " . $authPass . "";
	$buildHeaders .= "";

	fputs($openSocket, $buildHeaders);
	while(!feof($openSocket)) {

	//Return the New SUbdomain with full URL
	$newDomain = "http://" . $subDomain . "." . $rootDomain . "/";

	//return with Message
	return "Created subdomain".$newDomain;

Done!!. Our subdomain creator is ready to go now. Next we are going to call this function along with the cPanel credentials and subdomain name which you want to create.

//Call the subdomain creator function
echo create_subdomain($subDomainname,$cPanelUserName,$cPanelPassName,$rootDomainName);

Bottom line:

By using this method you can create subdomain dynamically. If required means update the subdomain name in a MYSQL table for manage the subdomain from program itself. Hope you will like this article.

2 Comments How to create subdomains using PHP

  1. Aslam Mulla

    Thanks for posting a solution for how to create subdomain.

    I had tried to implement but I got an error while creating a subdomain.

    Could you please advice whether I can create subdomain dynammically in shared server?
    Or I have to purchase a serve for doing this job.

    1. Prasad G K

      HI Aslam Mulla, Usually you can’t create the subdomain on a shared hosting. This can only possible in a dedicated hosting. But it would be great you can share the error so that i can suggest you the solution.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>