Jump to content

uninque activation code


nathanmaxsonadil

Recommended Posts

Lets assume this activation code is >10 alphanumerical characters to start, if its less make it longer it will seriously help.

You have something like

<?php
function make_code (){
$code = "";
$i = 0;
while($i <=10){
$code .= chr(rand(97,122));
$i++;
}
return $code;
}
$code = make_code();
?>

 

Now you got something like that I'd assume so i'd do something like this next

<?php
//connectsql
function code_test(){
$q = "select from `table` where Code = '".$code."'";
$r = mysql_query($q) or die(mysql_error());
if(mysql_num_rows($r){
//We found a match lets re roll code
$code = make_code();
code_test();
}
else{
//valid unused code lets return out of the function
}
}
?>

 

 

Since the code is 10 characters + long the odds of matching are so low (unless you have a billion+ of em) that its okay to test it because odds are you will never match.  This also gives you a non linear sequence to codes which will be helpful.

Link to comment
Share on other sites

like that I'd assume so i'd do something like this next

<?php
//connectsql
function code_test(){
$q = "select from `table` where Code = '".$code."'";
$r = mysql_query($q) or die(mysql_error());
if(mysql_num_rows($r){
//We found a match lets re roll code
$code = make_code();
code_test();
}
else{
//valid unused code lets return out of the function
}
}
?>

this would work but if there were duplicates for the 1'st time and 2'cnd time it would only check for the first time..

Link to comment
Share on other sites

Nope because the function will recall its self, thus testing with the newly generated code value each time until it finally resolves out of the function by getting to the else.  Actually what needs to happen is this instead because it will not initialize code properly

<?php
function make_code (){
$code = "";
$i = 0;
while($i <=10){
$code .= chr(rand(97,122));
$i++;
}
return $code;
}
function code_test(){
$code = make_code();
$q = "select from `table` where Code = '".$code."'";
$r = mysql_query($q) or die(mysql_error());
if(mysql_num_rows($r){
//We found a match lets re roll code
code_test();
}
else{
//valid unused code lets return out of the function
}
}
//Connect to your sql db
code_test();
?>

 

 

However the generator done by uniqid(); is not a bad choice, but if you want to control what goes in your code (Some people don't like the number 1 or the letter "I" or "L" because of possible confusion so they remove them from the random bank.  I wrote a very basic code generator that only generates alpha characters you can redo it and make it generate all sorts of fun things

Link to comment
Share on other sites

i dont think u need to search your database for unique activation code.

Make random timestamp and then encrypt in as password in database.

u can make random without checking database with :

 

$tempActivationpassword = md5( uniqid(rand(), true) ); //makes strong unique randompassword then md5's it so actual activation 'password' code isnt stored in database.

or

$tempActivationpassword = md5( microtime() ); //kinda like previous user said.

Link to comment
Share on other sites

if u want to do it the recursive way, u should return something in the 'else' right (been a while since i did recursion)? and dont call the function test.

it will recursively return the code to the '1st' function that called it;

 

function get_unique_activ_code(){

$code = make_code();

$q = "select from `table` where Code = '".$code."'";

$r = mysql_query($q) or die(mysql_error());

if(mysql_num_rows($r){

//We found a match lets re roll code

$anothercode = get_unique_activ_code();

return $anothercode;

}

else{

return $code;

}

}

 

$unique = get_unique_activ_code();

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.