Page 1 of 1

Parsing a File - User Authentication

Posted: Tue Feb 18, 2003 7:10 pm
by romeo
I have

Code: Select all

<?
$username = $uname;
$password = $pword; 
$hostport = $hport; 

$array = file('lpdat'); 
foreach($array as $value)&#123; 
  $temp = explode("::", $value); 
  if($username != $temp&#1111;'0'] || $password != $temp&#1111;'1'] || $hostport != $temp&#1111;'2'])&#123; 
  die('Invalid Username/Password/Port Combination'. "<br>" . 'Please try again'); 
  &#125; 
&#125; 
// user is logged in
?>

but if I put more then 1 line in it (1 set of user::password::host) it doesn't work at all, any help?

hmm

Posted: Wed Feb 19, 2003 9:16 am
by romeo
i know that it is only reading the first line as long as there is no line after it... any and all help appreciated

Posted: Wed Feb 19, 2003 9:41 am
by Stoker
if the user you are trying to log in is on line two, the script will die since it doesnt match line 1.. if the user logging in is on line 1 (and there is a line 2), the user will be "verified", but then the script goes on and tries to verify him with line 2 and dies..

Code: Select all

<?php

  $userstring = $_POST['user'].'::'.$_POST['pass'].'::'.$_SERVER['HTTP_PORT'];
  $valid_user = file('my_secret_file');

  if (!in_array($userstring,$valid_user)) die ('Simon says your not worthy');

?>

almost

Posted: Wed Feb 19, 2003 10:24 am
by romeo
I think I am having a problem with it reading line returns

i.e. any line with a line under it doesn't authenticate... how would one trim this?

just to be clear

Posted: Wed Feb 19, 2003 10:42 am
by romeo
I am ytryuing ot take your advise but no userstring except the last in the file is authenticating.. i am trying this now..

Code: Select all

$userstring = "$username".'::'."$password".'::'."$hostport"; 
$valid_user = file('lpdat'); 


$userstring = ereg_replace ("\n", "", $userstring);
$userstring = ereg_replace ("\r", "", $userstring);

if (!in_array($userstring,$valid_user)) die('Invalid Username/Password/Port Combination'. "<br>" . 'Please try again');
Any and all help is appreciated

Posted: Wed Feb 19, 2003 10:50 am
by Stoker
NEVER use ereg for anything!

perhaps the strings in file include line endings? try adding thisline after file()

foreach ($valid_user as $idx => $vu) $valid_user[$idx] = trim($vu);

(and delete all the ereg stuff)

alrighty

Posted: Wed Feb 19, 2003 10:55 am
by romeo
Working so far...

Can you please explain to me what that does...

and why never use ereg?

I'm by no means a PHP Pro but I am lost :)

Posted: Wed Feb 19, 2003 11:03 am
by Stoker
ereg uses the POSIX regex engine which uses a lot of resources and is very slow, if you need the power of regex, use preg.. but any such simple replacement like that doesnt reed regex, there is plenty of other string functions that can do such things, e.g. str_replace

the foreach loop goes thru every record in the array, and $idx becomes the array index and $vu the string, then it re-assigns a trim()'ed version (which takes away whitespace on either end) to the same position in the array.

Posted: Wed Feb 19, 2003 12:31 pm
by romeo
FYI, Thank you, you rule...