Search code examples
phpfilevariablesdynamicgenerated

Can't set variable in generated php code


I have a php script that generates 3 files:

  1. A .txt file
  2. A .php file

The Script works fine but one thing destroys the idyll, the script does not write the vars in the 2nd generated script (I have also tried . $var . ). Any help would be appreciated.

The Code:

    <?php
$filename = uniqid(rand(), true) . '.php';

$fh = fopen($filename, "w+");
if($fh)
{
    fwrite($fh, "<html><body>
        <div align='center'>
        Neue Nachricht erstellen<br><br>

        <form action='' . $filenamebes . '' Method='post'>

        Titel:<br>
        <input name='Name' size='40'><br><br>

        Inhalt:<br>

        <textarea name='inhalt' cols='40' rows='12'
        wrap='physical'></textarea><p>
        <input type='submit' value='Absenden'>
        </form><br><br>
                    <?php
                    $beitrag = file('' . $filenametxt . '');
                    krsort($beitrag);

                    foreach($beitrag as $ausgabe)
                       {
                       $ausgabe = stripslashes($ausgabe);
                       $zerlegen = explode('|', $ausgabe);

                       echo '
                       <table align=\'center\'
                        border=\'1\' cellspacing=\'0\'
                       cellpadding=\'5\' bordercolorlight=\'black\'
                       bordercolordark=\'black\' width=\'50%\'>
                       <tr>
                       <td>
                      Titel: <a href=\'mailto:$zerlegen[0]\'>$zerlegen[1]</a>
                      am $zerlegen[2]
                       </td>
                       </tr>

                       <tr>
                       <td>
                       $zerlegen[3]
                       </td>
                       </tr>
                       </table><br>
                       ';
                       }
                    ?>
                </div>
</body>
</html>
");
}
fclose($fh);


$filenametxt = uniqid(rand(), true) . '.txt';
$fhtxt = fopen($filenametxt, "w+");
if($fhtxt)
{
    fwrite($fhtxt, "");
}
fclose($fhtxt);


$filenamebes = uniqid(rand(), true) . '.php';

$fhbes = fopen($filenamebes, "w+");
if($fhbes)
{
    fwrite($fhbes, "<html>
<head>
<title>Speichere Nachricht</title>
</head>
<body>

<?php
$user = {$_POST['Name']};
$user = htmlentities($user);

$inhalt = {$_POST['inhalt']};
$inhalt = htmlentities($inhalt);
$inhalt = str_replace('\n', '<br>', $inhalt);

$email = {$_POST['EMail']};
$email = htmlentities($email);

if ($inhalt == '' or $user == '')
   {
   echo 'Sie müssen das Feld \'Namen\'
   und \'Inhalt\' ausfüllen';
   }

else
   {
   $datum= date('d.m.Y H:i:s');

   $eintrag='$email|$user|$datum|$inhalt';

   $datei = fopen('' . $filenametxt . '', 'a');
   fwrite($datei, '\n'.$eintrag);
   fclose($datei);

   echo 'Ihre Nachricht wurde erfolgreich gespeichert';
   }
?>

<br>
<a href='' . $filename . ''>Zurück</a>
</body>
</html>");
}
fclose($fhbes);


echo "PHP = $filename TXT = $filenametxt BES = $filenamebes <br><br><a href='".$filename."'>ÖffnenPHP</a> <br><br><a href='".$filenametxt."'>ÖffnenTXT</a> <br><br><a href='".$filenamebes."'>ÖffnenBES</a>"
?>

Solution

  • You are using Super string format to build your string. Super string means double quotes "".

    here

    fwrite($fh, "<html><body>
    

    and you use php variables in the super string format. So php identify the variable as a string or whatever it contains

    Ex :

    $a="ABCD";
    echo "$a"; // this will print ABCD
    

    but when you do it this way

    $a="ABCD";
    echo '$a';// this will print $a
    

    Here is the solution

        fwrite($fh, '<html><body>
            <div align="center">
            Neue Nachricht erstellen<br><br>
    
            <form action=" . $filenamebes . " Method="post">
    
            Titel:<br>
            <input name="Name" size="40"><br><br>
    
            Inhalt:<br>
    
            <textarea name="inhalt" cols="40" rows="12"
            wrap="physical"></textarea><p>
            <input type="submit" value="Absenden">
            </form><br><br>
                        <?php
                        $beitrag = file(" . $filenametxt . ");
                        krsort($beitrag);
    
                        foreach($beitrag as $ausgabe)
                           {
                           $ausgabe = stripslashes($ausgabe);
                           $zerlegen = explode("|", $ausgabe);
    
                           echo "
                           <table align=\"center\"
                            border=\"1\" cellspacing=\"0\"
                           cellpadding=\"5\" bordercolorlight=\"black\"
                           bordercolordark=\"black\" width=\"50%\">
                           <tr>
                           <td>
                          Titel: <a href=\"mailto:$zerlegen[0]\">$zerlegen[1]</a>
                          am $zerlegen[2]
                           </td>
                           </tr>
    
                           <tr>
                           <td>
                           $zerlegen[3]
                           </td>
                           </tr>
                           </table><br>
                           ";
                           }
                        ?>
                    </div>
    </body>
    </html>
    ');