Php文本字段编辑文件


Php text field edit file

我做了一个系统,允许你通过网页上的文本框编辑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函数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"" }

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

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