I would like to know how do I create subdomains using php? Basically, I want a new user that signs up to my website to have a homepage: username.domain.com as well as sub directories as: username.domain.com/journal Thanks in advance, Ivan
I think you may be talking about a virtual domain in which case you need to create a new DNS entry in the named file as well as a new VirtualHost container in the httpd.conf file.
How do I create subdomains? Everyone has a "www" subdomain (In "www.yourdomain.com", the "www" part is the subdomain). The www subdomain is created by, and serves files from, your /htdocs/www directory on the filesystem. To add more subdomains, just create new subdirectories in the /htdocs directory. For instance, if you had the domain name mychevycars.com and wanted a subdomain for Corvettes, then you would just create the directory /htdocs/corvettes and upload the files in there that you wanted to see at http://corvettes.mychevycars.com Second Level Subdomains To create second level subdomains like www.corvettes.mychevycars.com ("www" is the second level subdomain and "corvettes" is the first level subdomain in this example) then you would do one of the following: 1. If you have made an /htdocs/corvettes directory so that you have http://corvettes.mychevycars.com then you can make http://www.corvettes.mychevycars.com by getting a shell prompt and creating a symlink like so: cd /htdocs ln -s corvettes www.corvettes Now http://www.corvettes.mychevycars.com will lead to the same place as http://corvettes.mychevycars.com and all the files for both of these URLs are uploaded in the /htdocs/corvettes directory. 2. Alternatively, you may want your second level subdomain to lead to a separate area of your website instead of being symlinked together with the first level subdomain. For example you may want http://splitbumper.corvettes.mychevycars.com (where "splitbumper" is the second level subdomain) to lead to a section of your website dedicated to 1963-1965 Corvettes with split bumpers, and have http://corvettes.mychevycars.com just lead to a different section of your site about Corvettes in general. To setup the second level subdomain this way, you would make a separate directory altogether for it, like this: cd /htdocs mkdir splitbumper.corvettes The "splitbumper.corvettes" subdirectory is created right alongside all the other subdomain subdirectories, including the "corvettes" subdirectory, if you made one. Then you upload files related to splitbumpers into the /htdocs/splitbumper.corvettes subdirectory, and view them in a browser at http://splitbumper.corvettes.mychevycars.com User-Contributed Notes add a note 06-Mar-2003 12:27 If you need to delete a subdomain symlink made with the "ln -s ..." rm -f /htdocs/www.corvettes 08-Oct-2003 09:30 Make sure the directory name you create is all lower-case, with no spaces. CAPS or Mixed-Case will not work. 07-Nov-2003 21:09 Careful that you only use letters, numbers and (I think) hyphens in your sub-domains. I spent a whole day debugging and wondering why PHP sessions weren't carrying through page to page. It was because I had an underscore in the subdomain. As soon as I changed the subdomain name not to have an underscore, then PHP sessions worked again and I could login to my application.
To simply create subdomains from cpanel, click cpanel's subdomain icon Note that you need shell access to create the second level subdomains as tccoder said. I think it would be impossible to describe to you in detail how to implement automated creation of subdomains and it work with your implementation. You will need to make a script that will create a folder in /htdocs and then save their files into that subdomain. How exactly you do that is up to you
Hello Would You Please Tell me ======================= I am Using Apache Server In My PC And Its http://localhost How Can I Use http://www.localhost/ To Be Redirected To http://localhost/www/ Editing conf Files or Doing Something Else
Create host port ownername password and request variables and call this function $request = "/frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$domain&domain=$subd"; function subd($host,$port,$ownername,$passw,$request) { $sock = fsockopen('localhost',2082); if(!$sock) { print('Socket error'); exit(); } $authstr = "$ownername:$passw"; $pass = base64_encode($authstr); $in = "GET $request\r\n"; $in .= "HTTP/1.0\r\n"; $in .= "Host:$host\r\n"; $in .= "Authorization: Basic $pass\r\n"; $in .= "\r\n"; fputs($sock, $in); while (!feof($sock)) { $result .= fgets ($sock,128); } fclose( $sock ); return $result; } foreach($doms as $dom) { $lines = explode(';',$dom); if (count($lines) == 2) { // domain and subdomain passed $domain = trim($lines[0]); $subd = trim($lines[1]); } else { // only subdomain passed $domain = getVar('domain', DOMAIN); $subd = trim($lines[0]); } // http://[domainhere]:2082/frontend/x/subdomain/doadddomain.html?domain=[subdomain here]&rootdomain=[domain here] $request = "/frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$domain&domain=$subd"; $result = subd('localhost',2082,$cpaneluser,$cpanelpass,$request); $show = strip_tags($result); echo $show; } Code (markup):
Hi, In case you have a cpanel based account, then you can easily create subdomain using php. Here is the link that shows how to create subdomain using PHP. http://php-tutor.com/creating-subdomains-automatically/ This site has more quick php tutorials to various other whm and cpanel functionalies so you can check them out too... -- Om Technologies The best web development php company
The link above (for subdomain creator script) doesnt work. The new php script is here - zubrag.com/scripts/cpanel-subdomains-creator.php
//Function to create subdomain function create_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain) { //Generate URL for access the subdomain creation in cPanel through PHP $buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain . "&dir=public_html/subdomains/" . $subDomain; //Open the socket $openSocket = fsockopen('localhost',2082); if(!$openSocket) { //SHow error return "Socket error"; exit(); } //Login Details $authString = $cPanelUser . ":" . $cPanelPass; //Encrypt the Login Details $authPass = base64_encode($authString); //Request to Server using GET method $buildHeaders = "GET " . $buildRequest ."\r\n"; //HTTP $buildHeaders .= "HTTP/1.0\r\n"; //Define Host $buildHeaders .= "Host:localhost\r\n"; //Request Authorization $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n"; $buildHeaders .= "\r\n"; //fputs fputs($openSocket, $buildHeaders); while(!feof($openSocket)) { fgets($openSocket,128); } fclose($openSocket); //Return the New SUbdomain with full URL $newDomain = "http://" . $subDomain . "." . $rootDomain . "/"; //return with Message return "Created subdomain".$newDomain; } Call the subdomain creator function echo create_subdomain($subDomainname,$cPanelUserName,$cPanelPassName
Thank god you answered this from Jan 25, 2006.. now the original poster can finish his 8 year old project.