【问题标题】:Best way to store html in a config file将 html 存储在配置文件中的最佳方法
【发布时间】:2012-05-06 23:50:34
【问题描述】:

我正在编写自己的下载跟踪器,我想为用户提供在下载页面上显示自定义消息的能力。我想允许 html 和 javascript,以便用户可以编写段落或使用广告代码等。

我将我的设置存储在配置文件中(不是我所知道的最佳方式)

示例:<?php define("WAIT_MESSAGE", "htmlcodehere"); ?>

问题是引号或斜杠会弄乱配置文件,并且无法加载设置页面。我已经研究过添加斜杠以尝试转义这些字符,但它只是添加了多个斜杠。

在我的配置文件中存储 html 内容/javascript 的最佳方式是什么?

编辑:尝试了几种方法,但是每次单击保存以更新配置文件时,所有引号都会转义,\"hello\" 变为 \"hello\" 等

【问题讨论】:

  • 你是什么意思引号或斜线弄乱了配置文件??你的配置文件是什么格式...你能放一个smached配置文件的样本

标签: php quotes


【解决方案1】:

您是否尝试过像单个 ' 一样?

<?php define('WAIT_MESSAGE', '<p>Please wait.. your download starts shortly</p>'); ?>

【讨论】:

    【解决方案2】:

    您应该如此信任您的用户,以至于您让他们在您的网站上发布和保存 JavaScript 和 HTML。

    【讨论】:

    • 是自己网站用的脚本,只是想让他们自定义一下
    【解决方案3】:

    这根本不安全。有人可以轻松地将 PHP 注入其中。

    你可以做的(有点hacky)是base64_encode()数据,当你需要使用它时base64_decode()它。这样做将消除引号/特殊字符问题和安全问题。

    在配置文件中编写 base64_encoded HTML 后,要使用它,您将执行以下操作:

    <?php
        echo base64_decode(WAIT_MESSAGE);
    ?>
    

    【讨论】:

      【解决方案4】:

      允许用户在你的页面中实际插入 HTML/Javascript/PHP 是一件非常糟糕的事情

      说了这么多,这个问题时常困扰着我们所有人。您需要以某种不会改变上述代码含义的格式存储 HTML 代码。

      这个问题通常可以通过将任何此类字符转换为其等效的 HTML 实体来解决,以便您可以安全地存储

      查看http://php.net/manual/en/function.htmlspecialchars.phphttp://www.php.net/manual/en/function.htmlspecialchars-decode.php 了解更多信息。

      【讨论】:

      • 是的,脚本将由用户自行托管,他们将在其自己的网站上添加代码。您的答案有效,但引号仍然被转义,每次用户点击保存时,引号都会再次转义。 \"hello\" 变成 \\"hello\\" 等等。
      • @JoshFradley 您启用了magic_quotes,在设计将遇到不同设置的脚本时,您应该使用get_magic_quotes_gpc() 进行检查,并相应地处理斜杠的剥离。
      • 您在调用这些函数时是否设置了ENT_QUOTES 标志。根据文档,双引号 (") 应编码为 &amp;quot;,单引号 (') 应编码为 &amp;#039;
      • @LawrenceCherone 这行得通吗? if (get_magic_quotes_gpc()) { $waitmessage = stripslashes(htmlspecialchars($_POST["waitmessage"])); $waitadcode = stripslashes(htmlspecialchars($_POST["waitadcode"])); } else { $waitmessage = htmlspecialchars($_POST["waitmessage"]); $waitadcode = htmlspecialchars($_POST["waitadcode"]); }
      【解决方案5】:

      为了安全起见,我个人会在数据库中保存任何可编辑的值, 但是如果你真的想要/需要编辑一个 php 配置文件,那么这也许是最安全的方法。

      <?php 
      /*Function to check if magic_quotes is enabled.
       (Stops double slashes happening)
      */
      function check_magic_quotes($value){
          if (get_magic_quotes_gpc()) {
              return stripslashes($value);
          } else {
              return $value;
          }
      }
      
      /*Form was posted, 
      You should also do a check to see if logged in and have rights to edit*/
      if($_SERVER['REQUEST_METHOD']=='POST'){
          //Check for magic quotes and then base64_encode the string.
          $value = base64_encode(check_magic_quotes($_POST['configVal']));
      
          /*Use heredoc to create the php line for the config & insert the
            base64 encoded string into place*/
      $config=<<<CONFIG
          <?php define("WAIT_MESSAGE", '$value'); ?>
      CONFIG;
      
          file_put_contents('someConfig.php',$config);
      }
      
      
      //When you want to include the config
      include('someConfig.php');
      
      /*To echo out the config value: base64_decode it,
        and then htmlentities encode it, to protect from XSS*/
      echo 'This was included: '.htmlentities(base64_decode(WAIT_MESSAGE));
      
      
      //Basic form with current value when someConfg.php has not been included
      $config = file_get_contents('someConfig.php');
      preg_match("#\"WAIT_MESSAGE\", '(.*?)'#",$config,$match);
       ?>
      
      <form method="POST" action="">
        <p>Config Value:<input type="text" name="configVal" value="<?php echo htmlentities(base64_decode($match[1]));?>" size="20"><input type="submit" value="Update"></p>
      </form>
      

      【讨论】:

        猜你喜欢
        • 2011-07-30
        • 2013-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-10
        • 2012-07-01
        • 2011-11-11
        • 1970-01-01
        相关资源
        最近更新 更多