1

I would like to know how to set up an automatic & light PHP creating subdomains for each users in my database ? Is that possible ?

Bruce Burge
  • 144
  • 1
  • 15
Marc Delalonde
  • 335
  • 4
  • 17
  • are you sure you need a real subdomain or url like `mydomain.com/user1234`? Look here: http://stackoverflow.com/questions/183928/how-to-let-php-to-create-subdomain-automatically-for-each-user – bitWorking Jun 21 '13 at 14:02
  • If you know how to set up vhosts on Apache then it should be fairly trivial to write scripts to do this for you that can be run using the PHP exec function. – Garry Welding Jun 21 '13 at 14:05
  • @redreggae Yes i need a real subdomain like johndoe.mydomain.com :/ – Marc Delalonde Jun 21 '13 at 14:07
  • 1
    @MarcDelalonde a wildcard dns entry could be a solution: http://stackoverflow.com/questions/586129/create-subdomains-on-the-fly-with-htaccess-php – bitWorking Jun 21 '13 at 14:11

2 Answers2

2
<?php
$domainName = 'example.com';
    $subDomainName =  'demo';
     $subDomain = $subDomainName;
    $cPanelUser = 'cpanel_user_name';
    $cPanelPass = 'cpanel_pass';
    $rootDomain = $domainName;
    
    $buildRequest = "/frontend/paper_lantern/subdomain/doadddomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain . "&dir=public_html/$domainName/$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);
     
    echo $newDomain = "http://" . $subDomain . "." . $rootDomain . "/";
?>
Surendra
  • 21
  • 2
0

ya sure its possible go to http://vikku.info/programming/php/create-subdomain-dynamically-in-php-code-to-create-subdomains-in-server-using-php.htm or http://www.phpclasses.org/browse/file/17134.html

just configure the setting like add subdomain name ,cpanel username,cpanel password,domail name.it will work.

for example

if your sub domain name is test.example.com

create_subdomain("test","CPANEL_USERNAME","CPANEL_PASSWORD","example.com");