我可以在 PHP 中使用 .ini 作为配置文件吗?

发布于 2024-08-02 02:55:23 字数 176 浏览 1 评论 0原文

我的朋友要求我更新他公司使用的 PHP 应用程序。 我发现该应用程序使用 .ini 扩展名作为数据库配置文件。 该文件包含数据库主机地址、用户名和密码! 问题是我可以在网络浏览器上访问该文件。

我试图理解为什么。 使用带有 .ini 扩展名的常规 php 文件有什么特殊原因吗??? 我就是不明白。

My friend asked me to update a PHP application that his company uses. I found out that the application uses .ini extension for DB configuration file. The file contains DB host address, username, and password!!. The problem is that I can access the file on web-browsers.

I am trying to understand why. Is there any particular reasons to use a regular php file with .ini extension??? I just don't get it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

じее 2024-08-09 02:55:23

可读性是我过去使用 ini 文件进行 php 脚本配置的主要原因之一。 非编码人员之前至少遇到过一次 ini 文件,并且可以理解它比简单的 php 文件容易得多。

可以通过服务器端配置来防止每个人都可读 ini 文件的问题,或者更好的是,只需在文件顶部的注释行中添加一行代码即可。

这样,当通过浏览器访问该文件时,php 将输出“禁止直接访问”,而 ini 文件将继续像以前一样运行。

Readability is one of the main reasons I've used ini filies for php script configs in the past. People who are not coders have run into an ini file at least once before, and can understand what it is much easier than even a simple php file.

The issue of ini files being readable by everyone can be prevented by server side configuration, or even better, by simply adding a single line of code inside a comment line at the top of the file.

That way php will output an 'Direct access forbidden' when the file is accessed via a browser, and the ini file will continue to function as before.

抹茶夏天i‖ 2024-08-09 02:55:23

您可以使用 Zend_Config_Ini。 它既舒适又简单。 只是不要将配置文件放在任何用户都可以访问的地方(例如 public_html)。

You can use Zend_Config_Ini. It is comfortable and easy. Just simply do not put config files where any user can reach them (for example public_html).

剩一世无双 2024-08-09 02:55:23

INI 文件只是处理配置的一种方式,也许开发人员来自 Windows 开发背景并使用他熟悉的任何东西:)。 此外,PHP通过parse_ini_file函数提供了一种解析INI文件的便捷方法。

不过,您需要确保无法从网络访问 .INI 文件。 将其移至文档根目录下方,以便您的 PHP 脚本仍然可以访问它,但随机浏览器不能访问它。

INI files are just one way of dealing with configuration, perhaps the developer came from a Windows-developing background and used whatever he was familiar with :). Besides, PHP offers a convenient way of parsing INI files through the parse_ini_file function.

You'll want to make sure the .INI file is not accessible from the web though. Move it below the docroot so your PHP script can still access it, but random browsers cannot.

梦回梦里 2024-08-09 02:55:23

无论如何,PHP 传统上使用 php.ini 配置 PHP。 那么也许这是某种遗产?

For what it's worth, PHP has traditionally used php.ini to configure PHP. So maybe it's some kind of legacy thing?

戏剧牡丹亭 2024-08-09 02:55:23

看起来这只是以前的程序员希望使用不同的文件类型进行配置。 如果该文件没有其他用途,请将其重命名为 *.php 并忽略它。 如果没有,请配置 Web 服务器将 ini 解析为 php,或者更好的是,将其移动到无法从 Web 服务器访问的目录。

Seems like this is just former programmer's wish to use different file type for configuration. If there is no other uses for this file, rename it to *.php and forget it. If not, configure webserver to parse ini as php or, better, move it to directory, not reachable from web-server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文