dongyuan1160 2018-05-21 18:06
浏览 96
已采纳

PHP parse_ini_file并包括不工作(Ubuntu桌面LAMP)

New here, and PHP's a new language to me. I'm trying to develop a web app. To keep things secure, I'm trying to save my database login credentials in a simple ini file (called db.ini).

1) My system is running Ubuntu 18.04. I've installed LAMP

2) The script, which updates a database via mysqli_connect and mysqli_query, works fine when I have login credentials in the myqli_connect script pasted below. Note, for troubleshooting I'm echoing a message upon successful connection:

$db = 'my_db';
$db_use = 'my_root_user';
$db_pass = 'my_password';
$db_host = 'localhost:3306';

$conn = mysqli_connect($db_host, $db_use, $db_pass, $db);
if(! $conn) 
    {die('Could not connect connect: ' . mysqli_connect_error()) ;
} else
    {echo ("Success");
}

3) I created a db.ini file for security reasons. Though I plan to move it later, I've stored in in the same directory as my .php script: (\var\www\html\my_site).

Here's how the .ini reads

[database]
host = localhost:3306
username = my_root_user
password = my_password
dbname = my_database

3) I have tried to parse this file via the following methods. All have failed at print_r, let alone opening the database, so they're not opening.

parse_ini_file('db.ini') // failed
parse_ini_file('/var/www/html/my_site/db.ini'); // failed

4) I've confirmed that the file exists in my php using the following code.

$filename = 'db.ini';

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

5) Ergo, the import does not appear to be working on my setup (Ubuntu LAMP).

6) On a related not, I tried this as a "require", and saving the variables as db_config.php as well. This did not link up either even though the file exists.

Needless to say, I'm at the end of my rope. Any suggestions? I've a feeling it's a configuration issue, where the script is walled off from communicating with other files on either Apache or my Linux, but have no experience on where to go.

  • 写回答

1条回答 默认 最新

  • dongwu5743 2018-05-21 19:32
    关注

    Try using this one parse_ini_string.

    Code

    $filename = 'db.ini';
    
    if (file_exists($filename)) {
        $db = parse_ini_string(file_get_contents($filename));
        var_dump($db);
    } else {
        echo "The file $filename does not exist";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序