Create subdomains on the fly with .htaccess (PHP)

后端 未结 9 1686
情深已故
情深已故 2020-11-22 02:07

I am looking to create a system which on signup will create a subdomain on my website for the users account area.

e.g. johndoe.website.com

I think it would

9条回答
  •  孤城傲影
    2020-11-22 02:24

    I found it easier doing it with PHP. In fact is creating a subdomain within cPanel and create your folder under the desired domain name. As you will do it manually in cPanel but all it's done in milliseconds by a simple PHP function. No click necessary :)

    function create_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain) {
    
        //  $buildRequest = "/frontend/x3/subdomain/doaffffdomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain;
    
        $buildRequest = "/frontend/x3/subdomain/doaffffdomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain . "&dir=public_html/subdomains/" . $subDomain;
    
        $openSocket = fsockopen('localhost',2082);
        if(!$openSocket) {
            return "Socket error";
            exit();
        }
    
        $authString = $cPanelUser . ":" . $cPanelPass;
        $authPass = base64_encode($authString);
        $buildHeaders  = "GET " . $buildRequest ."\r\n";
        $buildHeaders .= "HTTP/1.0\r\n";
        $buildHeaders .= "Host:localhost\r\n";
        $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
        $buildHeaders .= "\r\n";
    
        fputs($openSocket, $buildHeaders);
            while(!feof($openSocket)) {
               fgets($openSocket,128);
            }
        fclose($openSocket);
    
        $newDomain = "http://" . $subDomain . "." . $rootDomain . "/";
    
       //  return "Created subdomain $newDomain";
    
    }
    
    • See more at: http://vikku.info/programming/php/create-subdomain-dynamically-in-php-code-to-create-subdomains-in-server-using-php.htm#sthash.HV4D2Uig.dpuf

提交回复
热议问题