Page 1 of 1

How can I create automatic registration numbers for users?

Posted: Wed Aug 16, 2006 6:35 am
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!

Posted: Wed Aug 16, 2006 6:43 am
by JayBird
Search for CAPTCHA...that should give you a start

Posted: Wed Aug 16, 2006 7:29 am
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...

Posted: Wed Aug 16, 2006 7:57 am
by JayBird
litebearer: that doesn't create an image though

Posted: Wed Aug 16, 2006 8:15 am
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?

Posted: Wed Aug 16, 2006 8:26 am
by feyd
There's always a potential for repeating when using such a limited result potential.

Posted: Wed Aug 16, 2006 11:02 am
by prasitc2005
litebearer, how can I apply this function into php insert record into mysql table column called resgistration number? Thanks

Posted: Wed Aug 16, 2006 11:15 am
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...

Posted: Wed Aug 16, 2006 11:24 am
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.

Posted: Wed Aug 16, 2006 6:45 pm
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...

Posted: Thu Aug 17, 2006 12:43 am
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; 

?>

Posted: Thu Aug 17, 2006 3:22 am
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.

Posted: Thu Aug 17, 2006 12:33 pm
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...

Posted: Thu Aug 17, 2006 12:56 pm
by feyd
That's why there's a REPLACE statement too. :)

for random generation of numbers

Posted: Thu Aug 17, 2006 11:57 pm
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]