【问题标题】:Serialized data being displayed显示的序列化数据
【发布时间】:2015-08-13 21:15:33
【问题描述】:

我需要为我的应用程序制作一个配置文件,但我偶然发现了一个问题,我试图解决它但找不到问题,这里的代码有效,除了一件事,每当我提交表单时,它会保存数据,但是当它重新加载时,它只会显示存储在 config.txt 中的序列化数据。

有谁知道为什么会发生这种情况以及我该如何解决它

感谢您的帮助 -oedze

编辑:更清楚地解释: 想法是config.txt中存储的数据都会在表单中输入,所以不用不断的添加,可以看到值是什么。当您按下提交按钮时,页面会将所有数据保存在 $config 数组中,然后将其保存在 config.txt 中,完成后将重新加载页面,因此将删除发布数据用户缓存。但是,每当页面重新加载时,它都会显示类似 a:1:{s:9:"groupname";s:4:"test";} 而不是 html 页面

<!DOCTYPE html>
<html>
<head>
    <?php
        function getConfig(){
            $file = fopen("config.txt", "r");
            $fileData = fread($file, filesize("config.txt"));
            fclose($file);
            $config = unserialize($fileData);

            return $config;
        }

        function saveConfig($config){
            $file = fopen("config.php", "w");
            fwrite($file, serialize($config));
            fclose($file);
        }
        if(isset($_GET["updateConfig"])){
            $config = array();
            foreach($_POST as $key => $value){
                $config[$key] = $value;
            }
            saveConfig($config);
            header("Location: config.php");
        }else{
            $config = getConfig();
        }



    ?>
</head>
<body>
    <h1>Configuratie</h1>
    <form method="post" action="config.php?updateConfig">
    <table>
        <tr class="optionsRow"><td class="optionsData">Groupname:</td><td class="optionsData"><input type="text" name="groupname" value="<?php $config["groupname"]?>"></td></tr>   

    </table>
    <input type="Submit" value="Opslaan">
    </form>
</body>
</html>

【问题讨论】:

  • 每当我按下提交按钮时,页面将重新加载,数据将被保存,但随后它将显示序列化数据而不是重新加载的页面,我的问题是为什么会发生这种情况以及我怎么能解决它
  • 由于您的所有代码都在一个文件中,因此请从文件中删除操作属性。谢谢
  • @anantkumarsingh 这个问题的联系在哪里?另外,我想说这无关紧要。
  • 它将显示序列化数据而不是重新加载的页面 - 抱歉,我仍然无法找出问题所在。你能解释清楚点吗?
  • 我想说,既然你只有一个“组”(只有一个输入),那么序列化和非序列化数据之间的区别在哪里?它是一个字符串!

标签: php html forms serialization


【解决方案1】:

您在这一行有一个错误:$file = fopen("config.php", "w");,这是config.txt 而不是config.php

【讨论】:

    猜你喜欢
    • 2019-10-06
    • 1970-01-01
    • 2021-09-30
    • 2014-01-22
    • 2020-04-09
    • 2019-04-23
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    相关资源
    最近更新 更多