benjamin benjamin - 3 years ago 208
PHP Question

Dynamically assign a subdomain that is not related to a folder

I need to create a sub-domain dynamically. What my code does is load html code from DB then displays it.

There are no sub folders or anything like that. I want a url that looks like this

XXXXXX.com/loadfile.php?website=test
to look like
test.XXXXXX.com
.

Answer Source

Something as basic as this should do the trick:

<?php
// assuming you have $_SERVER['SERVER_NAME'] reliably set and available
// i.e. something like $_SERVER['SERVER_NAME'] = 'XXXXXX.com';

$subdomain = $_GET['website'] . '.' . $_SERVER['SERVER_NAME'];

You might want to validate the input etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download