Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
why do you use a text file at first place ? it is less secure than using a php file directly. –  mpm Mar 28 '13 at 10:42
    
Because a text file is easier to change if the php code is going bigger –  user2219231 Mar 28 '13 at 10:44
    
Or you could use a simple config file like json, ini or yml, which have a decode function already included in php. –  Cyril N. Mar 28 '13 at 11:01

1 Answer 1

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.
share|improve this answer
    
Thank you, I didn't have look at that before. But it remains strange that the variable in the array can not be found. –  user2219231 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 –  user2219231 Mar 28 '13 at 11:44
    
I edited my post to give you an example of what your file should look like. –  vanneto Mar 28 '13 at 12:08
    
I have copied your config.ini but the errors remain –  user2219231 Mar 28 '13 at 13:33
    
What kinds of errors? I've tested it before editing my post and it worked fine –  vanneto Mar 28 '13 at 13:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.