How can I create automatic registration numbers for users?
Moderator: General Moderators
- prasitc2005
- Forum Commoner
- Posts: 42
- Joined: Thu Jul 13, 2006 7:14 am
How can I create automatic registration numbers for users?
Hi all gurus
I see some websites or forums which users can create their user name and password and the websites automaticly create graphic code to confirm the password. I want to do the same to randomly create user registration numbers through this so it won't start so obviously like abc0001 but a301c5 instead. Can you please recommend any good tutorial? Thanks a lot!
I see some websites or forums which users can create their user name and password and the websites automaticly create graphic code to confirm the password. I want to do the same to randomly create user registration numbers through this so it won't start so obviously like abc0001 but a301c5 instead. Can you please recommend any good tutorial? Thanks a lot!
-
litebearer
- Forum Contributor
- Posts: 194
- Joined: Sat Mar 27, 2004 5:54 am
This function will generate a random string. It uses the number 0-9 and the alphabet (upper and lower) as its choice base. You can set the length of the string being returned (default length is 8, which you can easily change).
Lite...
Code: Select all
<?php
function genRandString($what_len){
if($what_len<1) {$what_len = 8;}
$rand_string = "";
$choice_base = "0123456789bcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$i = 0;
while ($i < $what_len) {
$current_choice = substr($choice_base, mt_rand(0, strlen($choice_base)-1), 1);
if (!strstr($rand_string, $current_choice)) {
$rand_string .= $current_choice;
$i++;
}
}
return $rand_string;
}
$new_string = genRandString($my_seed);
echo $new_string;
?>Lite...
- prasitc2005
- Forum Commoner
- Posts: 42
- Joined: Thu Jul 13, 2006 7:14 am
Thanks a lot! The script is great!
One question though, can I adapt the script for user registration number for 8 digits (combination of 4 uppercase letters and 4 numbers and alphabet always precedes letters )like
MBAT0012
OXZR0057
LLLL8890 - I'm not sure if these codes look good on one's name tag
GGTT0000 - I'm not sure if these codes look good on one's name tag
YYYY1111 - I'm not sure if these codes look good on one's name tag
XXXX0000 - I'm not sure if these codes look good on one's name tag
OR this, even worse
BADY0000
PIMP1111
FU**0000
DAMN0000
YOBO1111
PRIC0000
TWAT1111
SU**0000
LAME1111
These codes will never be repeated each time we activate the script, right?
One question though, can I adapt the script for user registration number for 8 digits (combination of 4 uppercase letters and 4 numbers and alphabet always precedes letters )like
MBAT0012
OXZR0057
LLLL8890 - I'm not sure if these codes look good on one's name tag
GGTT0000 - I'm not sure if these codes look good on one's name tag
YYYY1111 - I'm not sure if these codes look good on one's name tag
XXXX0000 - I'm not sure if these codes look good on one's name tag
OR this, even worse
BADY0000
PIMP1111
FU**0000
DAMN0000
YOBO1111
PRIC0000
TWAT1111
SU**0000
LAME1111
These codes will never be repeated each time we activate the script, right?
- prasitc2005
- Forum Commoner
- Posts: 42
- Joined: Thu Jul 13, 2006 7:14 am
-
litebearer
- Forum Contributor
- Posts: 194
- Joined: Sat Mar 27, 2004 5:54 am
Modified so as to produce 8 character string - first 4 alph, next 4 numbers. Also checks to make sure
string has not been used before.
Lite...
string has not been used before.
Code: Select all
<?php
function genRandString(){
$what_len = 4;
$rand_string1 = "";
$choice_base1 = "0123456789";
$i = 0;
while ($i < $what_len) {
$current_choice1 = substr($choice_base1, mt_rand(0, strlen($choice_base1)-1), 1);
if (!strstr($rand_string1, $current_choice1)) {
$rand_string1 .= $current_choice1;
$i++;
}
}
$rand_string2 = "";
$choice_base2 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$i = 0;
while ($i < $what_len) {
$current_choice2 = substr($choice_base2, mt_rand(0, strlen($choice_base2)-1), 1);
if (!strstr($rand_string2, $current_choice2)) {
$rand_string2 .= $current_choice2;
$i++;
}
}
$rand_string = $rand_string2 . $rand_string1;
return $rand_string;
}
$good_name=FALSE;
// connect to database
// put already taken names into array $taken_names
while(!$good_name) {
$new_string = genRandString();
if(!in_array ($new_string, $taken_names)
$good_name=TRUE;
}
}
// add the new string to the database of taken names
// do whatever else you want with the string
?>Lite...
- prasitc2005
- Forum Commoner
- Posts: 42
- Joined: Thu Jul 13, 2006 7:14 am
-
litebearer
- Forum Contributor
- Posts: 194
- Joined: Sat Mar 27, 2004 5:54 am
Ok... First , I had a couple of typo's (read old man cannot type properly) in the other posts, so here it is functional.
I tried to comment as much as possible for you to follow along.
Hope it helps.
Lite...
Code: Select all
<?php
function genRandString(){
$what_len = 4;
$rand_string1 = "";
$choice_base1 = "0123456789";
$i = 0;
while ($i < $what_len) {
$current_choice1 = substr($choice_base1, mt_rand(0, strlen($choice_base1)-1), 1);
if (!strstr($rand_string1, $current_choice1)) {
$rand_string1 .= $current_choice1;
$i++;
}
}
$rand_string2 = "";
$choice_base2 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$i = 0;
while ($i < $what_len) {
$current_choice2 = substr($choice_base2, mt_rand(0, strlen($choice_base2)-1), 1);
if (!strstr($rand_string2, $current_choice2)) {
$rand_string2 .= $current_choice2;
$i++;
}
}
$rand_string = $rand_string2 . $rand_string1;
return $rand_string;
}
##########################
#
# connect to database
#
##########################
####################################
#
# being just an old hack, I am not sure
# if it would be quicker to keep searching
# through the database OR
# if placing all the taken names in an array
# using only 1 pass through the database
#
# I opted for the array
#
# we are using an example array to test
########################################
$taken_names = array ("abcd1234","WeRq9234", "mmmw12345");
##############################
# set the testing variable
##############################
$good_name=FALSE;
###################################
# set the BREAK OUT variable
###################################
$keep_track = 0;
######################################
# keep looping until a vaild string is found
#
# to keep from looping forever, we use
# the BREAK OUT variable
######################################
while(!$good_name) {
$new_string = genRandString();
if(!in_array ($new_string, $taken_names)) {
$good_name = TRUE;
}
if($keep_track>100000) {
echo "There is some type of problem.<br>So I am terminating this loop";
$good_name = TRUE;
exit();
}
$keep_track = $keep_track + 1;
}
$the_new_name = $new_string;
#############################################
# add the new name to the database of taken names
#
# $query = "INSERT INTO name_tbl (taken_names) VALUES ('$the_new_name')";
# $result = mysql_query($query);
#
############################################
#############################################
# print out the name as a test
#############################################
echo $the_new_name;
?>Hope it helps.
Lite...
- prasitc2005
- Forum Commoner
- Posts: 42
- Joined: Thu Jul 13, 2006 7:14 am
Hi litebearer
Thanks a lot! Great script! I have applied it to my table products and it works fine. It can only insert into the table one by one (it's good for a client when he registers ).
The problem is that I didn't do that before and I'm waiting until the end of registration period and will create the registration number for all users(immature I know). The script won't insert into the table with some rows with existing data, it can only insert into blank ones.

Here's I have applied it to my table:
Thanks a lot! Great script! I have applied it to my table products and it works fine. It can only insert into the table one by one (it's good for a client when he registers ).
The problem is that I didn't do that before and I'm waiting until the end of registration period and will create the registration number for all users(immature I know). The script won't insert into the table with some rows with existing data, it can only insert into blank ones.

Here's I have applied it to my table:
Code: Select all
<?php
function genRandString(){
$what_len = 4;
$rand_string1 = "";
$choice_base1 = "0123456789";
$i = 0;
while ($i < $what_len) {
$current_choice1 = substr($choice_base1, mt_rand(0, strlen($choice_base1)-1), 1);
if (!strstr($rand_string1, $current_choice1)) {
$rand_string1 .= $current_choice1;
$i++;
}
}
$rand_string2 = "";
$choice_base2 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$i = 0;
while ($i < $what_len) {
$current_choice2 = substr($choice_base2, mt_rand(0, strlen($choice_base2)-1), 1);
if (!strstr($rand_string2, $current_choice2)) {
$rand_string2 .= $current_choice2;
$i++;
}
}
$rand_string = $rand_string2 . $rand_string1;
return $rand_string;
}
##########################
#
require_once ('include/db_connection.php');
#
##########################
####################################
#
# being just an old hack, I am not sure
# if it would be quicker to keep searching
# through the database OR
# if placing all the taken names in an array
# using only 1 pass through the database
#
# I opted for the array
#
# we are using an example array to test
########################################
$product_id = array ("abcd1234","WeRq9234", "mmmw12345");
##############################
# set the testing variable
##############################
$good_name=FALSE;
###################################
# set the BREAK OUT variable
###################################
$keep_track = 0;
######################################
# keep looping until a vaild string is found
#
# to keep from looping forever, we use
# the BREAK OUT variable
######################################
while(!$good_name) {
$new_string = genRandString();
if(!in_array ($new_string, $product_id)) {
$good_name = TRUE;
}
if($keep_track>100000) {
echo "There is some type of problem.<br>So I am terminating this loop";
$good_name = TRUE;
exit();
}
$keep_track = $keep_track + 1;
}
$the_new_name = $new_string;
#############################################
# add the new name to the database of taken names
#
$query = "INSERT INTO products (product_id) VALUES ('$the_new_name')";
$result = mysql_query($query);
#
############################################
#############################################
# print out the name as a test
#############################################
echo $the_new_name;
?>-
litebearer
- Forum Contributor
- Posts: 194
- Joined: Sat Mar 27, 2004 5:54 am
for random generation of numbers
feyd | Please use
or
you can also use the follwing code
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]Code: Select all
$a=md5(uniqid(rand(),true)); // will give you a 30 digit noyou can also use the follwing code
Code: Select all
$block = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$block .= "0123456789";
$accno="";
for($i = 0; $i < 6; $i++)
{
$accno .= substr($block,(rand()%(strlen($block))), 1);
}feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]