Create Subdomain Dynamically Using PHP

By | April 20, 2015

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………..

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

One thought on “Create Subdomain Dynamically Using PHP

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.