Click to See Complete Forum and Search --> : username problem! Please Help!


deep
07-01-2003, 08:17 AM
Whats wrong with this code???
I want to check if a username already exists and if it does then write out an error message. Or else add the username to the file specified... ITS NOT WORKING.... PLZ HELP!




<?php

//**
$f = fopen("users/users.txt", "r+");
$user_existz == FALSE;
rewind(f);

while (!feof($f)) {

$l = fgets($f, 30);
print $l . "<p>";

if ($l == $us) {

print "Username is already <b>taken</b>! Please go back and choose a new username!";
$user_existz = TRUE;
break;

}
}

if ($user_existz == FALSE) {

fwrite($f, $us . "\n");
print "Welcome! You are now a registered member!";

}


fclose($f);

?>

pyro
07-01-2003, 08:32 AM
I didn't look through all your code, but this: rewind(f); should be rewind($f);

Anyway, it might be easier to do it like this:

<?PHP
$username_to_test = "user2"; #I used this, as I didn't know how you were obtaining the username to test
$filename = "usernames.txt";
$x = 0;
$contents = file($filename) or die ("Could not open $file");
foreach ($contents as $line) {
if ($username_to_test == $line) {
echo "Username is already <b>taken</b>! Please go back and choose a new username!";
$x = 1;
break;
}
}
if ($x == 0) {
$fp = fopen ($filename, "a");
fwrite ($fp, "\n".$username_to_test);
fclose ($fp);
echo "Welcome! You are now a registered member!";
}

?>That assumbs that each username is on a new line in the .txt file. I would actually recommend using a different way to delimit the .txt file, but wrote it like that, as that is how it looked like your original code would work.

deep
07-01-2003, 09:20 AM
Hi,

Thanks! I dont understand what was wrong in my code... but hay it works now :) .

What other soluution did you have for writing a file in?

brendandonhue
07-01-2003, 09:22 AM
Proabably an easier way of finding each email address.
Such as beginning and ending each one with a | or something.

pyro
07-01-2003, 09:22 AM
Originally posted by deep
What other soluution did you have for writing a file in? Huh? :confused:

deep
07-01-2003, 05:56 PM
I would actually recommend using a different way to delimit the .txt file,

There :), what other way?

Thanks.

Jona
07-01-2003, 10:36 PM
Originally posted by pyro
I would actually recommend using a different way to delimit the .txt file, but wrote it like that, as that is how it looked like your original code would work.

Actually, by using the \n to delimit the file, you can use the nl2br() function to print it out on a page... Although, you could set the contents of the file to a variable and replace the delimited with \n, then use the nl2br() function to print it out... But anyways... :rolleyes:

I'd have given two cents, but I've only got 1.5... :D

[J]ona

pyro
07-01-2003, 11:02 PM
Actually, what I meant was to delimit your file with something other than a newlind (\n). I personally would use something like the pipe ( | ), and put all the usernames on one line...

brendandonhue
07-01-2003, 11:06 PM
*COUGH I said that 5 posts ago :D

pyro
07-01-2003, 11:07 PM
oops... Sorry. I never even saw that post.