简体   繁体   English

无法在生成的php代码中设置变量

[英]Can't set variable in generated php code

I have a php script that generates 3 files: 我有一个生成3个文件的php脚本:

  1. A .txt file .txt文件
  2. A .php file .php文件

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 . ). 该脚本可以正常工作,但是有一件事破坏了田园诗,该脚本没有在第二个生成的脚本中写入vars(我也尝试过. $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>"
?>

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. 并且您使用超字符串格式的php变量。 So php identify the variable as a string or whatever it contains 因此,php将变量标识为字符串或包含它的任何内容

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>
');

PHP is treating those terms "$filenamebes" "$zerlegen" as actual variables. PHP将那些术语“ $ filenamebes”,“ $ zerlegen”视为实际变量。

Because those variables don't exist, it will not write anything to the file. 由于这些变量不存在,因此不会将任何内容写入文件。

Instead you should escape those variables like so: \\$filenamebes . 相反,您应该这样转义那些变量: \\$filenamebes This will allow you to physically write $filenamebes to the file, and not have PHP search for its value (null). 这将允许您实际将$filenamebes写入文件,而无需PHP搜索其值(空)。

Proof of concept: 概念证明:

<?php
$a = 'test';
$s = "\$a"; 
echo $s ."\n";  //will print '$a'

echo "---\n";

$s = "$a";
echo $s ."\n"; //will print 'test'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM