0

Hi,

I have used this code to make a php file with php code in it:

<?php
$fn = 'connect.inc.php';
$string = "<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_password = '';
$mysql_database = 'main';

@mysql_connect($mysql_host, $mysql_user, $mysql_password) or die("There was an error connecting to the server");
@mysql_select_db($mysql_database) or die("There was an error connecting to the table");
?>";
if ($handle = fopen($fn, 'w')){
	if (!fwrite($handle, $string)){
		die("Couldn't write to file");
		echo "Writing success";
	}
}
?>

However, the code inside the variable string gets executed but I don't want this to happen. How to tell php to avoid executing it?

Thanks in advance,
Nahiyan

2
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by cereal
0

I have no idea how to use eof, can you modify my code so that it will work with eof?

0

I'm sorry, I gave you wrong suggestion, I was tired x_x
Use single quotes instead:

<?php
$fn = 'connect.inc.php';
$string = '<?php
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "main";

@mysql_connect($mysql_host, $mysql_user, $mysql_password) or die("There was an error connecting to the server");
@mysql_select_db($mysql_database) or die("There was an error connecting to the table");
?>';

if ($handle = fopen($fn, 'w')){
	if (!fwrite($handle, $string)){
		die("Couldn't write to file");
		echo "Writing success";
	}
}
?>

Writing:

<?php
$fruit = 'apples';
echo "oranges, cherries, $fruit"; # will display apples
echo 'oranges, cherries, $fruit'; # will display $fruit
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.