Larry Brouwer

... just my personal technology sandbox

  • About
  • Blog
  • Archives
  • Contact

Connect

  • Email
  • Facebook
  • LinkedIn

Powered by Genesis

Archives for January 2012

PHP Random Key Generator

January 10, 2012 By Larry Brouwer Leave a Comment

Yesterday, I needed a way to generate a random alpha-numeric key of various lengths using PHP for a project that I’m currently working on. I didn’t find any built-in functions, so after some Googling, I decided to implement my own version.

[code type=PHP]

private function generateRandomKey($len)

{

$chars = array(‘1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’0’,

‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’,’k’,’l’,’m”

‘n’,’o’,’p’,’q’,’r’,’s’,’t’,’u’,’v’,’w’,’x’,’y’,’z’,

‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’,’K’,’L’,’M”

‘N’,’O’,’P’,’Q’,’R’,’S’,’T’,’U’,’V’,’W’,’X’,’Y’,’Z’);

$min = 0;

$max – sizeof($chars)-1;

$key = “”;

for ($i=0; $i<$len; $i++) {

$key .= $chars[mt_ramd($min, $max)];

}

return $key;

}

[/code]

 

Let me know if anyone has a better suggestion?

Filed Under: Daily Log Tagged With: PHP

Recent Posts

  • Weaver’s Bamboo “Bambusa textilis” clumping bamboo for sale
  • configuring NTP Service on FreeNas, XenServer, and virtualized Windows Server 2012 R2 Domain Controller
  • CentOS 7 Warning: Your Magento folder does not have sufficient write permissions.
  • AOE Scheduler 1.3.0 cron issue with Magento 1.9.2.2

Tag cloud

Outlook Connector Visual Studio OpenCart AWStats Outlook 2007 Azure Windows 7 DIR-655 WordPress SMTP Spam Blogs PST SVN BlogEngine.Net CentOS Database Mail iframe Live Writer Yahoo Mail Magento SSL Scanpst.exe clumping bamboo 301 SBS2011 redirect MySQL Port 25 PHP Log Parser mstsc.exe Comcast Silverlight Security Exchange 2010 TomatoCart .NET Framework Linux web.config Neptune C# Windows Remote Desktop SmarterMail eCommerce