2

This is my config load code:

    $WConfig;
    $lines = file($ToRootDirectory . 'config.txt', FILE_SKIP_EMPTY_LINES);
    foreach ($lines as $line_num => $line) {
        $line = trim($line);
        if (!(substr($line, 0, 1) == '#')){
            $WConfig[(trim(substr($line, 0, strpos($line, ":"))))] = trim(substr($line, strpos($line, ':') + 1));
        }
    }
    unset($lines, $line, $line_num, $temp);
    $host = $WConfig['mshost']; //line 11
    print_r($WConfig);          //line 12

It loads this config file: (ANSI)

    #--/ MySQL:             //Dont forget to execute Install.sql ;)
    #      username:        //NOT NEEDED TO BE ROOT -> Acces to INSERT, UPDATE, SELECT, SHOW
    msusername:PHP_Default
    #      password:
    mspassword:php
    #      database:
    msdatabase:PHP_Default
    #      host:
    mshost:localhost
    #--/ Session:
    #       sessionend: Time in minutes when the session will be end from last acces. Default 20 minutes.

sessionend:20

But Displays:

Notice: Undefined index: mshost in C:\######\PHP\LoadConfig.php on line 11
Array ( [msusername] => PHP_Default [mspassword] => php [msdatabase] => PHP_Default [mshost] => localhost [sessionend] => 20 ) 

Line 11 gives a error because he can't find 'mshost' but if I display the array in line 12, 'mshost' still exists.

Who knows the answer of this problem and what do I need to do to fix this problem?

UPDATE: It only appears by msusername and mshost

ANSWER: I have changed msusername and mshost to numbers -> 0 and 1. That works fine.

xlofev276
  • 36
  • 3

1 Answers1

0

Your configuration file looks a lot like INI. (Actually, exactly like INI, except for : instead of =)

I suggest you use PHP's INI loading routines to avoid such headaches.

Your file, in INI format, would look like:

; Use sections if you want
[MySQL]
;--/ MySQL:             //Dont forget to execute Install.sql ;)
;      username:        //NOT NEEDED TO BE ROOT -> Acces to INSERT, UPDATE, SELECT, SHOW
msusername = PHP_Default

;      password:
mspassword = php

;      database:
msdatabase = PHP_Default

;      host:
mshost = localhost

[Sessions]
;--/ Session:
;       sessionend: Time in minutes when the session will be end from last acces. Default 20 minutes.
mishmash
  • 4,422
  • 3
  • 34
  • 56
  • Thank you, I didn't have look at that before. But it remains strange that the variable in the array can not be found. – xlofev276 Mar 28 '13 at 11:34
  • I have changed the code and the config.txt to .ini standards but he cant still not find the variables the appear aggain in the array but can not be found when using $WConfig[] `$WConfig = parse_ini_file($ToRootDirectory . 'config.ini');` `;--/ MySQL: //Dont forget to execute Install.sql ;) ; username: //NOT NEEDED TO BE ROOT -> Acces to INSERT, UPDATE, SELECT, SHOW msusername=PHP_Default ; password: mspassword=php ; database:f msdatabase=loletje ; host: mshost=localhost ... sessionend=20` – xlofev276 Mar 28 '13 at 11:44
  • I edited my post to give you an example of what your file should look like. – mishmash Mar 28 '13 at 12:08
  • What kinds of errors? I've tested it before editing my post and [it worked fine](http://ideone.com/kmUXEs) – mishmash Mar 28 '13 at 13:51
  • I have changed mshost into a number -> 0 and now I have no errors – xlofev276 Mar 28 '13 at 14:11