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!
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 Code:
<?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.
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...
I'd have given two cents, but I've only got 1.5...
[J]ona
Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
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...
Bookmarks