如何轻松更新另一个文件中的PHP数组 [英] How To Update a PHP Array In Another File Easily

查看:97
本文介绍了如何轻松更新另一个文件中的PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个confg.php文件,其中包含如下配置详细信息:

so I have a confg.php file which contains config details like this:

$CONFIG = Array (
'APIKey' => 'xxxxxx',
'USERNAME' => 'admin',
'PASSWORD' => 'password',
);

我希望用户能够通过admin.php更新这些凭据,但是我不确定如何我可以通过另一个文件更新阵列,所以请帮帮我。谢谢!

I want users to be able to update these credentials through admin.php but I am quite not sure how I can update an array through another file so please help me out. Thanks!

推荐答案

我认为您正在解决此错误。我建议您创建一个名为config的数据库表,并在此处的列中包含APIKey,USERNAME和PASSWORD。在您的admin.php中,您可以允许用户更新这些记录..

I think you're approaching this wrong. I would suggest you create a database table called config and have in here columns for APIKey, USERNAME and PASSWORD. Within your admin.php you can allow the user to update these records ..

update settings_table set APIKEY='a', USERNAME='b', PASSWORD='c' where id=1

然后在您的前端可以查询该表以获取值..

And then on your front end you can query this table to get the values ..

$query = mysql_query("select * from settings_table where id=1", $connection);
$row = mysql_fetch_array($query, MYSQL_ASSOC);

$CONFIG = Array (
   'APIKey' => $row['APIKey'],
   'USERNAME' => $row['USERNAME'],
   'PASSWORD' => $row['PASSWORD'],
);

编辑:您不想使用MySQL

You don't want to use MySQL

确定,您可以使用存储配置设置的文件来执行此操作。您应该将其放置在不可通过网络访问的目录中。

OK, you could do this with a file that stores the config settings. You should put this into a non-web accessible directory.

因此,在admin.php文件中,您可以将详细信息写入文件:

So, in your admin.php file you can write the details to a file:

$myFile = "/full/path/to/config.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_POST['APIKey'] . "\n";
$stringData = $_POST['USERNAME'] . "\n";
$stringData = $_POST['PASSWORD'] . "\n";
fclose($fh);

然后在前端,您可以打开文件,拆分行,然后构建$配置数组:

And then on your front end you can open the file, split up the rows and then build your $CONFIG array:

$data = file_get_contents("/full/path/to/config.txt");
$data = explode("\n", $data);

$CONFIG = Array (
   'APIKey' => $data[0],
   'USERNAME' => $data[1],
   'PASSWORD' => $data[2],
);

这篇关于如何轻松更新另一个文件中的PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆