0

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.

<?php
session_start();
    /////////////////////////////////////////////////////////// 
    //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);  
    }
    fclose($file);

    $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 ?
thx!

Edited by mike_2000_17: Fixed formatting

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by rolfhorror
0

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

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

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

0

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

eg

$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.
:)

0

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.
:-)

0

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] .

0

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

Edited by rolfhorror: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.