Search code examples
phpsubdomainregistrationvirtualhostposterous

Generate Subdomain From PHP form


I would like to create a registration page which takes user information and generates a user profile subdomain. Example www.UserName.Domain.com similar to posterous. I don't even know where to start, I'm guessing PHP forms with some server side scripts and something about wildcards... but thats just what i've turned up after several hours of googling...

I just starting off and not even sure of the best way to ask this question so thanks for you help.


Solution

  • First of all, make sure the domain (domain.tld - mydomain.com for example) goes to the folder with your PHP code.

    <VirtualHost *:80>
        DocumentRoot "/my/path/"
        ServerName mydomain.com
        ServerAlias mydomain.com
        <Directory "/my/path/">
            allow from all
            Options +Indexes
        </Directory>
    </VirtualHost>
    

    Then on the PHP part simply use:

    $urlParts = explode('.', $_SERVER['HTTP_HOST']);                                                                    
    print_r($urlParts);
    

    If you use: http://www.user.mydomain.com/ you will get:

    Array
    (
        [0] => www
        [1] => user
        [2] => mydomain
        [3] => com
    )
    

    which $urlParts[1] = your username

    If you use: http://user.mydomain.com/ you will get:

    Array
    (
        [0] => user
        [1] => mydomain
        [2] => com
    )
    

    which $urlParts[0] = your username