473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to increment Usernames so they are unique

I'm wanting to increment usernames in ONLY the lowercase a-z for example if a user enters "abc" it will automatically be given "abc1" and the next user if entering the same username will automatically be given "abc2", the next "abc3" and so on. I'm trying to establish a registration as simple as possible, meaning whatever username is inserted it will be accepted but with a different integer added to the end. With that system it would not matter if the username exists already because each similar username will have a different number, making the entire username unique. If the username does not already exits then the number 1 to be added. The base login and register script I'm using can be found on http://evolt.org/node/60265/ The database.php and register.php pages are below, but excluding the amendments I am seeking. I've been advised to create a MySQL database with 2 tables, 1 table named 'Users' with 3 columns named (1) 'userid' primary key, auto increment, (2) 'username' (3) 'password'. The other table is named 'UserCount' with 2 columns named (1) 'username' (2) 'count'. I've also been advised the above can be achieved in 1 table, which is preferred, so I'm open to suggestions.

Can someone provide the PHP coding for the following process? When a new user registers a new username firstly PHP to check the characters are in lowercase a-z only and no other characters, and delete any foreign characters if they do not match, then for PHP to check to see if that username exists in the 'UserCount' table. If it doesn't exist, PHP to insert the new username into the 'username' column and also set the value of the 'count' column to 1. If the username does exist, PHP to add that username to the 'username' column and increment the value of the 'count' column by 1. Then in the 'Users' table PHP to insert a record with 'Users.username = UserCount.username + count' and PHP to inform the new user of their complete username including the integer that has been added to their first entered username. I also tend to think the search in the database should start at the bottom and find up, but stand to be corrected.

It would also help if I could be provided a link to a website on where I can better understand how to collate coding of this kind, coz I have tried to formulate this coding but as I'm playing around in the dark I have no idea if it is the right process in particular considering security factors.

database.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /**
  4.  * Connect to the mysql database.
  5.  */
  6. $conn = mysql_connect("localhost", "your_username", "your_password") or die(mysql_error());
  7. mysql_select_db('your_database', $conn) or die(mysql_error());
  8.  
  9. ?>
  10.  
register.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start(); 
  3. include("database.php");
  4.  
  5. /**
  6. * Returns true if the username has been taken
  7. * by another user, false otherwise.
  8. */
  9. function usernameTaken($username){
  10. global $conn;
  11. if(!get_magic_quotes_gpc()){
  12. $username = addslashes($username);
  13. }
  14. $q = "select username from Users where username = '$username'";
  15. $result = mysql_query($q,$conn);
  16. return (mysql_numrows($result) > 0);
  17. }
  18.  
  19. /**
  20. * Inserts the given (username, password) pair
  21. * into the database. Returns true on success,
  22. * false otherwise.
  23. */
  24. function addNewUser($username, $password){
  25. global $conn;
  26. $q = "INSERT INTO Users VALUES ('$username', '$password')";
  27. return mysql_query($q,$conn);
  28. }
  29.  
  30. /**
  31. * Displays the appropriate message to the user
  32. * after the registration attempt. It displays a 
  33. * success or failure status depending on a
  34. * session variable set during registration.
  35. */
  36. function displayStatus(){
  37. $uname = $_SESSION['reguname'];
  38. if($_SESSION['regresult']){
  39. ?>
  40.  
  41. <h1>Registered!</h1>
  42. <p>Thank you <b><?php echo $uname; ?></b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>
  43.  
  44. <?php
  45. }
  46. else{
  47. ?>
  48.  
  49. <h1>Registration Failed</h1>
  50. <p>We're sorry, but an error has occurred and your registration for the username <b><?php echo $uname; ?></b>, could not be completed.<br>
  51. Please try again at a later time.</p>
  52.  
  53. <?php
  54. }
  55. unset($_SESSION['reguname']);
  56. unset($_SESSION['registered']);
  57. unset($_SESSION['regresult']);
  58. }
  59.  
  60. if(isset($_SESSION['registered'])){
  61. /**
  62. * This is the page that will be displayed after the
  63. * registration has been attempted.
  64. */
  65. ?>
  66.  
  67. <html>
  68. <title>Registration Page</title>
  69. <body>
  70.  
  71. <?php displayStatus(); ?>
  72.  
  73. </body>
  74. </html>
  75.  
  76. <?php
  77. return;
  78. }
  79.  
  80. /**
  81. * Determines whether or not to show to sign-up form
  82. * based on whether the form has been submitted, if it
  83. * has, check the database for consistency and create
  84. * the new account.
  85. */
  86. if(isset($_POST['subjoin'])){
  87. /* Make sure all fields were entered */
  88. if(!$_POST['user'] || !$_POST['pass']){
  89. die('You didn\'t fill in a required field.');
  90. }
  91.  
  92. /* Spruce up username, check length */
  93. $_POST['user'] = trim($_POST['user']);
  94. if(strlen($_POST['user']) > 30){
  95. die("Sorry, the username is longer than 30 characters, please shorten it.");
  96. }
  97.  
  98. /* Check if username is already in use */
  99. if(usernameTaken($_POST['user'])){
  100. $use = $_POST['user'];
  101. die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
  102. }
  103.  
  104. /* Add the new account to the database */
  105. $md5pass = md5($_POST['pass']);
  106. $_SESSION['reguname'] = $_POST['user'];
  107. $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
  108. $_SESSION['registered'] = true;
  109. echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
  110. return;
  111. }
  112. else{
  113. /**
  114. * This is the page with the sign-up form, the names
  115. * of the input fields are important and should not
  116. * be changed.
  117. */
  118. ?>
  119.  
  120. <html>
  121. <title>Registration Page</title>
  122. <body>
  123. <h1>Register</h1>
  124. <form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
  125. <table align="left" border="0" cellspacing="0" cellpadding="3">
  126. <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
  127. <tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
  128. <tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
  129. </table>
  130. </form>
  131. </body>
  132. </html>
  133.  
  134.  
  135. <?php
  136. }
  137. ?>
  138.  
  139.  
Oct 9 '10 #1
1 4150
kovik
1,044 Expert 1GB
When users register for a username, they generally don't simply accept their username with a 1 at the end of it. When a registration system wants each user to have a unique username, they check if the name exists in the database and, if it does, they inform the user and allow them to change their desired username. Forcing a name upon a user isn't a good idea in my opinion.

But if you insist...

If a user enters a name, check for it in the database. If that name exists, change it to the name with a 1 and check for it in the database. If that name exists, change it to the name with a 2 and check for it in the database. And keep going until you find an available name.

Expand|Select|Wrap|Line Numbers
  1. $final_username = $username;
  2. $result = mysql_query("select * from users where username='$username'");
  3.  
  4. for ($i = 1; mysql_num_rows($result); $i++) {
  5.     $final_username = $username . $i;
  6.     $result = mysql_query("select * from users where username='$final_username'");
  7. }
  8.  
  9. $username = $final_username;
Oct 9 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
2
by: Tom | last post by:
I am trying to store information into a table that has an auto increment field. There is currently no data in the table. Using the code below I cannot insert data into the table. I get an error...
13
by: kailasam | last post by:
Hello, Iam having a doubt. Which is more efficient post increment or Pre increment? I have read that preincrement is efficient than Post increment. Iam not able to think how it is? For an...
3
by: George Ter-Saakov | last post by:
What is the purpose of having Interlocked.Increment if it does not work with variable declared as volatile. Here is my problem, Interlocked.Increment increments the variable in thread safe...
2
by: john | last post by:
Is it true that if I split my access database in backend and frontend and I implement custom auto increment for the ID fields, that my database is ready to be used in a multi-user environment? I...
15
by: Ryan Liu | last post by:
Hi, Is there any known bug related to Interlocked.Increment(ref var)? My client report var's value going up and down in the client/server multile-thread application. There are about 80...
11
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
0
chumlyumly
by: chumlyumly | last post by:
Hello scripters - OS: Mac OSX Language: PHP w/ MySQL database I've created an insert page where a user inputs his info, which then goes to four different tables in a MySQL database. The...
8
by: bintom | last post by:
Why doe the following C++ code behaves as it does? int i; i=1; cout << (++i)++; Output: 2 i=1; cout << ++(++i); Output: 3 i=1; cout << (i++)++; Output: Error. LValue required...
1
by: Justsomeguy | last post by:
I want to able to ban usernames on my site. I've got an interface for site admins to use and i've set up a database already. I don't want to ban IP addresses because there are ways around that. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.