Help - Search - Members - Calendar
Full Version: [PHP] Write to new line of apache conf file
Pixel2Life Forum > Help Section > PHP, ASP, MySQL, JavaScript and other Web/Database Programming Help
timdav83
I have searched google for a few hours regarding this issue. I'm using WAMP which uses php 5.3.0 under the command line, since I'm modifying apache via script/php cli, I'm stuck with this version.

So far, everthing I have researched has not worked under windows xp or this version of php.

nr2bl()
\n
\n\r

or
/n
/n/r

Using these special characters appears in the conf file which would ultimately break apache.

Also note the title, I'm not formatting for web. So <pre> tags and storing this in a database are out of the question.

fwrite breaks under php 5.3.0. Can't close the file. I keep getting a resource error. Apprently more research has concluded this to be a bug under this version sad.gif

CODE
// ip based virtual host
$conf  = '<VirtualHost '.$ip.'>';
$conf  .= '    ServerAdmin '.$sadmin;
$conf  .= '    DocumentRoot '.$install_dir . $NewVhostDomain.'/www/';
$conf  .= '    ServerName '.$NewVhostDomain;
$conf  .= '    ErrorLog '.$install_dir . $NewVhostDomain.'/errors/';
$conf  .= '    TransferLog '.$install_dir . $NewVhostDomain.'/logs/';
$conf  .= '</VirtualHost>';

$conf_file = $install_dir .'/vhostconf/'.$NewVhostDomain.'.conf';

file_put_contents($conf_file, $conf);


Everything works, it just appears in one line.

Conf Input:
CODE
<VirtualHost >    ServerAdmin email@gmail.com    DocumentRoot c:/wamp/domain.net/www/    ServerName tim1.net    ErrorLog c:/wamp/domain.net/errors/    TransferLog c:/wamp/domain.net/logs/</VirtualHost>
NDBoost
Your telling me something like this wont work? If its not working its an issue with your php.ini config...

Seeing as \n is parsed in PHP as a new line item.. it would "fwrite" the file contents, then would parse the \n as a new line. and begin writting the next conf to the next line.


CODE
$conf  .= '    ServerAdmin '.$sadmin.' \n';


Have you tried using an associative array and writing the contents of the array to the file vs using .=?

IE:
CODE
<?php
$data =  array ('1','2','3','4');

$filename = 'data.txt';
$string   = '';

foreach($data as $key => $val) {
    $string .= "$key = $val\n";
}

file_put_contents($filename, $string);
?>
rc69
1. There are no new lines in the $conf variable as you have it written there.
2. If you were to add "\n" to them, you need to make sure to not use single quotes.

Try:
CODE
$conf  = '<VirtualHost '.$ip.'>'."\n";
$conf  .= '    ServerAdmin '.$sadmin."\n";
$conf  .= '    DocumentRoot '.$install_dir . $NewVhostDomain.'/www/'."\n";
$conf  .= '    ServerName '.$NewVhostDomain."\n";
$conf  .= '    ErrorLog '.$install_dir . $NewVhostDomain.'/errors/'."\n";
$conf  .= '    TransferLog '.$install_dir . $NewVhostDomain.'/logs/'."\n";
$conf  .= '</VirtualHost>'."\n";

ref: http://php.net/manual/en/language.types.string.php
timdav83
I have tried both methods suggested. Both output the squarish special characters [] and the data is still on one line. Unfortunately, I can't copy and paste that part of the input.
NDBoost
QUOTE (rc69 @ Sep 12 2009, 12:45 AM) *
2. If you were to add "\n" to them, you need to make sure to not use single quotes.

duhh i should of noticed that..
Neil
Windows = made of fail.

What rc69 gave you should work
rc69
QUOTE (Neil @ Sep 11 2009, 08:44 PM) *
Windows = made of fail.


Try:
CODE
$conf  = '<VirtualHost '.$ip.'>'."\r\n";
$conf  .= '    ServerAdmin '.$sadmin."\r\n";
$conf  .= '    DocumentRoot '.$install_dir . $NewVhostDomain.'/www/'."\r\n";
$conf  .= '    ServerName '.$NewVhostDomain."\r\n";
$conf  .= '    ErrorLog '.$install_dir . $NewVhostDomain.'/errors/'."\r\n";
$conf  .= '    TransferLog '.$install_dir . $NewVhostDomain.'/logs/'."\r\n";
$conf  .= '</VirtualHost>'."\r\n";
timdav83
\r\n shows the characters as well.
rc69
"\r\n" is how new lines are supposed to be done on Windows. If they don't work, then i don't know what else to try.

I guess you could try looking at the following comment on php.net, but i doubt it will help any:
http://php.net/manual/en/function.file-put...tents.php#91793

As an aside: If you open the file in Wordpad instead of Notepad, you should see everything as it's supposed to be (on new lines, without the square characters).
timdav83
I believe what I'll have to do is create wild cards, %ip and use preg_replace to replace those values. Because doing it this way:

CODE
$conf  = '<VirtualHost %ip>
    ServerAdmin %sa
    DocumentRoot %dr
    ServerName %sn
    ErrorLog %el
    TransferLog %tl
</VirtualHost>';


appears to work the way I want it in notepad.

Edit *

This works:

CODE
// ip based virtual host
$conf  = '<VirtualHost ip>
    ServerAdmin sa
    DocumentRoot dr
    ServerName sn
    ErrorLog el
    TransferLog tl
</VirtualHost>';

// logs
$error_log = $install_dir . 'vhosts/' . $NewVhostDomain . '/logs/errors/';
$transfer_log = $install_dir . 'vhosts/' . $NewVhostDomain . '/logs/transfers/';

// doc root
$doc_root = $install_dir .'vhosts/' . $NewVhostDomain . '/www/';

// use preg_replace to replace wildcards
$conf = preg_replace('/ip/', $NewVhostIP, $conf);
$conf = preg_replace('/sa/', $sadmin, $conf);
$conf = preg_replace('/dr/', $doc_root, $conf);
$conf = preg_replace('/sn/', $NewVhostDomain, $conf);
$conf = preg_replace('/el/', $error_log, $conf);
$conf = preg_replace('/tl/', $transfer_log, $conf);
rc69
That shouldn't be any different from appending the new-lines like i had it, but if it works, it works. My only suggestions are:

1. Don't use preg_replace() for such simple substitutions, use str_replace().
2. Don't use either:
CODE
$conf  = '<VirtualHost '.$NewVhostIP.'>
    ServerAdmin '.$sadmin.'
    DocumentRoot '.$doc_root.'
    ServerName '.$NewVhostDomain.'
    ErrorLog '.$error_log.'
    TransferLog '.$transfer_log.'
</VirtualHost>';
timdav83
I know, in php 4, I didn't have to replace characters. I remember not having to append a new line either. Perhaps it just has to do with using 5.3.0.
Hayden
Putting it all in one variable was variable was what I was thinking as I was reading this.

I agree with rc69 not to use preg_replace or any other sort of regex or _replace. tongue.gif

CODE
$conf  = '<VirtualHost %s>
    ServerAdmin %s
    DocumentRoot %s
    ServerName %s
    ErrorLog %s
    TransferLog %s
</VirtualHost>';

$install_dir = '/path/to/your/vhosts/%s';

$output = sprintf(
   $conf,
   $ip,
   $server_admin,
   sprintf($install_dir,$doc_root),
   $server_name,
   sprintf($install_dir,$error_log),
   sprintf($install_dir,$txfr_log)
);


then you just echo $output. You could also easily use a foreach() on it and create multiple vhosts at a time.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.