Menu

Create Subdomain Dynamically Using PHP

PHP By Apr 20, 2015 3 Comments

Hello, today i am going to write this tutorial on how to create subdomain dynamically using PHP. Many times we work on our project and we need this so here i am giving this tutorial to help you in that matter.

What We Need

  1. cPanel
  2. cPanel Theme Name
  3. Subdomain Script
  4. Subdomain Name

Assemble our Things

cPanel – Before starting this tutorial we need some crucial things which is the core part of this tutorial that is cPanel. If you are having a running website and you hosted it than i suppose you have a cPanel also. Stop stop stop We dont want you to open your cPanel we just need its username and password. Get them and wait for the next process.

cPanel Theme Name – This is very important part so please consider it else your query will not work. Please check and confirm the theme name which you are using at cPanel.

Subdomain Script – Thats the real superman for our code and it will help us to survive. Yoooo we are going to write a script about how we will add the subdomain in your hosting service under your given path.

Subdomain Name – Wait wait wait you are finding name here lol.

Subdomain Script

Get your cPanel username and password, and the subdomain name. At 2nd line under function after the “/frontend/ please enter the cPanel theme name here/subdomain………..

<?php
function create_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain) {
   $subdomainRequest = "/frontend/paper_lantern/subdomain/doadddomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain . "&dir=public_html/" . $subDomain;
 
    $openSocket = fsockopen('localhost',2082);
    if(!$openSocket) { return "Socket error";exit();}
 
    $authString = $cPanelUser . ":" . $cPanelPass;
    $authPass = base64_encode($authString);
    
    $buildHeaders  = "GET " . $subdomainRequest."\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";
}
echo create_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain);

In the above code we have created a function for Login to cPanel than create a subdomain in the cPanel. Let me elaborate it.

Before testing this script please upload this script in your cPanel else it will not work if you run it at your localhost. Our create_subdomain will take 4 parameters.

  1. Subdomain Name
  2. cPanel Username
  3. cPanel Password
  4. Domain Name

Subdomain name will be our subdomain name which we are going to create under our website. cPanel username and password is for getting the entry into the cPanel. Last one is domain name which will tell us that we have to use this domain for creating the subdomain.

After the getting the above four values. We will create a query to perform. In first line we will create this query which will take all these parameters and allow us to connect to our cPanel on “Localhost on given port”. Here we are using “Localhost” and “2082”. If it wont connect it than it will show the socket error. cPanel took the authentication in base64 so we will change our username and password to base64.

We will create the header and process our query in the header. At last it will close the socket connection and shows the success message.

So here is the end of the tutorial about how to create subdomain dynamically using PHP. Please comment below and let us know if you found it good or worse. Please comment

Author

Hello i am Vivek Moyal PHP developer and writes tutorials for students and beginners. Love to explore new cities

3 Comments

  1. got Created subdomain message but not created

    1. Vivek Moyal says:

      Now the cpanel has changed many tools inside it. So it might wont work.

  2. AbdulBasit says:

    What should i do if i get an socket error

Leave a comment

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