php class to create vhosts on testing server (only!!!!)
Posted by: frosty1 (---.maine.res.rr.com)
Date: November 29, 2007 07:16PM

I got tired of repetitively creating vhosts on my wamp server for each project so i wrote this class. If you use it please do so with adequate caution, but it should be a big timesaver.

<?php
/**
* this class adds a new vhost to a windows / apache build
* it has been tested and works on
* windows xp / wampserver 2.0
*
* this comes with absolutely no guarantee
* you are working with system folders and it is
* !!!!!!!!!!CRITICAL!!!!!!!!!!!!!!!
* that you know what you are doing.
*
* i developed this class for my testing server that is
* NEVER PUT ONLINE
*
* use this class at YOUR OWN RISK
*
* a few things to keep in mind:
* uncomment
* # Include conf/extra/httpd-vhosts.conf
* httpd.conf
*
* HOW TO USE
* $newHost = new DSF_Server_Vhost('host name')
* this will return true if it was able to
* 1.create the directory
* 2.update httpd-vhosts.conf
* 3.update windows hosts
*
* restart your apache server
*
*/
class DSF_Server_Vhost
{
protected $_windowsHosts = "C:\WINDOWS\system32\drivers\etc\hosts";
protected $_apacheHosts = "C:\wamp\bin\apache\apache2.2.6\conf\extra\httpd-vhosts.conf";
protected $_vhost = "
<VirtualHost *:80>
DocumentRoot \"C:\wamp\www\{vhost}\"
ServerName {vhost}
</VirtualHost>";


/**
* this creates the new vhost
* 1. create the directory
* 2. update apache's vhosts file
* 3. update windows hosts file
*
* @param string $vhost, the name of your new vhost
* @return bool
*/
public function __construct($vhost)
{
// 1. make the new dir
if(mkdir('C:/wamp/www/' . $vhost))
{
// 2.update apaches vhosts
$apacheHosts = file_get_contents($this->_apacheHosts);
$newVhost = str_replace("{vhost}", $vhost, $this->_vhost);
if(file_put_contents($this->_apacheHosts, $apacheHosts . $newVhost))
{
// 3. update windows hosts
$windowsHosts = file_get_contents($this->_windowsHosts);
$windowsHosts .= "\n127.0.0.1 \t\t" . $vhost;
if(file_put_contents($this->_windowsHosts, $windowsHosts))
{
return true;
}
}
}
}
}

Options: ReplyQuote
Re: php class to create vhosts on testing server (only!!!!)
Posted by: yfastud (Moderator)
Date: November 29, 2007 07:59PM


Options: ReplyQuote


Sorry, only registered users may post in this forum.