Jump to content

Creating a randomly genterated username


DBookatay

Recommended Posts

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.