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

Recommended Answers

All 3 Replies

Use EOF:

<?php
echo <<<EOF
<?php echo 'test'; ?>
EOF;
?>

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

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
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.