how can i create a subdomain using php?

Thanks in advance

You can't, Sub-Domains are managed by the web server. Speak to your host for information on how to set one up.

I'm a beginner,
I mean is there any way to create a subdomains like some free hosting sites do?
Is there any other way to make subdomain creation upon user request.
I used create sub domains from my control panel.
i don't know what actually CGI is, is it possible with that? or ruby or perl?

Forgive me if my question is a blunder :).
Thanks a lot for your reply.

unix-like or windows server?

unix-like server

The only way that I know how to make subdomains (other than using the servers control panel) is by editing the apache httpd.conf file which you may be able to program a php script to do if you have full permissions of the server. Guides can be found at So if you are the server administrator then just program a php script to edit the httpd.conf file as instructed in the above and the apache codes would look something like:


   DocumentRoot "C:\My Sites\Site1"

   DocumentRoot "C:\My Sites\Site2"

And obviously replace in the code above the term "" with your registered domain and replace the word "subdomain" with the name of your subdomain. But that is a sketch of what I think the apachie code needs to look like but would wait for further comments on the above code before copying it.

Also in the link I provided, you will need to ignore the information below the title: "Resolving the DNS issue" as you have a domain in the first place instead of localhost.

So basically, the only thing that php can really do is edit the apache httpd.conf file.

Thanks a lot,
can you please tell me where to place the httpd.conf
i'll have to place http.conf with contents as above in my root folder?

i'm attaching directory structure of my domain.


You need server root access in order to manually modify the httpd.conf file. Very few hosting companies will give a site owner root access. Using cPanel to add a subdomain does the same thing.

Unless you are very familiar with Linux/Unix servers, you should not be doing anything at the server root level.

I think what you really want to do is to set up a widcard DNS that allows you to artificially create sub domains. I have used this article to do that. The article also shows you how to manually edit the httpd.conf file.

Wildcard DNS

Another option would be to create subdirectories for your users with PHP. That is easier to do.

Thanks for your reply, I don't have the login details for my DNS ("Edit DNS Zone") configuration
I only have username and password for my cpanel. Then it seems to be a difficlut job for me. Also i didn't get you completely (wildcard DNS).

also I think I have to switch to the second methid you've suggested.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.