【问题标题】:Php text field edit filephp文本字段编辑文件
【发布时间】:2013-04-14 01:19:30
【问题描述】:

我已经制作了一个系统,允许您通过网页上的文本字段编辑 config.php 文件但是文本框显示整个文件内容有没有办法我可以限制它只显示一行的一部分这个

$paypal_email = "mypaypalemail@gmail.com"; 

只显示

mypaypalemail@gmail.com

在文本框中。这是我的代码

<?php
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process)) {
    foreach ($val as $k => $v) {
        unset($process[$key][$k]);
        if (is_array($v)) {
            $process[$key][stripslashes($k)] = $v;
            $process[] = &$process[$key][stripslashes($k)];
        } else {
            $process[$key][stripslashes($k)] = stripslashes($v);
        }
    }
}
unset($process);
?>
<?php

$file = 'config.php';
// print_r( glob( dirname(__FILE__) . "/*.php"  ) );

$configFile = html_entity_decode(file_get_contents($file), ENT_NOQUOTES);

if ( isset( $_POST["save_button"] ) && $_POST["config_changes"]){
$changes = $path = str_replace("\"", "", $_POST["config_changes"]);
  file_put_contents($file, $_POST["config_changes"]);
}
?>

<html>
  <body>
  <center>
  <b>
  <font face="arial" size="3" color="#000000">
  Welcome edit your Options here
  <br>
    <form method="post" action="controlpanel.php">
      <textarea name="config_changes" rows="30" cols="50"><?php echo $configFile ?></textarea>
      <br>
      <button name="save_button">Save</button>
    </form>
  </body>
</html>

【问题讨论】:

  • 我认为这是不安全的编程,但您可能只需要使用正则表达式和 PHP preg_match 函数
  • 如果您打算执行 config.php,那真的非常不安全,因为注入任意代码是微不足道的。我建议改用 JSON 编码文件或类似文件,并清理允许从白名单中设置哪些键。

标签: php html file config textfield


【解决方案1】:

我的第一个想法是使用正则表达式,但我偶然发现了更好的方法。 准确地说是 PHP 函数 token_get_all()

您将文件内容传递给该函数(在此演示中,我使用 token_get_all 中的一些示例代码作为参数。不过,您可以在此处传递完整的文件。

$tokens = token_get_all('<?php $paypal_email = "mypaypalemail@gmail.com"; ?>');

$vars = array();
foreach ($tokens as $token) {
    if ($token[0] == T_CONSTANT_ENCAPSED_STRING) {
        $vars[] = $token[1];
        continue;
    }
}

var_dump($vars);

它应该返回该文件中的所有变量,如下所示:

array(1) { [0]=> string(25) ""mypaypalemail@gmail.com"" }

使用它进行一些调试并检查您正在搜索的变量是哪个索引。 然后,您可以访问给定索引处的变量。

如前所述,我不知道你在用它做什么,但听起来不安全。 我强烈建议您寻找其他解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2012-10-09
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多