www.webdeveloper.com
Results 1 to 10 of 10

Thread: username problem! Please Help!

  1. #1
    Join Date
    Jun 2003
    Posts
    60

    Unhappy username problem! Please Help!

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

    ?>

  2. #2
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    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.

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  3. #3
    Join Date
    Jun 2003
    Posts
    60
    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?

  4. #4
    Join Date
    May 2003
    Posts
    550
    Proabably an easier way of finding each email address.
    Such as beginning and ending each one with a | or something.

  5. #5
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Originally posted by deep
    What other soluution did you have for writing a file in?
    Huh?

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  6. #6
    Join Date
    Jun 2003
    Posts
    60
    I would actually recommend using a different way to delimit the .txt file,
    There , what other way?

    Thanks.

  7. #7
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    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.

  8. #8
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    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...

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  9. #9
    Join Date
    May 2003
    Posts
    550
    *COUGH I said that 5 posts ago

  10. #10
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    oops... Sorry. I never even saw that post.

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles