$符号的parse_ini_file问题

时间:2017-02-27 10:45:19

标签: php database connection

我有一个 settings.ini.php 文件,其中包含我的数据库详细信息。

;<?php return; ?>
[SQL]
host = localhost
user = root
password =Gohead123$
dbname = nri_demo

并且

**db.class.php**
private function Connect()
        {
            $this->settings = parse_ini_file("settings.ini.php");
                     echo '<pre>';
                     print_r($this->settings );exit;
            $dsn = 'mysql:dbname='.$this->settings["dbname"].';host='.$this->settings["host"].'';
            try 
            {
                # Read settings from INI file, set UTF8
                $this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

                # We can now log any exceptions on Fatal error. 
                $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                # Disable emulation of prepared statements, use REAL prepared statements instead.
                $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

                # Connection succeeded, set the boolean to true.
                $this->bConnected = true;
            }
            catch (PDOException $e) 
            {
                # Write into log
                echo $this->ExceptionLog($e->getMessage());
                die();
            }
        }

如果我解析配置(settings.ini.php)文件,密码无显示。如果我更改密码,如

[SQL]
host = localhost
user = root
password =Gohead123
dbname = nri_demo

我得到了像

这样的输出
Array
(
    [host] => localhost
    [user] => root
    [password] => Gohead123
    [dbname] => nri_demo
)

我如何解决这个问题?请帮助我。

2 个答案:

答案 0 :(得分:1)

首先 - 您可以从.ini目录中移出public_html文件以使其无法访问,或在.htaccess目录上使用config。这个PHP“hack”不是一个好的选择。

另外,我在PHP文档中发现了一个注释:

Characters ?{}|&~![()^" must not be used anywhere
in the key and have a special meaning in the value.

您可以尝试使用INI_SCANNER_RAW标志,也许它会有所帮助。

BTW,考虑将配置更改为JSON文件。

答案 1 :(得分:1)

尝试将parse_ini_file的第三个参数设为INI_SCANNER_RAW

parse_ini_file('settings.ini.php', false, INI_SCANNER_RAW)

输出:

Array ( [host] => localhost [user] => root [password] => Gohead123$ [dbname] => nri_demo )