load array from file into variables for login ?
I have an external file with some userpreference called userprefs.txt which contains values on several lines (array values).
In another page I have this code below(part of it), trying to get the $realUser and $realPass values to be parsed from $userPrefs[0] and $userPrefs[1], seems simple enough, but I cant get it to work. I'm a little confused.

    //read file contents into array, each line in a new element
    $filename = "userprefs.txt";
    $userPrefs = array();
    $file = fopen($filename, "r");
    while(!feof($file)) {
        //read file line by line into a new array element
        $userPrefs[] = fgets($file, 4096);  

    $realUser = $userPrefs[0];
    $realPass = $userPrefs[1];  
//if I echo($userPrefs[0]) it displays the value, but when trying to use this to login it fails ...?

//blabla loginscript 
if($realUser == blablabla.......and so on..

All works fine, if I define $realUser and $realPass with strings; $realUser = 'admin' $realPass = 'abc' and I can login. If I echo $realUser I get the filecontents from the array, but not when trying to login with defining it with value from $userPrefs[0].
Is the array read after page has loaded ? and the values not present when post submitting ?

Anyone have any ideas on what's wrong ?

Make sure there are no leading nor trailing blank characters. Try:

$realUser = trim($userPrefs[0]);
$realPass = trim($userPrefs[1]);

thanks, that made it work!! I didnt think the array added garbage...
But the trim() did the job :-)

trim always cleans the data. Expecially if its for a password or user data that needs a check.


$user=" Foo"
$pass="pass "

This data input will fail without trim to clean the unwanted spaces.
Mostly they are not seen but alway do trim exp. when data are comming from form inputs.

I'm aware of the trim, but I never thought the array added garbage, I'm almost 100% sure that the garbage was added by the array since I did not enter anything but the username and password. But thanks for the tip. Using the trim always cleans up.

I didnt think the array added garbage...

there is not "garbage" added "automatically". Most likely the data you are reading are on lines by themselves. Each line ends with a newline character, so when you read it in, that newline character (\n) is also included as part of $userPrefs[0] and $userPrefs[1] .

ok I see, newline characters are being parsed..
thanks for clearing that up.