dynamic sub-domain creation with PHP in a linux based server

只愿长相守 提交于 2019-12-18 09:07:13

问题


I want to create sub-domains using PHP on the fly. Suppose a user registers himself as a name "ABC". Then I want to create a sub-domain named 'ABC.mydomain.com' automatically by PHP. I'm using a linux based server.

Would anyone point me to the right direction?


回答1:


You should be aware that this is easily done using wildcard DNS records. This way:

  • you do not have to register each user to your DNS server.
  • your DNS A-record may contain as few as 1 record: e.g *.mydomain.com -> 12.34.56.78
  • your web server at 12.34.56.78 have to be configured to accept wildcard

In your server-side scripts, you dynamically resolve "abc.mydomain.com" on your controller/routing code by checking if abc is an existing active username, sample code below:

<?php

// Note that I am using SERVER_NAME vs HTTP_HOST, 
//    but for additional safety also check your httpd.conf
list($user, $domain) = split("\.", $_SERVER['SERVER_NAME'], 2);

// check if domain is correct, 
//    or you can leave this part if the web server checks this already
if ($domain === "mydomain.com") {

    // here, you verify $user if existent/active 
    // and reroute or render the page depending on request params 
    // ...

}

?>


来源:https://stackoverflow.com/questions/2395325/dynamic-sub-domain-creation-with-php-in-a-linux-based-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!