Solved

Store php code in a variable to write to file

Posted on 2011-09-06
6
300 Views
Last Modified: 2012-05-12
Hi Experts,

I am trying to write an automated setup script so that when user types in details on a form a config file is written to the server.

This is my code:

$handle = fopen($setting['config']['folder'] . $setting['config']['file'], 'w');
	

$input = "<?php
// Generated ".date('F j, Y H:i:s')."

// Database Details

$config['hostname'] = '$db_host';
$config['username'] = '$db_user';
$config['password'] = '$db_pass';
$config['database'] = '$db_name';
$config['table_prefix'] = 'sadmin_';
$config['universal_admin_url'] = '$admin_url';
$config['base_url'] = '$base_ur';
$config['seo_page'] = '$seo_page';
$config['show_errors'] = true;
$config['webmaster_email'] = '$webmaster_email';
$config['webmaster_name'] = '$webmaster_name';
$config['language'] = 'english';
?>
";

fwrite($handle, $input);
fclose($handle);
if (file_exists($setting['config']['folder'] . $setting['config']['file']))
	echo '<h3>Configuration file created!</h3><p>The file <code>'.$setting['config']['folder'] . $setting['config']['file'].'</code> has been created successfully. To continue the installation, please continue the installation. </p><a href="?step=3" class="button" style="float:right;"> <span>Generate Database Tables &rsaquo;</span> </a> ';
else
	echo '<h3>ERROR!</h3><p>Configuration file was not created. Please check the the folder <code>'.$setting['config']['folder'].'</code> is created and the permissions to the  folder are set to <code>777</code>. After checking, click the link button below to regenerate the configuration file again..</p> <a href="?step=1" class="button fail" style="float:left"> <span>&lsaquo; Regenerate Configuration</span> </a>';

Open in new window



But its not working. What I am trying to achieve is the config file shows this inside
<?php

// Generated September 6, 2011 16:00:00

// Database Details

$config['hostname'] = "localhost";
$config['username'] = "jim";
$config['password'] = "password";
$config['database'] = "testdb";
$config['table_prefix'] = "td_";
$config['universal_admin_url'] = "http://www.jimclarke.co.uk/admin";
$config['base_url'] = "http://www.jimclarke.co.uk/";
$config['seo_page'] = "index2.php";
$config['show_errors'] = true;
$config['webmaster_email'] = 'me@example.com';
$config['webmaster_name'] = 'Administrator';
$config['language'] = 'english';
?>

Where am I going wrong?

Thanks
Jim
0
Comment
Question by:dovercomputers
6 Comments
 
LVL 8

Accepted Solution

by:
stalhw earned 500 total points
ID: 36489194
you probably need to escape $config
like \$config
and if you want double quotes you need to escape them too:

$input = "<?php
// Generated ".date('F j, Y H:i:s')."

// Database Details

\$config['hostname'] = \"$db_host\";


But you are not saying what's not working, are you getting error messages? is the file created?
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36489204
What are you getting now?
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 36489217
Please explain "But its not working."

Is it producing an error or undesirable results?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:woepwobin
ID: 36489226
You should escape the dollar signs that do not point to a variable:

...
\$config['hostname'] = '$db_host';
\$config['username'] = '$db_user';
\$config['password'] = '$db_pass';
...

Open in new window

0
 

Author Comment

by:dovercomputers
ID: 36489258
Sorry, it is just producing a parsing error. I thought I put that.

I will try escaping the $'s on each line. I just thought that being enclosed within " " they wiouldn't need escaping.

WIll report back shortly.

jim
0
 

Author Closing Comment

by:dovercomputers
ID: 36489316
Thats what it was, I hadn't escaped the $'s at the beginning of each line.

Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now