nathanmaxsonadil Posted September 17, 2007 Share Posted September 17, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/69658-uninque-activation-code/ Share on other sites More sharing options...
cooldude832 Posted September 17, 2007 Share Posted September 17, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69658-uninque-activation-code/#findComment-349997 Share on other sites More sharing options...
arianhojat Posted September 17, 2007 Share Posted September 17, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69658-uninque-activation-code/#findComment-349998 Share on other sites More sharing options...
chocopi Posted September 17, 2007 Share Posted September 17, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69658-uninque-activation-code/#findComment-350000 Share on other sites More sharing options...
nathanmaxsonadil Posted September 17, 2007 Author Share Posted September 17, 2007 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.. Quote Link to comment https://forums.phpfreaks.com/topic/69658-uninque-activation-code/#findComment-350004 Share on other sites More sharing options...
cooldude832 Posted September 17, 2007 Share Posted September 17, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69658-uninque-activation-code/#findComment-350008 Share on other sites More sharing options...
nathanmaxsonadil Posted September 17, 2007 Author Share Posted September 17, 2007 it's not a loop so it will only do it one time!!! 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 } Quote Link to comment https://forums.phpfreaks.com/topic/69658-uninque-activation-code/#findComment-350016 Share on other sites More sharing options...
arianhojat Posted September 17, 2007 Share Posted September 17, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69658-uninque-activation-code/#findComment-350018 Share on other sites More sharing options...
cooldude832 Posted September 17, 2007 Share Posted September 17, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69658-uninque-activation-code/#findComment-350019 Share on other sites More sharing options...
arianhojat Posted September 17, 2007 Share Posted September 17, 2007 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(); Quote Link to comment https://forums.phpfreaks.com/topic/69658-uninque-activation-code/#findComment-350037 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.