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?
Assuming that you want to collect data from the admin page and write it as array to sitesdb.php checkout the var-export function.
In your admin script you check if your form was submitted using
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// your logic to read the posted data and save it to a file
// goes here
}
I looked for a simple, not-outdated, non-stupid tutorial on the web, not so easy to find. Most people use Frameworks like Laravel, Symfony2, Silex or Slim now (e.g. to avoid security issues). Eventually you'll find this one helpful.
Update: Note the additional parameter of var_export which will return the result in a variable instead of printing it out
Update 2: I don't want to frustrate you, but this approach is really insecure. Imagine someone writing evil php stuff in your form. You'll write it in the sitesdb.php file and execute it the next time you are reading sitesdb.php. Better have a look at ini files (parse_ini_file is the command to read an ini file in PHP).