How do you create subdomains using PHP?

How will you create sub domains without manually creating it from Control Panel? Sub Domains must me created by using PHP script for a membership website. Answer with a sample script.

Questions by thunderman

Showing Answers 1 - 7 of 7 Answers

* Create the appropriate web root directory, for example /home/sites/username/web , and any subdirectories you wish 
* Edit httpd.conf -  add a new virtual host section 
* Restart httpd


In a normal Linux server setup, php/Apache does not have the necessary permissions to do all this.

  Was this answer useful?  Yes

dipeshpatel

  • Mar 14th, 2008
 

Hello,

You can crate a virtual domain by using .htaccess file and you need to set some little things in control panel.

by using .htaccess file you can crate like as a under mention example.


http://www.domain.com/?a=user --> http://user.domain.com/

  Was this answer useful?  Yes

use this script to add new subdoamin in your cpanel account

subdomainform.php


<html>
<head>
<title>create a subdomain</title>
</head>
<body>
<form name="form1" method="post" action="addsubdomain.php">
<h1>create Sub Domains</h1>
Sub Domain Name : <input type="text" name="subdomain"><br />
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>


==============================

addsubdomain.php
=============================

<?php

ini_set('display_errors', 1);

$host = "domainname.com"; // your domain name without the www
$port = 2082;
$path = "/frontend/x/subdomain/doadddomain.html?domain=".$_POST['subdomain']."&rootdomain=".$host; //or .dll, etc. for authnet, etc.

// these lines are changed
$cpaneluser = "username";
$cpanelpass = "pass";
$authstr = "$cpaneluser:$cpanelpass";
//****************************

// Setup the Auth String
$pass = base64_encode($authstr);

$fp = fsockopen($host, $port, $errno, $errstr, $timeout = 30);

if(!$fp){
//error tell us
echo "$errstr ($errno)n";

}else{

//send the server request

fputs($fp, "POST $path HTTP/1.1rn");
fputs($fp, "Host: $hostrn");
fputs($fp, "Authorization: Basic $pass rn");
fputs($fp, "Content-type: application/x-www-form-urlencodedrn");
fputs($fp, "Content-length: ".strlen($poststring)."rn");
fputs($fp, "Connection: closernrn");
fputs($fp, $poststring . "rnrn");

//*************************************
// Remove this to stop it from displaying the output fron the CPanel
//*************************************
//loop through the response from the server
/*
while(!feof($fp)) {
echo fgets($fp, 4096);
}

*/
while(!feof($fp)) { fgets($fp, 4096); }
//close fp - we are done with it
fclose($fp);
}


  Was this answer useful?  Yes

  1. In you main httpd.conf, add the following line at the bottom:

    Include /path/to/subdomains.conf

file name subdomains.conf


@mkdir ( "/path/to/subdirectory/".$name );

  1.     $vhost = "
  2.     ServerAdmin yourname@yourserver.com
  3.     DocumentRoot /path/to/subdirectory/".$name."
  4.     ServerName ".$name.".yourserver.com
  5.     ErrorLog /path/to/logs/".$name."_error_log
  6. ";
  7.     $fo = fopen ( "/path/to/subdomains.conf", "a" );
  8.     $fw = fwrite ( $fo, $vhost );
  9.     fclose ( $fo );

  Was this answer useful?  Yes

You do it by url rewiting by using .htaccess file and you need to make some change in anchor tags and form submit buttons and you have to write rule in .htaccess file telling it to behave the following url
http://yiippee.com/index.php?id=demo to the used url in anchor tag http://demo.yiippee.com
Same process for submit button of the form.

dipeshpatel

  • Apr 14th, 2009
 

You can create virtual sub domain via .htaccess file. For this you can get
help with Wordpress MU 2.7.


In wordpress wu 2.7 is supporting this features.


You need any more detail or you have any confusion, please get back to me.


Thanks


  Was this answer useful?  Yes

abc

  • Sep 12th, 2011
 

http://www.domain.com/?a=user --> http://user.domain.com/

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions