Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
PHP ›
PHP random string generation

PHP random string generation

January 18, 2011 PHP Jesin A Leave a Comment

php category thumbnail

Generate a random alphanumeric string containing both uppercase and lowercase characters in PHP. The rand() function in PHP generates a random number we’ll be using this function to generate a random alphanumeric string. A variable should be declared containing all the alphabets in English both uppercase and lowercase (A-Z and a-z) and numbers from 0-9. The concept here is to randomly pick a character from the string containing all the characters and assign it to a variable. Each time this process is done the currently randomly selected character is concatenated with the previously generated characters. This should be done as many times the length of the random string required. For this a looping statement can be used.First lets assign all the uppercase and lowercase alphabets and numbers to a variable named $chars. Then the variable which will hold the random string value should be assigned an empty value.

$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$string = "";

If you don’t assign $string and empty value you’ll get a notice saying “Notice: Undefined variable: string in /path/to/php/file on line xx” if you have display_errors on. Using the substr() function a single character is randomly picked from $chars variable. The syntax for substr() is substr(string_variable,start_position,length). In this case the string variable is $chars and length is 1 the start_position parameter should be randomly generated using rand() function. So the min value parameter of rand() is 0 and max value is the length of all the characters. The value returned should be assigned to $string and st the same time concatenated with the previous values. So it will look like $string = $string . substr(parameters) instead we can shortly write it as $string .= substr(parameters) All this should be placed inside a for loop which should loop the length of random string required.

$string = "";
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for($i=0;$i<10;$i++)
$string.=substr($chars,rand(0,strlen($chars)),1);

The above code will generate a random string containing 10 characters and store it inside $string. To create a PHP function generating random string the following code is used.

function randomstring($len)
{
$string = "";
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for($i=0;$i<$len;$i++)
$string.=substr($chars,rand(0,strlen($chars)),1);
return $string;
}

The function should be called with a integer as a length parameter. E.g. randomstring(20) will return a random string containing 20 characters.

Related posts:

php category thumbnailPHP Captcha Tutorial web design category thumbnailJavascript Validation with Regular Expressions php category thumbnailPHP Sessions Tutorial php category thumbnailHow to display IP Address in PHP web design category thumbnailJavascript Reload Image

Tags: php, php scripts

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.