Create subdomain code in php

Download Source Code

The file contains a php functions for creating a subdomain and deleting a sub domain dynamically

Click here to download subdomain creation php code from GIT HUB

Here is the function to create a subdomain in php.

  • First parameter is the subdomain name. Like if you want arisha.example.com then you should provide the word arisha.
  • Second parameter is the user name of your web hosting control panel username.
    This is the login where you manage your sites files, ftp, database, etc,.
  • Third parameter is the password of your control panel.
  • Fourth parameter is the name of your original or root domain. for example if your domain name is example.com then you have to provide this here without the subdomain name.


function create_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain) {

// $buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain;

$buildRequest = "/frontend/x3/subdomain/doadddomain.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";

}

In the above code you can see that i have specified a folder name in public_html where all the subdomains will be created.
. $subDomain . “&dir=public_html/” . so all subdomains will be created inside the folder subdomains.
You can change this name according to yoru preference.

Delete Subdomain Code in php

function delete_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain)
{
$buildRequest = "/frontend/x3/subdomain/dodeldomain.html?domain=" . $subDomain . "_" . $rootDomain;

$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);

$passToShell = "rm -rf /home/" . $cPanelUser . "/public_html/subdomains/" . $subDomain;
system($passToShell);
}

Download Source Code

The file contains a php functions for creating a subdomain and deleting a sub domain dynamically

Click here to download subdomain creation php code from GIT HUB

That’s all. Enjoy.

Advertisements