I am using random codes a lot. I use them for generating API keys, random passwords, email verification links and more. I have a single function written in PHP that I am using over and over again. As it saves me loads of work I thought some of you might be able to use it as well.
Selec All Code:
1 2 3 4 5 6 7 8 9 10 11 12 | function randomCode($length = 10) { $possible = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; $code= '' ; for ($i=0; $i < $length; $i++) { $pos= rand(0,strlen($possible)-1); $code.= substr($possible , $pos, 1); } return $code; } |
It is really easy to use.
If you want to generate a code with the length of 10 simply do it this way.
If you want it any shorter or longer than 10 do it this way
Any feedback is welcome
