Solved

Store php code in a variable to write to file

Posted on 2011-09-06
6
299 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
Comment Utility
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 30

Expert Comment

by:Marco Gasi
Comment Utility
What are you getting now?
0
 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
Please explain "But its not working."

Is it producing an error or undesirable results?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:woepwobin
Comment Utility
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
Comment Utility
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
Comment Utility
Thats what it was, I hadn't escaped the $'s at the beginning of each line.

Thank you.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 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

16 Experts available now in Live!

Get 1:1 Help Now