doujia9833 2013-03-25 21:18
浏览 20
已采纳

在另一个文件中将数据添加到PHP数组[关闭]

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?

  • 写回答

1条回答 默认 最新

  • douweilaton2762 2013-03-25 22:27
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)