Get In Touch
Get In Touch

Dynamic cPanel Subdomain Create PHP

The debate over the use of sub domain is an old one and even today divides the SEO community into two warring halves. Yes, sub doamins are not exactly very rosy. They come up with their own hassles both – on the coding front (server side scripting to create subdomains with PHP) & SEO.

But before we argue over whether you should have a sub domain of your website, let’s first go through the SEO Basics of subdomains. You can start by reading this excellent post by Moz on how to handle subdomains, SEO & 301 redirects.

Subdomains & Search Engine Optimization

The Domain Name System (DNS) follows a tree structure or a hierarchy, with each non-RR (resource record) node on the tree being a domain name. A subdomain is a domain that is part of a larger domain. For instance if there is a website named www.suchagenericexample.com , then a sub domain of this website would look something like yeahtotally.suchagenericexample.com.

Generally, a subdomain is preferred over a sub-directory in cases where the content of the sub-domain is not related to the content of the main site. This disparity in the quality as well as context leads to the necessity of ‘separating’ two pages from an SEO perspective.

If a website doesn’t have a sub domain in place for content that differs in relevance as well as quality, then it might lead to a decrease in the quality score of the website due to the muddling of the content.

For instance, let’s say there is a automobile classifieds website, somanycars.com without any sub domains.

When the search engine spider crawls this website, it will go through all the pages of this website including the blog page (somanycars.com/blog). In this instance, the crawler will find vastly different content on both the pages. On most pages, it will find listing of various automobiles for new and used cars.

Whereas, on the blog page, it will find informative content such as a blog post titled “Safety Driving Tips”. Naturally, this will decrease the overall quality score of the website as the search engine crawler scanned two semantically different content.

In such cases, having a sub-domain helps as the search engine bot considers a sub domain as an entirely different website and hence has no effect on the quality score of the website.

Creating a Sub-domain, Dynamically, Via cPanel – Using PHP (Server Side Scripting)

Traditionally, a subdomain in PHP is created manually from the cpanel. To do so, a developer has to enter information such as the name of the subdomain, the domain under which it has to be created.

This process works completely fine if there is only one or a two sub domains to be made. But often, there arises a situation when multiple subdomains are needed to be created dynamically on user actions such as in the case of websites like Tumblr.

For instance, let’s say you have a website www.learningsubdomains.com. Now let’s there is a condition that to use any of your services, a user has to first register by filling up a form.

As soon as the user completes the registration process, a new subdomain is created that looks something like username.learningsubdomains.com. In this case, having to manually create a sub-domain is undesirable if not logistically impossible.

This is where the importance of using programming code to create subdomains surfaces.

In order to create subdomains on the fly, you can always add a snippet of code to your original code that will help you dynamically create subdomains.

Some prerequisites for this cPanel Creation PHP code to work:-

1. Port 2082 must be open for your cPanel installation
2. You must know your cPanel Skin in use. This can be found out by logging into cPanel > Preference > Change Style


// your cPanel username. Replace root with your cPanel username

$cpanel_user = ‘root’;

// your cPanel password. Replace ‘password; with your cPanel password.

$cpanel_pass = ‘password’;

// your cPanel skin. Replace x2 with your cpanel skin in use.

$cpanel_skin = ‘x2’;

// your cPanel domain

$cpanel_host = ‘yourdomain.com’;

// subdomain name that you desire to be created

$subdomain = ‘mysubdomain’;

// Checks that port 2082 is open for your cPanel. This is open by default.

$sock = fsockopen($cpanel_host,2082);
if(!$sock) {
print(‘Socket error’);

// Authenticates username and password for cPanel

$pass = base64_encode(“$cpanel_user:$cpanel_pass”);
$in = “GET /frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$cpanel_host&domain=$subdomainrn”;
$in .= “HTTP/1.0rn”;
$in .= “Host:$cpanel_hostrn”;
$in .= “Authorization: Basic $passrn”;
$in .= “rn”;

// Creates a subdomain after authentication of credentials.

fputs($sock, $in);
while (!feof($sock)) {
$result .= fgets ($sock,128);

echo $result;


Now, if you would like to dynamically handle file operations into this subfolder, which is often why programmers want the subdomain creation to be done dynamically (via PHP) – here is a piece of code that will help you manage files dynamically into your subdomain:-

Create a file inside subdomain:

// cpanel dir path : /home2/domain/public_html

Note:: if you don’t know about your cpanel dir path, you can find it by doing the following:

Step 1. create a index.php file inside subdomin
Step 2. write this code inside index.php

<?php echo getcwd(); ?>

Step 3. Run index.php
Step 4. output will be your cpanel dir path.

Now you can also use the following code to dynamically manage files inside your subdomain:-

$myfile =fopen(“/home2/domain/public_html/mysubdomain/mytextfile.txt”, “w”) or die(“Unable to open file!”);

$txt = “Write a file contents to store a file ……n”;
fwrite($myfile, $txt);



Have a project in mind?
We’d love to help make your ideas into reality.