Parsing a File - User Authentication

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Parsing a File - User Authentication

Post 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?
Last edited by romeo on Wed Feb 19, 2003 9:17 am, edited 1 time in total.
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

hmm

Post 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
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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');

?>
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

almost

Post 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?
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

just to be clear

Post 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
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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)
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

alrighty

Post 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 :)
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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.
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

FYI, Thank you, you rule...
Post Reply