我正在为我正在处理的网站制作一个安装脚本,我在制作生成 config.php 文件的脚本时遇到了麻烦。 以下是我为配置文件生成字符串的方法:
<?php
if (file_exists("config.php")) {
header("Location: index.php");
} else {
if (isset($_POST['smtp_password'])) {
if (!($configFile = fopen("config.php", "c"))) {
print("ERROR: Cannot write in this directory!");
exit();
}
$config = <<<EOT
<?php
$_AMCFG['login_dir'] = '{$_POST['login_dir']}'; /* LINE 12 */
$_AMCFG['server_key'] = '{$_POST['server_key']}';
$_AMCFG['host'] = '{$_POST['host']}';
$_AMCFG['database'] = '{$_POST['database']}';
$_AMCFG['user'] = '{$_POST['user']}';
$_AMCFG['password'] = '{$_POST['password']}';
$_AMCFG['smtp_name'] = '{$_POST['smtp_name']}';
$_AMCFG['smtp_mail'] = '{$_POST['smtp_mail']}';
$_AMCFG['smtp_host'] = '{$_POST['smtp_host']}';
$_AMCFG['smtp_port'] = {$_POST['smtp_port']};
$_AMCFG['smtp_user'] = '{$_POST['smtp_user']}';
$_AMCFG['smtp_password'] = '{$_POST['smtp_password']}';
?>
EOT;
fwrite($configFile, $config);
$db = mysqli_connect($_POST['host'], $_POST['user'], $_POST['password']);
mysqli_select_db($db, $_POST['database']);
$sqlFile = file_get_contents("install.sql");
mysqli_multi_query($sqlFile);
mysqli_query($db, "INSERT INTO admins (steamid, name, mail, disabled, superadmin) VALUES (\"".escape($_POST['admin_steamid'])."\", \"".escape($_POST['admin_name'])."\", \"".escape($_POST['admin_email'])."\", 0, 1)");
}
}
?>
这是我得到的错误:(第12行)
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
我做错了什么?
答案 0 :(得分:1)
以下是我对此问题的贡献,这是否是预期结果,请参阅下面的结果。
<?php
$config = "<?php\n\n";
$config .= '$_AMCFG[\'login_dir\']' . " = " . '$_POST[\'login_dir\']' . ";\n";
$config .= '$_AMCFG[\'server_key\']' . " = " . '$_POST[\'server_key\']' . ";\n\n";
$config .= '$_AMCFG[\'host\']' . " = " . '$_POST[\'host\']' . ";\n";
$config .= '$_AMCFG[\'database\']' . " = " . '$_POST[\'database\']' . ";\n";
$config .= '$_AMCFG[\'user\']' . " = " . '$_POST[\'user\']' . ";\n";
$config .= '$_AMCFG[\'password\']' . " = " . '$_POST[\'password\']' . ";\n\n";
$config .= '$_AMCFG[\'smtp_name\']' . " = " . '$_POST[\'smtp_name\']' . ";\n";
$config .= '$_AMCFG[\'smtp_mail\']' . " = " . '$_POST[\'smtp_mail\']' . ";\n";
$config .= '$_AMCFG[\'smtp_host\']' . " = " . '$_POST[\'smtp_host\']' . ";\n";
$config .= '$_AMCFG[\'smtp_port\']' . " = " . '$_POST[\'smtp_port\']' . ";\n";
$config .= '$_AMCFG[\'smtp_user\']' . " = " . '$_POST[\'smtp_user\']' . ";\n";
$config .= '$_AMCFG[\'smtp_password\']' . " = " . '$_POST[\'smtp_password\']' . ";\n\n";
$config .= "?>";
echo $config;
在HTML源代码中回显:
<?php
$_AMCFG['login_dir'] = $_POST['login_dir'];
$_AMCFG['server_key'] = $_POST['server_key'];
$_AMCFG['host'] = $_POST['host'];
$_AMCFG['database'] = $_POST['database'];
$_AMCFG['user'] = $_POST['user'];
$_AMCFG['password'] = $_POST['password'];
$_AMCFG['smtp_name'] = $_POST['smtp_name'];
$_AMCFG['smtp_mail'] = $_POST['smtp_mail'];
$_AMCFG['smtp_host'] = $_POST['smtp_host'];
$_AMCFG['smtp_port'] = $_POST['smtp_port'];
$_AMCFG['smtp_user'] = $_POST['smtp_user'];
$_AMCFG['smtp_password'] = $_POST['smtp_password'];
?>
答案 1 :(得分:0)
$config .= " $_AMCFG['smtp_port'] = ".$_POST['smtp_port'].";\n";
应该是:
$config .= " $_AMCFG['smtp_port'] = '".$_POST['smtp_port'].";\n";
我认为这个代码在&#34; evaled&#34;时失败了。
答案 2 :(得分:0)
我认为您的问题是在每行的第一部分使用双引号。你试图为$ _AMCFG [*]插入一个值,当它应该只是一个文字字符串时。
尝试在这部分周围使用单引号:
$config .= ' $_AMCFG['login_dir'] = \''.$_POST['login_dir']."';\n";
或者,我可以为此建议heredoc语法吗?那看起来像是:
$config = <<<EOT
<?php
$_AMCFG['login_dir'] = '{$_POST['login_dir']}';
$_AMCFG['server_key'] = '{$_POST['server_key']}';
...
$_AMCFG['smtp_password'] = '{$_POST['smtp_password']}';
?>
EOT;
更清洁。实际上,您可以将该配置开发为单独的PHP文件,然后将其剪切/粘贴到位。
答案 3 :(得分:0)
您的代码中有两个<?php
个实例。你只需要第一个。
第二个用于输出文件,需要进行转义。
$config = <<<EOT
\x3C?php
带有两个实例的旧版本让php解析器感到困惑。