generate random string in php code example

Example 1: generate random string in php

/**
 * Generate a random string, using a cryptographically secure 
 * pseudorandom number generator (random_int)
 *
 * This function uses type hints now (PHP 7+ only), but it was originally
 * written for PHP 5 as well.
 * 
 * For PHP 7, random_int is a PHP core function
 * For PHP 5.x, depends on https://github.com/paragonie/random_compat
 * 
 * @param int $length      How many characters do we want?
 * @param string $keyspace A string of all possible characters
 *                         to select from
 * @return string
 */
function random_str(
    int $length = 64,
    string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
): string {
    if ($length < 1) {
        throw new \RangeException("Length must be a positive integer");
    }
    $pieces = [];
    $max = mb_strlen($keyspace, '8bit') - 1;
    for ($i = 0; $i < $length; ++$i) {
        $pieces []= $keyspace[random_int(0, $max)];
    }
    return implode('', $pieces);
}

Example 2: Generate Random String in PHP

phpCopy<?php
 
echo "Output-1: ",bin2hex(random_bytes(10)),"\n";
 
echo "Output-2: ",bin2hex(random_bytes(20)),"\n";
 
echo "Output-3: ",bin2hex(random_bytes(24)),"\n";
 
?>

Example 3: Generate Random String in PHP

phpCopy<?php
 
function secure_random_string($length) {
    $rand_string = '';
    for($i = 0; $i < $length; $i++) {
        $number = random_int(0, 36);
        $character = base_convert($number, 10, 36);
        $rand_string .= $character;
    }
 
    return $rand_string;
}
 
echo "Sec_Out_1: ",secure_random_string(10),"\n";
 
echo  "Sec_Out_2: ",secure_random_string(10),"\n";
 
echo  "Sec_Out_3: ",secure_random_string(10),"\n";
 
?>

Example 4: Generate Random String in PHP

phpCopy<?php 
$Random_str = uniqid();  
echo "Random String:", $Random_str, "\n";
?>

Example 5: Generate Random String in PHP

phpCopy<?php
 
echo "Out1: ",substr(md5(time()), 0, 16),"\n";
 
echo "Out2: ",substr(sha1(time()), 0, 16),"\n";
 
echo "Out3: ",md5(time()),"\n";
 
echo "Out4: ",sha1(time()),"\n";
 
?>

Example 6: Generate Random String in PHP

phpCopy<?php 
echo uniqid('user_');
?>

Example 7: Generate Random String in PHP

phpCopy<?php
 $x = 0;
 $y = 10;
$Strings = '0123456789abcdefghijklmnopqrstuvwxyz';
echo "Gen_rand_str: ",substr(str_shuffle($Strings), $x, $y), "\n";

$a = 0;
$b = 20;
$Strings='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
echo "Gen_My_Pass: ",'hello.'.substr(str_shuffle($Strings), $a, $b).'.World',"\n";
?>

Example 8: Generate Random String in PHP

phpCopy<?php
function random_str_generator ($len_of_gen_str){
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    $var_size = strlen($chars);
    echo "Random string ="; 
    for( $x = 0; $x < $len_of_gen_str; $x++ ) {  
        $random_str= $chars[ rand( 0, $var_size - 1 ) ];  
        echo $random_str;  
    }
echo "\n";
}
random_str_generator (8)
?>

Tags:

Php Example