SitePoint Sponsor

User Tag List

Results 1 to 23 of 23

Thread: Random username

  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random username

    Hey,

    I am taking users through a registration section on a client website. When the submit button is clicked, i need to create a ransom username for the user and insert it into the database table. I am doing a simple INSERT as so...

    PHP Code:
                    $insert "INSERT INTO tbl_members(username, password, email, gender, postcode, DOB, education, date_added) VALUES 
                    (
    // Insert values here
                    )"
    ;
                    
    $add_member mysql_query($insert); 
    Now the username needs to be a random username, and cannot be the same as any other usernames in the same table..

    How can i do this?

    Regards

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you provide a sample of 5 of these random usernames to show examples format, length, composition?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is unusual, to create random usernames.
    Why do you need it, billy? Every site lets user to choose desired name.

  4. #4
    SitePoint Enthusiast SimonJ621's Avatar
    Join Date
    Mar 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a thought to help you move forward

    One way... to ensure the username is unique, you could select all usernames from your tbl_members table and then ensure your generated username doesn't conflict, if it does, do the process over again with a new username. Below is pseudo code... I'm not sure if it runs as I'm just writing it off the top of my head, but the idea is what counts right

    PHP Code:
    <?php
    // function generate username
    function generateUsername() {
            
    $chars "ABCDEFGHKLMNopqrstuvwxyzOPQRSTWXYZabcdefghijkmn0123456789";
        
    $len 9;
        
    $count strlen($chars);
        while(
    $len--){
            
    $uname .= substr($chars,mt_rand()%$count,1);
        }
        return 
    $uname;
    }

    function 
    compareUsername($uname) {
        
    // select all usernames and compare
        
    $result mysql_query("select all username statement"); 
        while (list(
    $username) = mysql_fetch_row($result)) {
            if (
    strtolower($username) == strtolower($randUsername)) {
                return 
    false;
            }
        }
        return 
    true;
    }

    // initially set the random username
    $randUsername generateUsername();

    // cycle through the compare until you don't conflict
    while (!compareUsername($randUsername)) {
        
    $randUsername generateUsername();
    }
    ?>
    A potential drawback there is making a lot of database calls, but I don't think you will run into the situation where you have the same random name too often. Another way, to avoid ever having a duplicate name, is to do something along these lines...

    1. Setup the unique ID as auto-incrementing
    2. Insert the random generated username (even if it is duplicate)
    3. Then update the username of the last inserted user and append the auto-incremented ID of that row. This ensures it is random.

    Hope that helps you get going...

  5. #5
    SitePoint Enthusiast SimonJ621's Avatar
    Join Date
    Mar 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with Shrapnel, I don't see why you would need the username to be random. You could let them choose a username and then check to ensure it doesn't already exist.

  6. #6
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Simon, don't you think database can handle that search?

  7. #7
    SitePoint Enthusiast SimonJ621's Avatar
    Join Date
    Mar 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I was just splitting hairs. Making the database call and compare is probably the most 'expensive' code in that script, so if someone were very concerned about making an optimally efficient script, it may be something to look at. However, like I said before, it would probably be a very rare case when a random username actually was the same as a previous random username.

  8. #8
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what? Do you mean no need to check at all? Or what?

    Better not to answer at all than answer with such bad practice.

  9. #9
    SitePoint Enthusiast SimonJ621's Avatar
    Join Date
    Mar 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What are you talking about? The above solution would work fine in most cases. If you have a high traffic site and are worried about optimizing, mysql is going to be one of the biggest first steps you take. I don't know what Billy is developing a solution for nor do I know what exactly the random username is going to be. If he wants the username to be 3 characters, then you're going to end up with more duplicates than a username with 20 characters. For this reason, my solution provided an additional possibility if his situation fell into the category of high chance of duplicate unique or required a lot of optimization. There are always more than one way to skin a cat.

    I don't understand your criticism, nor do I don't understand the point of posting a comment like that.

  10. #10
    SitePoint Enthusiast SimonJ621's Avatar
    Join Date
    Mar 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I never said "don't check if the username is unique". My comment was in the context of optimizing. The process of checking the uniqueness of the username is the most resource intensive in the above script. What I said was, since you are most likely not going to be generating a lot of duplicates, it's ok that this code is more resource intensive and therefor you don't need to worry about optimizing it. I never said you don't need to worry about checking the uniqueness.

  11. #11
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. After you took first step, you have to learn to walk.
    Never select all database records in purpose of search.
    Datbase can handle it for you.
    select * from table where field='value'
    will do the thing.

    it i s not optimising. it's abc of database use

  12. #12
    SitePoint Enthusiast SimonJ621's Avatar
    Join Date
    Mar 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that would make the script better, I agree. That's the point of this being a public forum, so suggestions can be made. Why don't you try producing a solution rather than just criticizing.

  13. #13
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // % sp highlight bug workaround

    $s 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

    $len 10$max mt_rand() % ( 62 $len );
    $name substrstr_shuffle$s ), $max$len );

    var_dump$name ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  14. #14
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you try producing a solution rather than just criticizing.
    Because I see no point in the question at all.

  15. #15
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I have seen something like this for events I went to, and usernames were like:
    (Shortened Event/company name)[date mmyy](auto number 3/4/5 digits depending on attendees)

    So if SitePoint was doing an event in Decemeber it may be:
    SP12090001

    You could do something like that.
    Ryan B | My Blog | Twitter

  16. #16
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Everyone,

    Sorry i havent got back to this. Basically the client wants users to be sent a random username, which requires them to check their emails in order for them to register.

    This tries to ensure that the user is genuine.

    Now, i know there is another way whihc i did suggest, but has proven to be no good to my client.

    I suggested that we allow users to select their own username which can be checked against the database to see if it already exists. THEN they would be sent a one time link which ACTIVATES their account. I thought this would be the better option.

    But i have to do the random username way. So i need to know if i can do this. What i want to accomplish is that when a user registers, they are sent a RANDOM username. This would need to check against the database and only send the user a username which does not already exist.

    I think Ebay does this..

    But anyway, can this be done?

    Regards


  17. #17
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I think Simon's post #4 seems to answer the question. I need to try to implement this and will get back to you.

    Regards

  18. #18
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And by the way, Shrapnel_N5, if you dont think there is any point in my question then i would appreciate you not to contribute..

    I simply want to know if such a thing is possible. I have seen it done before, which is why i am asking howe it can be done..

    Regards

  19. #19
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I can't
    If I see someone is going to step into abyss, I can't help it.

    To create a random string is sure possible.
    To search string in database is possible too.
    These operations are wery simple and usual.

    Bot random usernames are nonsense.
    If you describe, why do you need id, there may be another way, proper one. May be you need unique index or smth. Why don't you explain?

  20. #20
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have explained this in post #16.

  21. #21
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems very strange.
    Your proposal is all ok. Everyone does the same. Sitepoint does. I am allowed to choose my username, but I need to confirm by clicking link in email.

    Well what's the problem at all?
    You don't know how to build a random string or how to search string in database?

  22. #22
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know how to search string, i dont know how to build a random string. I also suggested the one time link whihc activates people accounts..

    But my manager persists in using this approach..

    Therefore the logic will be this:-

    1. User registers
    2. They are sent a random username
    3. This username is also stored in the database
    4. The user then checks their emails and logs in using this username
    5. The user will have the opportunity to change their username in their profile section


    This is the logic i need to follow...

  23. #23
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    May be it's aganist spam.
    But I'd prefer CAPTCHA for this.

    Well, for random string, there is one at #13


Bookmarks

Posting Permissions

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