6

I have a config.ini file that looks like this:

[database]
username = user1
password = pass1
dbname = db1

And I want to parse it in PHP, but I'm having no luck. The code I'm trying is:

$inifile = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . "/cfi/config.ini", true);
var_dump($inifile);

The PHP is part of an include, thats why I tried adding $_SERVER['DOCUMENT_ROOT'] to make sure it was finding the config file.

The result of the var_dump is always bool(false).

What am I doing wrong?

EDIT:

I have added the / and it now definitely points to the config file. But there is still a result of bool(false)

Lee
  • 1,485
  • 2
  • 24
  • 44
  • 3
    I think you can not open file. May be you are missing / in $_SERVER['DOCUMENT_ROOT'] . '/' . "cfi/config.ini" – taliezin Mar 05 '15 at 13:14
  • I'm not sure whether keeping a configuration file inside the document root is such a good idea; it would be better to keep it outside. – Ja͢ck Mar 05 '15 at 13:21
  • @Ja͢ck it's not, but it's the way he's doing it. OP should use an absolute path to a non www folder, such as `/home/cfg/config.ini` where `/home/public_html/` is the root of the website. – Martin Mar 05 '15 at 13:22
  • Thanks, I have added the / and updated the question. It didn't fix my problem though :( Jack - the config.ini is in root/cfi/config.ini, is this still bad practice? Where should I put it? - Thanks Martin, just seen your comment, will have a look now – Lee Mar 05 '15 at 13:23
  • 1
    Lee, you will probably need to adjust permissions for the file, get the PHP to output the `fileperms($filename);` value, this will probably tell you that your file is disallowed to be opened or viewed by the non-creator party (ie, PHP). – Martin Mar 05 '15 at 13:25
  • 2
    Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Mar 05 '15 at 13:26
  • hahaha @Fred-ii- yeah, error reporting is always a good idea :) – Martin Mar 05 '15 at 13:27
  • @Martin I hope it helps the OP :-) – Funk Forty Niner Mar 05 '15 at 13:28
  • Thanks for all the comments! OK so I've added the fileperms() code and it has resulted in **33188**. Anyone know what that number means? I'm quite new to PHP so apologies if I'm asking dumb questions – Lee Mar 05 '15 at 13:35
  • @Lee Did you add error reporting and did it return anything? – Funk Forty Niner Mar 05 '15 at 13:36
  • 1
    Those file permissions are `-rw-r--r--`, which should work fine; have you checked the directory permissions leading up to that file as well? Btw, if `false` is returned from that function it should also have shown an error message. – Ja͢ck Mar 05 '15 at 13:38
  • good call @Jack on the directory permissions, but if the file is readable by fileperm then the directory should be readable also, I would have expected. – Martin Mar 05 '15 at 13:40
  • There could be a parse error in the ini file itself of course; but there must be an error message! – Ja͢ck Mar 05 '15 at 13:42
  • I'm trying to keep up with all comments, thanks very much for all the feedback. Fred I tried the **error_reporting(E_ALL); ini_set('display_errors', 1);** line at the top of my php page but then it failed to render - I'll have another go now – Lee Mar 05 '15 at 13:42
  • I too had problems and gave up. I now just use a small, dynamic include file as my configuration file. It's tidier anyway. – www-0av-Com Mar 04 '18 at 13:20

4 Answers4

4

Lee, if your document you are trying to access is in the folder root/cfi/config.ini this looks very much like it is outside the scope of the $_SERVER['Document_Root']; variable - which would look something like:

/home/accountname/websitefolder/

Your ini file sounds like it is located in the folder

/home/accountname/root/cfi/

Which means you will not reach it. You can debug if the address is correct with :

if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/cfi/config.ini")){
print fileperms($_SERVER['DOCUMENT_ROOT'] . "/cfi/config.ini");
}
else {
die("does not exist at this location");
}

The fileperms function will feedback the access permissions for the file which may be preventing PHP reading it, if the file is located in the correct place.

ini file Structure:

Also, You will need to confirm your ini file is the correct structure, the ini file needs to be a valid ini structure like the php.ini file, see http://php.net/manual/en/function.parse-ini-file.php

(edit: for the benefit of BoltClock's edit, I want to retain the above statement regarding the structure of the ini file as this was actually the cause of the OP problem, I have however reworded it. Thank you)

Additional:

Error tracking is vital to finding out why things went wrong: From this post on SO - How to log errors and warnings into a file? - and from the wise advice of Fred-ii- anyone can add the following to the top of their file for some error feedback:

error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");

This will look excessive to those that know, but the above code in order will:

report all errors
display these errors to the browser
log these errors to the log file
set where the log file lives. 

Displaying and logging files isn't needed but I put both in so people can choose their method. Displaying errors to the browser should always be disabled with production servers.

Now, you have a log file with many errors in it, you need your (S)FTP program to look in the folder for it, the folder stated above /tmp/ is NOT in your website domain folder, but in a companion folder, next to it, so you need (S)FTP to access it. Log in, find the folder, and file and download it. Open it with a text editor and it will give you your errors. Delete the file from the server and a new one will be generated when new errors occur.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • This makes a lot more sense. Let's see what the OP has to say. *Fingers crossed* ;-) – Funk Forty Niner Mar 05 '15 at 13:35
  • Thanks Martin, I've made sure the file is in the correct area, like the **/home/accountname/websitefolder/** example you gave. I have tried file_exists and it returned a 1. So I guess that means it found it? – Lee Mar 05 '15 at 13:40
  • ok Lee, next step - did you add error logging to your code? Do it. Then the next step after that, use the `chmod()` function to set the file to something like 0750 with http://php.net/manual/en/function.chmod.php . This will try and force the file to be readable for all. – Martin Mar 05 '15 at 13:42
  • I've never done PHP error logging before, so I added Fred's code `error_reporting(E_ALL); ini_set('display_errors', 1);` to the top of the page and now get a **The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete.** error... – Lee Mar 05 '15 at 13:46
  • ^ Hm... sounds like an endless loop problem. – Funk Forty Niner Mar 05 '15 at 13:48
  • You really need to spend about 20 minutes reading up on Stack Overflow about how to find out and diagnose errors, it will save you a massive amount of ball-ache in the long run. Search SO about how to get error logging on PHP and use your FTP viewing to find and download the error file and read it's wise goodness :) . Also I updated my answer for you!! – Martin Mar 05 '15 at 13:48
  • try here for info: http://stackoverflow.com/questions/3531703/how-to-log-errors-and-warnings-into-a-file – Martin Mar 05 '15 at 13:49
  • It's fixed! I changed the username and password in the config file to something simpler and the parse worked. The original password contained an **@** and a **!**, does this cause a problem when parsing? Thanks for all the help guys, you've been awesome! – Lee Mar 05 '15 at 13:52
  • 1
    @Lee Great, mark this answer as solved ;-) another happy ending. – Funk Forty Niner Mar 05 '15 at 13:53
  • I think those characters would need to be escaped but I don't know too much about parsing ini files to say more. I would still highly recommend sorting out a way of tracking your error logging so you can find these sort of errors in 1/10th of the time :) . But I'm pleased you solved it!! – Martin Mar 05 '15 at 13:54
  • I'll take your advice Martin and have a read up, always happy to learn new stuff. Thats why I'm building this site infact, purely as a learning exercise. Thanks again everyone! – Lee Mar 05 '15 at 13:54
  • 1
    @Fred-ii- a pleasure solving things with you, I have added your advise for error reporting to my answer, error logs should be the first point of call for any question on SO PHP to be honest!! – Martin Mar 05 '15 at 14:04
1

I'm not sure if it's causing your overall issue, but I think DOCUMENT_ROOT does not include a trailing slash, so you'll need to add one to before "cfi".

You can check with:

echo $_SERVER['DOCUMENT_ROOT'] . "cfi/config.ini";
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Dan Walker
  • 434
  • 1
  • 5
  • 15
  • Purging all comments. Please restrict commentary to things that are relevant to the content of the answer; for everything else, there's [meta]. – Shog9 Mar 05 '15 at 19:26
0

I had the same problem with my code. After trying with some failures I noticed a special char ('^') in my password was cause of the error. It was fixed by putting the values inside double quotes ("").

For eg:

[database]
username = "user1"
password = "pass1"
dbname = "db1"

Hope this may help someone like me.

Naseer
  • 11
  • 5
0

My problem was that I has a variable no. Changing variable name solved my problem.

Vitalicus
  • 1,188
  • 13
  • 15