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.
searching a text file
Moderator: General Moderators
Use file() to read the file into an array, then you can compare each row's content agains the given username.
It might need some modification (for example $_POST and such) but I'm tired and need coffee...
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...'; }