DBookatay Posted October 16, 2007 Share Posted October 16, 2007 As I mentioned in previous posts, I am working on a site for my upcoming wedding, where family members can login and RSVP, choose their dinner meal selections, send reception song requests, and so on. I have a working login page (http://www.cindyandbrad.com/index.php?category=002&sub=101), where information (username & password) gets pulled from the table 'authuser', and if all matches takes them to a secure page, (http://www.cindyandbrad.com/passed.php). I have a separate page where an admin (me) adds guests, and certain values into the table. Problem is... I want the usernames to be the guests last names, and since its a lot of family on the list there are going to be multiple members with the same username. So I want to add a 4 or 5 digit random number to the name, as long as the number is only used once. So the last name "Smith" will become username "Smith0654" Heres my code now: // Add New Guest to List if (isset($_POST['per1_sir'])) { $per1_nmF = ucfirst($_POST['per1_nmF']); $per1_nmM = ucfirst($_POST['per1_nmM']); $per1_nmL = ucfirst($_POST['per1_nmL']); $per2_nmF = ucfirst($_POST['per2_nmF']); $per2_nmM = ucfirst($_POST['per2_nmM']); $per2_nmL = ucfirst($_POST['per2_nmL']); $uname = ucfirst($_POST['per1_nmL']).RANDOM_NUMBER_GOES_HERE; mysql_query("INSERT INTO authuser ( per1_sir, per1_nmF, per1_nmM, per1_nmL, per2_sir, per2_nmF, per2_nmM, per2_nmL, app, salad, status, level) VALUES ('$per1_sir', '$per1_nmF', '$per1_nmM', '$per1_nmL', '$per2_sir', '$per2_nmF', '$per2_nmM', '$per2_nmL', '2', '2', 'active', '2')")or die(mysql_error()); echo "<meta http-equiv=\"Refresh\" content=\"0; url=add.php?body=Added\">";} Does anyone have any ideas as to how this is done... Quote Link to comment https://forums.phpfreaks.com/topic/73515-creating-a-randomly-genterated-username/ Share on other sites More sharing options...
kpesanka Posted October 16, 2007 Share Posted October 16, 2007 If you are using the 4 or 5 digit number solely to prevent duplicate usernames, why not just increment the number after their username (smith1, smith2, smith3), etc? Is there a reason is has to be a 4 or 5 digit random number? Quote Link to comment https://forums.phpfreaks.com/topic/73515-creating-a-randomly-genterated-username/#findComment-370866 Share on other sites More sharing options...
DBookatay Posted October 16, 2007 Author Share Posted October 16, 2007 Because on the add form that I use as admin, I dont choose the username, I want to have it automatically created. Since both myself and my fience will be adding names to our list. Quote Link to comment https://forums.phpfreaks.com/topic/73515-creating-a-randomly-genterated-username/#findComment-370872 Share on other sites More sharing options...
kpesanka Posted October 16, 2007 Share Posted October 16, 2007 Then I would use this strategy: 1. Write a function to generate a random 4 or 5 digit number. 2. Before your code does the mysql insert, query the database first using 'LIKE username%' 3. If there are no results, the username is fine as is. 4. If there are results, you can proceed in one of two ways. The easier path is to generate a number using your function and append it to the username and try the query - assuming you have the database table set up such that username must be unique, your query will either fail or not. If it works, then you know your new username is not a duplicate. If it doesn't, just run it again with a new username. We're only talking about a few hundred guests (my assumption) and even if one side of the family has a lot of people with the same last name, collisions should be fairly infrequent (again, i'm not a stats guy, so I could be wrong) since you're using a 5 digit number with 100,000 possible combinations. Quote Link to comment https://forums.phpfreaks.com/topic/73515-creating-a-randomly-genterated-username/#findComment-370877 Share on other sites More sharing options...
DBookatay Posted October 16, 2007 Author Share Posted October 16, 2007 Then I would use this strategy: 1. Write a function to generate a random 4 or 5 digit number. This is what I am trying to get help with... Quote Link to comment https://forums.phpfreaks.com/topic/73515-creating-a-randomly-genterated-username/#findComment-370880 Share on other sites More sharing options...
kpesanka Posted October 16, 2007 Share Posted October 16, 2007 http://php.about.com/od/phpfunctions/g/rand_random_num.htm First google result - should do what you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/73515-creating-a-randomly-genterated-username/#findComment-370882 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.