Page 1 of 1

searching a text file

Posted: Wed Aug 13, 2003 11:42 am
by sulen
Hi ,
I need to search a text file .htpasswd for the existence of user names entered by users on a form so that I can prompt them that their username already exists.

The script needs to search this file and return true if the user name.

Please help.

Posted: Wed Aug 13, 2003 3:23 pm
by JAM
Use file() to read the file into an array, then you can compare each row's content agains the given username.

Code: Select all

function userexists($formuser) {
  // read the file into array
 $data = file('.htpasswd');
  // loop...
  for ($i=0;$i<count($data);$i++) {
    // break it up in pieces (interesting uses)
    list($user,$pass,$uid,$gid,$gecos,$home,$shell) = explode(":",$data[$i]);
    // note === for exakt match...
    if ($user === $formuser) { return 1; }
  }
  return 0;
}
// Usage:
if (userexists('name')) { echo 'Allready in the file'; }
else { echo 'Not filed...'; }
It might need some modification (for example $_POST and such) but I'm tired and need coffee...