我设置的是一个主页(输出基于require_once()
带有 PHP 数组的 PHP 文件的表)和管理页面(应该将数据写入上述 PHP 数组文件),以及带有数组的 PHP 文件. 管理页面是问题所在。我已经尝试了不同的解决方案fread
和 txt 文件,这些文件很快就变得一团糟。
我不擅长 PHP。如何从我的管理页面编辑这个数组?
代码:
主页
<?php
require_once("sitesdb.php");
?>
<html>
<body style="text-align:center">
<div style="width:500px;background-color:ddd;padding:10px">
<table width="100%">
<tr bgcolor="gray">
<td>Domain Host</td>
<td>Prefix</td>
<td>Suffix</td>
</tr>
<? foreach ($sites as $row) : ?>
<tr bgcolor="white">
<td><? echo $row[0]; ?></td>
<td><? echo $row[1]; ?></td>
<td><? echo $row[2]; ?></td>
</tr>
<? endforeach; ?>
</table>
</div>
</body>
</html>
数组文件
<?php
$sites = array(
array("POPNIC.COM", "uk", "pn"),
array("POPNIC.COM", "co.uk", "pn"),
...
array("SUBDOMAIN.COM", "aa6", "de"),
array("SUBDOMAIN.COM", "aa8", "de"),
);
?>
管理页面
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Administration"');
header('HTTP/1.0 401 Unauthorized');
echo '404 - Unauthorized! Sorry about that.';
exit;
} else {
if ($_SERVER['PHP_AUTH_USER'] = "*****" && $_SERVER['PHP_AUTH_PW'] = "*****"){
if ($_POST['host'] && $_POST['prefix'] && $_POST['suffix']){
??????????????
}
?>
<html>
<head>
<title>Administration</title>
</head>
<body>
<h1>Administration</h1>
<h2>Add a Value</h2>
<form method="post" action="">
<label for="host">Domain Host: </label><br />
<input name="host" id="host" type="text" /><br />
<label for="prefix">Prefix: </label><br />
<input name="prefix" id="prefix" type="text" /><br />
<label for="suffix">Suffix: </label><br />
<input name="suffix" id="suffix" type="text" /><br />
<input type="submit" value="Add Value" /><br />
</form>
</body>
<?php
}
}
?>
更新
我想我已经差不多了。这是我的新代码:
if ($_POST['host'] && $_POST['prefix'] && $_POST['suffix']){
$ins_host = $_POST['host'];
$ins_prefix = $_POST['prefix'];
$ins_suffix = $_POST['suffix'];
include_once("sitesdb.php");
array_push($sites, array($ins_host, $ins_prefix, $ins_suffix));
$file = fopen("sitesdb.php", "w");
$newdata = "<?php " . var_export($sites) . "?>";
fwrite($file, $newdata);
fclose($file);
}
它没有正确写入数据。每次我改变一些东西都会出错。有人可以指出我正确的方向吗?