What I have set up is a main page (that outputs a table based on a require_once()
PHP file with a PHP array), and admin page (that is supposed to write data to the aforementioned PHP array file), and the PHP file with the array. The admin page is the problem. I've tried different solutions with fread
and txt files that just got messy really fast.
I'm not that good at PHP. How can I edit this array from my admin page?
Code:
Main Page
<?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>
Array File
<?php
$sites = array(
array("POPNIC.COM", "uk", "pn"),
array("POPNIC.COM", "co.uk", "pn"),
...
array("SUBDOMAIN.COM", "aa6", "de"),
array("SUBDOMAIN.COM", "aa8", "de"),
);
?>
Admin Page
<?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
}
}
?>
UPDATE
I think I've almost got it. Here is my new code:
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);
}
It's not writing the data correctly. Every time I change something something else goes wrong. Can someone point me in the right direction?