2017-02-27 21 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

私のために働いて、? –

+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) 

​​として出力の三番目のパラメータを入力して試してください:uが使用しているPHPのバージョン

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

最初に - あなたはそれが到達不能にするかconfigディレクトリに.htaccessを使用するpublic_htmlディレクトリのうち.iniファイルを移動することができます。このPHPの "ハック"は良い選択肢ではありません。

また、私はPHPのドキュメントでノートを発見:

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

あなたは多分それは助ける、​​フラグを使用してみてください。

ところで、変更設定をJSONファイルにすると思います。