Author Topic: uninque activation code  (Read 710 times)

0 Members and 1 Guest are viewing this topic.

Offline nathanmaxsonadilTopic starter

  • Enthusiast
  • Posts: 305
    • View Profile
uninque activation code
« on: September 17, 2007, 03:00:36 PM »
I was wondering how to have a unique activation code.
I already have a function  that create's the activation code but was wondering how to do a loop so it makes a new activation code if there is already a activation code like that in the database?
<?php

echo 'Hello World!';

?>

Offline cooldude832

  • Fanatic
  • Posts: 4,783
  • PM me if you figure out what this is
    • View Profile
    • ScriptBetter
Re: uninque activation code
« Reply #1 on: September 17, 2007, 03:07:42 PM »
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
Code: [Select]
<?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
Code: [Select]
<?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.
I am willing to help a person that is willing to be helped. 
I admit I don't have all the answers, but I do have all the answer keys.

Current Project:
http://www.scriptbetter.net

Offline arianhojat

  • Enthusiast
  • Posts: 235
    • View Profile
Re: uninque activation code
« Reply #2 on: September 17, 2007, 03:09:38 PM »
possibly php uniqid() func.

for($i=0; $i<10; $i++)
$randNum = uniqid();

u said u make your own, but why not use php's. think it makes a random one based on the time each time its called.

Offline chocopi

  • Devotee
  • Posts: 550
  • Gender: Male
    • View Profile
Re: uninque activation code
« Reply #3 on: September 17, 2007, 03:10:26 PM »
the best thing i find is just to md5($username) but if you really want them to be different you could add a timestamp on the end aswell

~ Chocopi

Offline nathanmaxsonadilTopic starter

  • Enthusiast
  • Posts: 305
    • View Profile
Re: uninque activation code
« Reply #4 on: September 17, 2007, 03:14:12 PM »
like that I'd assume so i'd do something like this next
Code: [Select]
<?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..
<?php

echo 'Hello World!';

?>

Offline cooldude832

  • Fanatic
  • Posts: 4,783
  • PM me if you figure out what this is
    • View Profile
    • ScriptBetter
Re: uninque activation code
« Reply #5 on: September 17, 2007, 03:19:10 PM »
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
Code: [Select]
<?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
« Last Edit: September 17, 2007, 03:21:08 PM by cooldude832 »
I am willing to help a person that is willing to be helped. 
I admit I don't have all the answers, but I do have all the answer keys.

Current Project:
http://www.scriptbetter.net

Offline nathanmaxsonadilTopic starter

  • Enthusiast
  • Posts: 305
    • View Profile
Re: uninque activation code
« Reply #6 on: September 17, 2007, 03:24:54 PM »
it's not a loop so it will only do it one time!!!
Quote
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
}
<?php

echo 'Hello World!';

?>

Offline arianhojat

  • Enthusiast
  • Posts: 235
    • View Profile
Re: uninque activation code
« Reply #7 on: September 17, 2007, 03:25:36 PM »
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.

Offline cooldude832

  • Fanatic
  • Posts: 4,783
  • PM me if you figure out what this is
    • View Profile
    • ScriptBetter
Re: uninque activation code
« Reply #8 on: September 17, 2007, 03:25:57 PM »
its not a loop, but its recalling the parent function it is in there for it will move back up to the top of the function and generate a code.
I am willing to help a person that is willing to be helped. 
I admit I don't have all the answers, but I do have all the answer keys.

Current Project:
http://www.scriptbetter.net

Offline arianhojat

  • Enthusiast
  • Posts: 235
    • View Profile
Re: uninque activation code
« Reply #9 on: September 17, 2007, 03:38:35 PM »
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();