-1

I'm attempting to create an automatic installer for a system I have created, but the ONLY issue is the generation of a php file, in which variables simply do not show up.

Here's my code to create the file

<?php
$content = " <?php
    $sqlHost = '".$host."';
    $sqlUser = '".$user."';
    $sqlPass = '".$pass."';
    $sqlDatabase = '".$db."';
    $sqlTables = array('donate_categories', 'donate_items', 'donate_claim', 'donate_admin');

    $connection = new PDO('mysql:host='.$sqlHost.';dbname='.$sqlDatabase.';charset-utf8', $sqlUser, $sqlPass);
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>";

$fp = fopen("connection.php", "wb");
fwrite($fp, $content);
$fclose($fp);
?>

as you can see there's php code wrapped in a string there; However the output is a little strange...

<?php
     = 'localhost';
     = 'root';
     = '';
     = 'test';
     = array('donate_categories', 'donate_items', 'donate_claim', 'donate_admin');

     = new PDO('mysql:host='..';dbname='..';charset-utf8', , );
    (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

None of the variables are there, and I've looked up possible issues with this, but I haven't found any.

6
  • Try error_reporting(E_ALL);. Double quotes interpolate variables. Commented Dec 19, 2014 at 23:16
  • The problem itself can be easily fixed by escaping the $ symbols (or switching the double quotes to single quotes), but why would you want to do this? What exactly is the point of using PHP code generate more PHP code? Commented Dec 19, 2014 at 23:20
  • 1
    This seems like a good use case for a heredoc rather than this confusing concatenation. php.net/manual/en/… The $connection would still need \$ escaping though. Commented Dec 19, 2014 at 23:20
  • 1
    Just a thought: it would probably be cleaner to generate a ini or yaml file. Commented Dec 19, 2014 at 23:21
  • 1
    @Dagon It's not a duplicate. That question is for displaying PHP on a web page. This question is about writing it to a file. The solutions are different. The problem he has here is actually with quoting. Commented Dec 20, 2014 at 0:33

2 Answers 2

4

I can't test this right now, but since you've wrapped the text in double quotes, your server is parsing the variables $sqlHost, $sqlUser, ... etc as variables and looking up their values in the string. Try escaping the values like so, and let me know if it works as you intend it to.

<?php
$content = " <?php
    \$sqlHost = '".$host."';
    \$sqlUser = '".$user."';
    \$sqlPass = '".$pass."';
    \$sqlDatabase = '".$db."';
    \$sqlTables = array('donate_categories', 'donate_items', 'donate_claim', 'donate_admin');

    \$connection = new PDO('mysql:host='.\$sqlHost.';dbname='.\$sqlDatabase.';charset-utf8', \$sqlUser, \$sqlPass);
   \$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>";

$fp = fopen("connection.php", "wb");
fwrite($fp, $content);
$fclose($fp);
?>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, it works; Could you provide a link to more information on the different between using 'string' and "string".. I had no idea that "string" would parse variables, does 'string' not?
@Christian.tucker That is explained in the strings doc I linked up in the main comment thread
Null User beat me to it, that link should have all the information you need. And to answer your question, no, strings encapsulated in single quotes such as '$string' would not parse variables, but "$string" would. I believe (been awhile since I thumbed through the INI specs) there's an option you can turn on/off, but it could be totally deprecated by now.
@Christian.tucker: You are writing an installer script for a system you created … without even knowing such absolute basics? Wow … installing your system might turn out “interesting” for whoever uses it.
|
0

Use single quotes, because it is trying to replace the variables into the string.

<?php
$content = ' <?php
    $sqlHost = "'.$host.'";
    $sqlUser = "'.$user.'";
    $sqlPass = "'.$pass.'";
    $sqlDatabase = "'.$db.'";
    $sqlTables = array("donate_categories", "donate_items", "donate_claim", "donate_admin");

    $connection = new PDO("mysql:host=".$sqlHost.";dbname=".$sqlDatabase.";charset-utf8", $sqlUser, $sqlPass);
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>';

$fp = fopen('connection.php', 'wb');
fwrite($fp, $content);
$fclose($fp);
?>

http://php.net/manual/es/language.types.string.php

Regards.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.