Larry Brouwer

... just my personal technology sandbox

  • About
  • Blog
  • Archives
  • Contact

Connect

  • Email
  • Facebook
  • LinkedIn

Powered by Genesis

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

Leave a Reply

You must be logged in to post a comment.

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

D-Link SSMS Windows 7 CentOS iframe AWStats PST Scanpst.exe DIR-655 Visual Studio SSL Google Analytics web.config TomatoCart Comcast BlogEngine.Net eCommerce Live Writer IIS7 Port 25 redirect WordPress Tools Outlook 2007 DotNetNuke Database Mail SMTP SmarterMail Windows Remote Desktop Exchange 2010 .NET Framework ASP.Net SqlServer Log Parser Neptune clumping bamboo Magento PrestaShop MySQL SVN Yahoo Mail C# Blogs Silverlight Maytag