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

Magento 301 IIS7 WordPress CentOS PHP Google Analytics SVN mstsc.exe DIR-655 .NET Framework Maytag PrestaShop OfficeLive Yahoo Mail eCommerce Log Parser AWStats Linux SSL Security Spam iframe SBS2011 ASP.Net Port 25 D-Link DotNetNuke Azure redirect Exchange 2010 SmarterMail SqlServer SMTP clumping bamboo MySQL BlogEngine.Net Live Writer OpenCart Tools PST Windows Remote Desktop Outlook 2007 Outlook Connector web.config