dadamssg Posted March 9, 2009 Share Posted March 9, 2009 im looking for a way to produce a random string of numbers and letters to use in a confirm membership script. what php function could i use to accomplish this? im going to put the random string in my db and then send it through a url through an email. When they click on it im going to write a script that will check to see if the variable in the url match with the db and confirm them if so. thanks Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/ Share on other sites More sharing options...
Mark Baker Posted March 9, 2009 Share Posted March 9, 2009 Either rand() or the better mt_rand() Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780742 Share on other sites More sharing options...
Mchl Posted March 9, 2009 Share Posted March 9, 2009 feed md5 with rand Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780744 Share on other sites More sharing options...
premiso Posted March 9, 2009 Share Posted March 9, 2009 Use an md5 or sha1 hash for that. Makes it secure and is easy to generate. IE Use their username with a random number: $verify = sha1($username . rand(11111111,99999999); Add that to the db then send it to them in an email. Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780745 Share on other sites More sharing options...
dadamssg Posted March 9, 2009 Author Share Posted March 9, 2009 so like md5(mt_rand(1,10000)) would do it? Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780746 Share on other sites More sharing options...
dadamssg Posted March 9, 2009 Author Share Posted March 9, 2009 oh ok awesome thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780748 Share on other sites More sharing options...
redarrow Posted March 9, 2009 Share Posted March 9, 2009 premiso winner lol. sorry but it much more secure. my version adapted theo. <?php $username=str_shuffle('redarrow'); $verify = sha1($username . rand(11111111,99999999)); echo $verify; ?> Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780751 Share on other sites More sharing options...
Mchl Posted March 9, 2009 Share Posted March 9, 2009 I wonder just what makes it more secure? Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780757 Share on other sites More sharing options...
gizmola Posted March 9, 2009 Share Posted March 9, 2009 im looking for a way to produce a random string of numbers and letters to use in a confirm membership script. what php function could i use to accomplish this? im going to put the random string in my db and then send it through a url through an email. When they click on it im going to write a script that will check to see if the variable in the url match with the db and confirm them if so. thanks I wrote one of these recently, and there was very little involved. My code did check in the database to make sure that the string hadn't been allocated previously, but that is something you will have to code for yourself. This simple function does the work of generating a random string. I also didn't want the first character to be a zero, so I have a little code to prevent that, but you could obviously omit that if you wanted. As you can see, it's pretty simple stuff and just does a few ascii lookups in combination with rand(). This is nothing you couldn't write yourself or modify to your liking. For my purposes i wanted a string that had numbers and letters. public function generateInviteCode($charnum = { $invitecode = null; $startrange = 1; for ($x = 0; $x $rand = rand(0, 61); if ($rand $invitecode .= $rand; else if ($rand $invitecode .= chr($rand + 55); } else { $invitecode .= chr($rand + 61); } } // on first character, don't allow a zero if ($invitecode[0] == '0') $invitecode[0] = rand(1, 9); return $invitecode; } Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780759 Share on other sites More sharing options...
gizmola Posted March 9, 2009 Share Posted March 9, 2009 BTW, you might ask why not use the SHA or MD5 versions above. If you want to take that approach I have no complaint with doing so but it does have these drawbacks. -Those hash functions are computationally expensive. -The hashes themselves are very long (36 or 40 characters depending on which you choose). If you want something shorter, you might want to consider the approach I outlined. The function can actually create whatever size string you want, but you could start producing collisions if it was too short. 6-8 characters provides a nice compromise. Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780763 Share on other sites More sharing options...
dadamssg Posted March 9, 2009 Author Share Posted March 9, 2009 sweet thanks, does anyone know how many characters sha1 outputs? i wanna know the varchar() i need to setup in my db. its either 20 or 40, can't really decipher the php manual.... Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780765 Share on other sites More sharing options...
premiso Posted March 9, 2009 Share Posted March 9, 2009 I wonder just what makes it more secure? Nothing....just another way to do it A varchar of 40 should be fine for SHA1 Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780766 Share on other sites More sharing options...
dadamssg Posted March 10, 2009 Author Share Posted March 10, 2009 awesome thanks Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780768 Share on other sites More sharing options...
Mchl Posted March 10, 2009 Share Posted March 10, 2009 I wonder just what makes it more secure? Nothing....just another way to do it Exactly. Security has nothing to do here. A varchar of 40 should be fine for SHA1 char(40) would actualy be better, since we know all strings will have exactly 40 characters. This will save a byte or two per row. Quote Link to comment https://forums.phpfreaks.com/topic/148681-solved-random-number-generator/#findComment-780957 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.