How can I create automatic registration numbers for users?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
prasitc2005
Forum Commoner
Posts: 42
Joined: Thu Jul 13, 2006 7:14 am

How can I create automatic registration numbers for users?

Post by prasitc2005 »

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!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Search for CAPTCHA...that should give you a start
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

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

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...
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

litebearer: that doesn't create an image though
User avatar
prasitc2005
Forum Commoner
Posts: 42
Joined: Thu Jul 13, 2006 7:14 am

Post by prasitc2005 »

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 :lol:

BADY0000
PIMP1111
FU**0000
DAMN0000
YOBO1111
PRIC0000
TWAT1111
SU**0000
LAME1111

These codes will never be repeated each time we activate the script, right?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's always a potential for repeating when using such a limited result potential.
User avatar
prasitc2005
Forum Commoner
Posts: 42
Joined: Thu Jul 13, 2006 7:14 am

Post by prasitc2005 »

litebearer, how can I apply this function into php insert record into mysql table column called resgistration number? Thanks
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

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.

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...
User avatar
prasitc2005
Forum Commoner
Posts: 42
Joined: Thu Jul 13, 2006 7:14 am

Post by prasitc2005 »

litebearer, thanks a lot!

Could you kindly fill in the example of script on //good names etc. I can't see any result when browse the php script at a moment. Sorry for troubles.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

Ok... First , I had a couple of typo's (read old man cannot type properly) in the other posts, so here it is functional.

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;

?>
I tried to comment as much as possible for you to follow along.

Hope it helps.

Lite...
User avatar
prasitc2005
Forum Commoner
Posts: 42
Joined: Thu Jul 13, 2006 7:14 am

Post by prasitc2005 »

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.

Image


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; 

?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

feyd wrote:There's always a potential for repeating when using such a limited result potential.
There's always a potential for repeated results no matter what. You have to verify things if you want to make sure.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

I think the issue with existing records is that INSERT is for new records, while UPDATE is used to 'change' data in existing records.

Lite...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That's why there's a REPLACE statement too. :)
swaruu
Forum Newbie
Posts: 2
Joined: Thu Aug 17, 2006 10:49 pm

for random generation of numbers

Post by swaruu »

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 no
or
you 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]
Post Reply