2017-02-27 74 views
1

我有一個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 
) 

我怎樣才能解決這個問題的輸出?請幫助我。

+0

工作對我來說,U盤使用的是哪個版本的PHP? –

+0

@Chethan Ameta PHP版本7.0.15 –

+0

嘗試'parse_ini_file($ str,false,INI_SCANNER_RAW)' –

回答

1

嘗試parse_ini_file爲​​

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

輸出的第三個參數:

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

首先 - 你可以搬出.ini文件出來public_html目錄,使其無法到達或config目錄使用.htaccess。這個PHP「黑客」不是一個好的選擇。

而且,我的PHP文件中發現一張紙條:

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

您可以嘗試使用​​標誌,也許會有所幫助。

順便說一句,考慮將配置更改爲JSON文件。