<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<form name="form" method="POST" action="">
<tr><td colspan="2" align="center"> Database </tr>
<tr><td>Servername:</td>
<td><input type="text" name="servername" value="" ></td></tr>
<tr><td>Username:</td>
<td><input type="text" name="username" value="" ></td>
</tr>
<tr><td>Password:</td>
<td><input type="text" name="password" value="" ></td>
</tr>
<tr><td>Database Name:</td>
<td><input type="text" name="dbname" value=""> </td>
</tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" ></td</tr>
</form>
</table>
</body>
</html>
我想使用表单的值创建一个php配置文件。我无法理解我怎么能这样做。我现在学习所以请建议我..
答案 0 :(得分:1)
如果是配置,您可以:
.php
扩展名创建文件,然后使用file_put_contents
或fwrite
写一些数据。您需要将<?php ?>
标记写入您想要的文件和变量OR preg_replace
或其他内容替换虚拟值。这是IMHO更好,更灵活的解决方案。 答案 1 :(得分:1)
简单示例:
// save to file
$post = json_encode($POST);
file_put_contents('config.txt', $post);
// read from file
$post1 = file_get_contents('config.txt');
$p = json_decode($post1);
// show array
print_r($p);
// show username
echo $p['username'];
或php文件
if (isset($_POST['username'])) {
$php = '<?php
$user = "'.$_POST['username'].'";
$pass = "'.$_POST['pass'].'";
?>';
file_put_contents('config.php', $php);
}
答案 2 :(得分:0)
要从用户输入创建配置文件,您可以创建一个空的config.php
文件,然后使用fwrite
和fopen
打开并写入该文件。
<?PHP
$errors = "";
if (isset($_POST["submit"])) {
if (empty($_POST['servername'])) {
echo "Enter servername";
$errors++;
} else {
$servername = $_POST['servername'];
}
if (empty($_POST['username'])) {
echo "enter username";
$errors++;
} else {
$username = $_POST['username'];
}
if (empty($_POST['password'])) {
echo "enter password";
$errors++;
} else {
$password = $_POST['password'];
}
if (empty($_POST['dbname'])) {
echo "enter database";
$errors++;
} else {
$dbname = $_POST['dbname'];
}
if ($errors <= 0) { // no errors
$string = '<?php
$dbhost = "' . $servername . '";
$dbuname = "' . $username. '";
$dbpass = "' . $password . '";
$dbname = "' . $dbname . '";
?>';
$fp = fopen("config.php", "w");
if (fwrite($fp, $string)) {
echo "db created";
}
fclose($fp);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<form name="form" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<tr><td colspan="2" align="center"> Database </tr>
<tr><td>Servername:</td>
<td><input type="text" name="servername" value="" ></td></tr>
<tr><td>Username:</td>
<td><input type="text" name="username" value="" ></td>
</tr>
<tr><td>Password:</td>
<td><input type="text" name="password" value="" ></td>
</tr>
<tr><td>Database Name:</td>
<td><input type="text" name="dbname" value="" > </td>
</tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit"></td></tr>
</form>
</table>
</body>
</html>