Introduction
Using this code you can create subdomains in the live server using php under cPanel control panel. There are two functions in this code. One is to create subdomains and the other is to delete a subdomain.
A subdomain is nothing but a folder in the root folder or a folder in any other place in your server domain space under the root folder which will be pointed by a url which you give as name.
Create subdomain code in php
Here is the function to create a subdomain in php.
• First parameter is the subdomain name. Like if you want music.sitename.com then you should provide the word music.
• 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.
[php]
<?php
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";
}
?>
[/php]
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/subdomains/" . so all subdomains will be created inside the folder subdomains.
You can change this name according to yoru preference.
Delete Subdomain Code in php
[php]
<?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);
}
?>
[/php]
The above code is for deleting a subdomain. It is just like calling the function for creating the subdomain where you will provide the subdomain name to be deleted and the user name of the control panel and the password of the control panel and then the root domain name.
have a look at this rough example-
[php]
//assume that your document root is like the following
/home/servers/user1/public_html/<br>
and you have created a folder in the name of subfolders in root folder and its path will be this
/home/servers/user1/public_html/subdomains/<br>
now if you have created a subdomain in the name of music then a folder is created in the /subdomains folder which would be like
/home/servers/user1/public_html/subdomains/music<br>
like this if you have created a subdomain in the name of the user then again a folder is created inside /subdomains like
/home/servers/user1/public_html/subdomains/username/ where username is the name of the subdomain a user has given when creating their profile for example.
and then you can access your subdomain like music.sitename.com or username.sitename.com which will automatically point to the respective subfolders.
[/php]
I just want to tell you that I'm beginner to blogs and absolutely savored you're web site. Very likely I’m going to bookmark your blog post . You actually have amazing stories. Cheers for sharing with us your web site.
ReplyDelete