How to let PHP to create subdomain automatically for each user?

前端 未结 12 1010
暖寄归人
暖寄归人 2020-11-22 13:43

How do I create subdomain like http://user.mywebsite.com ? Do i have to access htaccess somehow? Is it actually simply possible to create it via pure php code or I need to u

12条回答
  •  花落未央
    2020-11-22 14:14

    We setup wildcard DNS like they explained above. So the a record is *.yourname.com

    Then all of the subdomains are actually going to the same place, but PHP treats each subdomain as a different account.

    We use the following code:

    $url=$_SERVER["REQUEST_URI"];
    $account=str_replace(".yourdomain.com","",$url);
    

    This code just sets the $account variable the same as the subdomain. You could then retrieve their files and other information based on their account.

    This probably isn't as efficient as the ways they list above, but if you don't have access to BIND and/or limited .htaccess this method should work (as long as your host will setup the wildcard for you).

    We actually use this method to connect to the customers database for a multi-company e-commerce application, but it may work for you as well.

提交回复
热议问题