【问题标题】:dynamic sub-domain creation with PHP in a linux based server在基于 linux 的服务器中使用 PHP 创建动态子域
【发布时间】:2010-03-07 14:55:49
【问题描述】:

我想使用 PHP 即时创建子域。假设用户将自己注册为名称“ABC”。然后我想通过 PHP 自动创建一个名为 'ABC.mydomain.com' 的子域。我正在使用基于 linux 的服务器。

有人能指点我正确的方向吗?

【问题讨论】:

    标签: php subdomain


    【解决方案1】:

    您应该知道,使用通配符 DNS 记录很容易做到这一点。这样:

    • 您不必将每个用户都注册到您的 DNS 服务器。
    • 您的 DNS A 记录可能只包含 1 条记录:例如 *.mydomain.com -> 12.34.56.78
    • 您在 12.34.56.78 的网络服务器必须配置为接受通配符

    在您的服务器端脚本中,您可以通过检查 abc 是否是现有的活动用户名来动态解析控制器/路由代码上的“abc.mydomain.com”,示例代码如下:

    <?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 
        // ...
    
    }
    
    ?>
    

    【讨论】:

    • split 自 php 5.3 起已被弃用。您可能希望使用 explode 代替列表/拆分组合。 -explode('.', $_SERVER['SERVER_NAME']);
    • 啊,谢谢你的来信。我没有碰巧在 php 中全职编写代码,并且我的 perl 背景很明显使用 split
    猜你喜欢
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2015-06-04
    • 1970-01-01
    • 2020-03-09
    • 2016-04-14
    • 2016-04-12
    相关资源
    最近更新 更多