如何设置并保存PHP文件中的变量?

如何设置并保存PHP文件中的变量?,php,javascript,html,Php,Javascript,Html,例如,我有一个config.php: //default config $name = 'default name'; $mail = 'default mail'; 我想使用一个表单让用户编辑变量,一切都像使用数据库一样,但在这种情况下,区别在于它是在PHP文件上实现的 所以,在我得到$\u POST['name']之后$_邮寄[邮件],如何修改变量并将其保存在该PHP中?您可以使用file\u put\u contents函数将配置写入文件,并使用自定义函数构建内容本身,如下所示: fun

例如,我有一个config.php:

//default config
$name = 'default name';
$mail = 'default mail';
我想使用一个表单让用户编辑变量,一切都像使用数据库一样,但在这种情况下,区别在于它是在PHP文件上实现的


所以,在我得到
$\u POST['name']之后$_邮寄[邮件],如何修改变量并将其保存在该PHP中?

您可以使用file\u put\u contents函数将配置写入文件,并使用自定义函数构建内容本身,如下所示:

function generateConfig(){
    return "//default config"
    . '$name' . " ='{$_POST['name']}';"
    . '$mail' . " ='{$_POST['mail']}';";
}

file_put_contents("file.config", generateConfig());

这是一个解决方案,但更好的方法是使用JSON、XML或数据库。

您可以使用file\u put\u contents函数将配置写入文件,并使用自定义函数构建内容本身,如下所示:

function generateConfig(){
    return "//default config"
    . '$name' . " ='{$_POST['name']}';"
    . '$mail' . " ='{$_POST['mail']}';";
}

file_put_contents("file.config", generateConfig());
$config = file_get_contents('config.php');
这是一个解决方案,但更好的方法是使用JSON、XML或数据库

$config = file_get_contents('config.php');
将配置文件读入字符串。然后可以搜索字符串或使用正则表达式将
$name
值替换为
$\u POST['name']

这应该行得通

$name = $_POST['name'];
$mail = $_POST['mail'];
$config = file_get_contents('config.php');
$pos = strpos($config, '$name');
$config = substr_replace($config, '$name = \'' . $name . '\'', $pos, strpos($config, ';', $pos) - $pos);
$pos = strpos($config, '$mail');
$config = substr_replace($config, '$mail = \'' . $mail . '\'', $pos, strpos($config, ';', $pos) - $pos);
file_put_contents('config.php', $config);
将配置文件读入字符串。然后可以搜索字符串或使用正则表达式将
$name
值替换为
$\u POST['name']

这应该行得通

$name = $_POST['name'];
$mail = $_POST['mail'];
$config = file_get_contents('config.php');
$pos = strpos($config, '$name');
$config = substr_replace($config, '$name = \'' . $name . '\'', $pos, strpos($config, ';', $pos) - $pos);
$pos = strpos($config, '$mail');
$config = substr_replace($config, '$mail = \'' . $mail . '\'', $pos, strpos($config, ';', $pos) - $pos);
file_put_contents('config.php', $config);

配置文件是否需要使用PHP?您可以使用

配置文件
config.php

$config = array(
  'name' => 'My Name',
  'email' => 'some@one',
);
正在加载/保存配置文件:

include 'config.php';  // Bring config into the current namespace
if ($_SERVER['REQUEST_METHOD'] == 'POST') {  // Update on POST
 $config['name'] = $_POST['name'];
 $config['email'] = $_POST['email'];
 file_put_contents('config.php', sprintf("<?php\n%s\n", var_export($config, true)));
}
print_r($config);
包括'config.php';//将配置带到当前命名空间中
如果($\u服务器['REQUEST\u METHOD']='POST'){//POST更新
$config['name']=$\u POST['name'];
$config['email']=$_POST['email'];

file_put_contents('config.php',sprintf(配置文件需要使用php?您可以使用

配置文件
config.php

$config = array(
  'name' => 'My Name',
  'email' => 'some@one',
);
正在加载/保存配置文件:

include 'config.php';  // Bring config into the current namespace
if ($_SERVER['REQUEST_METHOD'] == 'POST') {  // Update on POST
 $config['name'] = $_POST['name'];
 $config['email'] = $_POST['email'];
 file_put_contents('config.php', sprintf("<?php\n%s\n", var_export($config, true)));
}
print_r($config);
include'config.php';//将config放入当前名称空间
如果($\u服务器['REQUEST\u METHOD']='POST'){//POST更新
$config['name']=$\u POST['name'];
$config['email']=$_POST['email'];

file_put_contents('config.php',sprintf)(“不要。严肃地说,严肃地说,不要。如果你想使用可编辑的数据,那么就使用一个数据库或使用一个合理格式的结构化配置文件(如ini、JSON、XML等)。试图以编程方式编辑PHP是一件非常重要的事情。这个配置文件基本上只是一个带有设置的文本文件吗?它只是一个带有变量的文本文件,例如id、pwd、服务器名称等。没错。@Quentin,谢谢,我使用PHP是因为它很容易实现,我只需要包含PHP并使用相应的变量即可开始我的工作,但如果我想做一些改变,我需要对它进行编辑。这里只暗示了部分可怕之处-解析PHP的复杂性与自我修改代码的风险/稳定性/争用性相比是微不足道的。不要。认真地说,认真地说,不要。如果你想使用可编辑数据,那么就使用数据库或结构化配置文件采用合理的格式(如ini、JSON、XML等)。试图以编程方式编辑PHP是一件非常重要的事情。这个配置文件基本上只是一个带有设置的文本文件吗?它只是一个带有变量的文本文件,例如id、pwd、服务器名称等。没错。@Quentin,谢谢,我使用PHP是因为它很容易实现,我只需要包含PHP并使用相应的变量即可开始我的工作,但如果我想做一些改变,我需要编辑它。这里只暗示了部分可怕之处-与自我修改代码的风险/稳定性/争用性相比,解析PHP的复杂性很小。危险:PHP代码注入漏洞。这使用户能够执行他们喜欢的任何PHP,包括
系统
调用。而且,它实际上不会工作。由于字符串是双引号,
$name
$mail
将插入到字符串中。你是对的,我对变量的看法不好。但是,这仍然是一些系统在启动时的工作方式。例如Joomla.Danger:PHP代码注入漏洞。这会使用能够执行他们喜欢的任何PHP,包括
系统
调用。而且,它实际上不会工作。由于字符串是双引号,
$name
$mail
将插入字符串中。你是对的,我对变量的看法不好。但是,有些系统在启动时仍然是这样工作的。例如Joomla.it w在我使用您提供的代码更改文件后,我将错过和$string=谢谢。哦,您是对的。我已经更新了答案。另外请注意,
config.php
中不需要结尾
?>
作为EOF(文件结尾)与
?>
的作用相同。在我使用您提供的代码更改文件后,它将丢失和$string=谢谢。哦,您是对的。我已经更新了答案。另外请注意,
config.php
中不需要结尾
?>
,因为EOF(文件结尾)的作用与
?>
相同