PDA

View Full Version : Multiple users login


Alex Piotto
12-03-2002, 11:34 AM
Hi forum friends!
I am trying to build a login script for multiple users.
The code below only works with the last entry in the flat db... why? :(

flat db example:

joe;888
mark;444
mario;222
Alex;111 (only works with this entry....)



login script (preceeded by a common html form):

<?php
$auth = false; // Assume user is not authenticated

if (isset($visitor) && isset($passcode)) {
// Read the entire file into the variable $file_contents
$filename = 'C:\Xitami\webpages\testprotect\tabu\usrspwds.txt';
$fp = fopen( $filename, 'r' );
$file_contents = fread( $fp, filesize( $filename ) );
fclose( $fp );

// Place the individual lines from the file contents into an array.
$lines = explode ("\n", $file_contents);

// Split each of the lines into a username and a password pair and attempt to match them.
foreach ( $lines as $line ) {
list($username, $password) = explode( ';', $line );

if ( ($username == $visitor) && ($password == $passcode) ) {
// A match is found, meaning the user is authenticated. Stop search.
$auth = true;
break;
}
}
}
if (!$auth) {
echo "Authorization Required.";
exit;
}
else {
echo "<P>You are Authorized!</P>";
}
?>

any help will be really appreciated... I am stuck on this...

thanks
:)
Alex

Alex Piotto
12-03-2002, 12:14 PM
I have just descovered that the code works well online, on my server, but on the internal server of my computer the problem is there... and still don't know why.
Probably is a problem of reading the /n in the db file...

alex

firepages
12-03-2002, 04:27 PM
thats probably the issue but can be overcome by using file()

$lines=file($filename);//puts each line of $filename into the lines array//

now if you

trim($line[$x])

each line that loses whitespace & the '\n' OR '\r\n' that *NIX or win32 may use & you should be fine on or offline

Alex Piotto
12-03-2002, 04:38 PM
Hi firepage, thanks for answer :)

Let's see if I understand well... I have to replace

$lines = explode ("\n", $file_contents);

with

$lines=file($filename);
trim($line[$x])

so I can succesfully use the script off and on line... is it right?

Alex :confused:

ConfusedOfLife
12-03-2002, 04:57 PM
Originally posted by firepages
thats probably the issue but can be overcome by using file()

$lines=file($filename);//puts each line of $filename into the lines array//

now if you

trim($line[$x])

each line that loses whitespace & the '\n' OR '\r\n' that *NIX or win32 may use & you should be fine on or offline

Wasn't it fgets that reads line by line?

firepages
12-03-2002, 05:32 PM
<?
//.....
// Place the individual lines from the file contents into an array.
$filename = 'C:\Xitami\webpages\testprotect\tabu\usrspwds.txt';
$lines = file ($filename);

// Split each of the lines into a username and a password pair and attempt to match them.
foreach ( $lines as $line ) {
list($username, $password) = explode( ';', trim($line) );

//etc
?>


not tested but that should do the job,

ConfusedOfLife ,eg: fgets($file_pointer,128); would read from an already open file $fp 128 bytes, so you can get fgets() to read a line at a time if you want but file() is just so easy to use for stuff like this that it seems the best option.

Alex Piotto
12-03-2002, 06:03 PM
OK!
now works on and offline... thank again firepages

all the best for you

ciao
Alex