chris_rulez001 Posted August 8, 2007 Share Posted August 8, 2007 hi, in php, how can you make a random code and then changes each time you submit a form? by a random code i mean: 1hak723isba92821k9 the random code has to be 18 characters long. Link to comment https://forums.phpfreaks.com/topic/63949-random-lettering-question/ Share on other sites More sharing options...
lemmin Posted August 8, 2007 Share Posted August 8, 2007 You can use the rand() function and specify a minimum and a maximum making the range in the characters you want. If you want lower case letters, you can use: for ($i=0;$i<18;$i++) echo chr(rand(97, 122)); If you want more than just lower case letters, mess around with the min and max. Link to comment https://forums.phpfreaks.com/topic/63949-random-lettering-question/#findComment-318754 Share on other sites More sharing options...
chris_rulez001 Posted August 8, 2007 Author Share Posted August 8, 2007 You can use the rand() function and specify a minimum and a maximum making the range in the characters you want. If you want lower case letters, you can use: for ($i=0;$i<18;$i++) echo chr(rand(97, 122)); If you want more than just lower case letters, mess around with the min and max. thanks Link to comment https://forums.phpfreaks.com/topic/63949-random-lettering-question/#findComment-318758 Share on other sites More sharing options...
Orio Posted August 8, 2007 Share Posted August 8, 2007 There are various ways doing that, I think I would use: <?php $random = str_shuffle("abcdefghijklmnopqrstuvwxyz1234567890"); $random = substr($random, 0, 18); ?> Orio. Link to comment https://forums.phpfreaks.com/topic/63949-random-lettering-question/#findComment-318759 Share on other sites More sharing options...
cooldude832 Posted August 8, 2007 Share Posted August 8, 2007 Generate a random number in the ASCII translation area of your choice and apply it to the chr() function http://us3.php.net/manual/en/function.chr.php i.e <?php $codelength = 15; $i = 1; $string = ""; while($i<=$codelength){ $type = rand(1,3); switch ($type){ //Number case 1: $string .= chr(rand(48,57)); break; //Upper Case Letter case 2: $string .= chr(rand(65,90)); break; //Lower Case Letter default: $string .= chr(rand(97,122)); } $i++; } //$string is now a 15 character long random combo of numbers, uppercase, lowercase letters/numbers echo $string; Link to comment https://forums.phpfreaks.com/topic/63949-random-lettering-question/#findComment-318760 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.