Prady
Prady

Reputation: 11300

create subdomain programmatically in PHP

I am on shared hosting and on a add on domain.

I need to create subdomain for each user of my website like if the username is jeff then he should have a url jeff.mydomain.com.

How can I create it programmatically using PHP?

Upvotes: 4

Views: 5824

Answers (2)

Ben Rowe
Ben Rowe

Reputation: 28691

There's two parts to this. Firstly you'll need to setup a wildcard dns entry.

Once you've got that setup you should have all your requests pointed back to a single domain. From there you can then use php to figure out which domain you're currently on:

$domain = $_SERVER['HTTP_HOST'];
$base = 'mydomain.com';
$user = substr($domain, 0, -(strlen($base)+1));// the user part of the domain
if(!empty($user)) {
  $user = sanatiseUser($user);
  require_once $user.'.php';
}

Upvotes: 12

Mitch Dempsey
Mitch Dempsey

Reputation: 39869

You need to set apache to listen for all domains coming into a specific IP.

You then need to setup a wildcard DNS entry to point *.domain.com to that IP.

Then inside your app, use $_SERVER['HTTP_HOST'] to determine which user to load.

Upvotes: 1

Related Questions