DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

[PHP] Create A Random String

12.10.2006
| 21832 views |
  • submit to reddit
        // create a random string of 16 chars


$len = 16;
$base='ABCDEFGHKLMNOPQRSTWXYZabcdefghjkmnpqrstwxyz123456789';
$max=strlen($base)-1;
$activatecode='';
mt_srand((double)microtime()*1000000);
while (strlen($activatecode)<$len+1)
  $activatecode.=$base{mt_rand(0,$max)};
  
echo $activatecode;