8 Ways to Generate Random String in PHP Tutorial
Introduction
In this tutorial, you will learn the 8 Different Ways of Generating Random Strings using PHP Language. The tutorial aims to provide a reference or guide to the IT/CS students or new to PHP Language for using some of the built-in methods or functions of PHP. Here, snippets will be provided and a sample web program source code file is free to download.
Why Generate Random Strings?
In some cases, developers commonly generate random strings to create API Keys, Access Tokens, or Secret Keys for some features of the web applications. The main purpose of the said generated string is often used for security purposes for accessing some data on the server. Generating a random sting can be used for generating a unique random string.
Technologies
In this tutorial, we will use the following technologies:
- Latest XAMPP with PHP Version 7.4+
- Text Editors such as Sublime Text, Notepad++, or VS Code
- HTML
- CSS
- Bootstrap (for interface design purposes)
8 ways of Generating Random Strings
Using md5()
The PHP md5() is a function that calculates the MD5 hash of a string. This function requires a string to convert into a hash.
Here is a sample snippet of generating a random string using md5().
- <?php
- function gen_using_md5($length = 16){
- return $code;
- }
- ?>
Using sha1()
The PHP sha1() is like an md5() that calculates the hash of strings. This hashing method calculates the hash of the string using the US Secure Hash Algorithm 1.
Here is a sample snippet of generating a random string using sha1().
- <?php
- function gen_using_sha1($length = 16){
- return $code;
- }
- ?>
Using mt_rand()
The PHP mt_rand() function is a Random Number Generator using Mersenne Twister. Using this function we can generate a random number and select from a character in an array of characters.
Here is a sample snippet of generating a random string using mt_rand().
Using random_int()
The PHP random_int() function generates a cryptographically secure, informally selected integer.
Here is a sample snippet of generating a random string using random_int().
- <?php
- function gen_using_random_int($length = 16){
- $code = [];
- for($i = 0 ;$i < $length ;$i++){
- }
- return $code;
- }
- ?>
Using rand()
The PHP rand() function generates a random integer.
Here is a sample snippet of generating a random string using rand().
Using shuffle()
The PHP shuffle() function shuffles the order of an array. Using this and an array of characters, we can generate random strings.
Here is a sample snippet of generating a random string using shuffle().
Using uniqid()
The PHP uniqid() function generates a unique ID. You can also add a string prefix for generating an ID.
Here is a sample snippet of generating a random string using uniqid().
Using bin2hex()
The PHP bin2hex() function converts binary data into hexadecimal representation. This function can generate random strings by converting random bytes using random_bytes().
Here is a sample snippet of generating a random string using bin2hex().
Example
Here's an example source code that demonstrates the 8 Different ways of Generating Random Strings.
Interface
- <?php require_once('functions.php') ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
- <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
- <style>
- html, body{
- min-height:100%;
- width:100%;
- }
- </style>
- </head>
- <body>
- <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient">
- <div class="container">
- <div>
- </div>
- </div>
- </nav>
- <div class="container px-5 my-3">
- <div class="col-lg-6 col-md-8 col-sm-12 mb-4 mx-auto">
- <div class="card shadow rounded-0">
- <div class="card-header rounded-0">
- <div class="d-flex justify-content-between">
- <div class="col-auto">
- </div>
- </div>
- </div>
- <div class="card-body">
- <div class="container-fluid">
- <table class="table table-bordered table-striped">
- <thead>
- <tr>
- </tr>
- </thead>
- <tbody>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- </tbody>
- </table>
- </div>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
PHP Scripts
- <?php
- function gen_using_md5($length = 16){
- return $code;
- }
- function gen_using_sha1($length = 16){
- return $code;
- }
- function gen_using_mt_rand($length = 16){
- $code = [];
- for($i = 0 ;$i < $length ;$i++){
- }
- return $code;
- }
- function gen_using_random_int($length = 16){
- $code = [];
- for($i = 0 ;$i < $length ;$i++){
- }
- return $code;
- }
- function gen_using_rand($length = 16){
- $code = [];
- for($i = 0 ;$i < $length ;$i++){
- }
- return $code;
- }
- function gen_using_shuffle($length = 16){
- return $code;
- }
- function gen_using_uniqid($length = 16){
- return $code;
- }
- function gen_using_bin2hex($length = 16){
- return $code;
- }
- ?>
That's it! You can test the sample program on your end and see if it meets our main goal for this tutorial. I have provided also the complete source code file that I created for this tutorial. You can download it by clicking the download button located below this article.
DEMO VIDEO
That's the end of this tutorial. I hope this 8 Ways of Generating Random String in PHP Tutorial will help you with what you are looking for and that you'll find this useful for your current and future PHP Projects.
Explore more on this website for more Tutorials and Free Source Codes.
Enjoy :)
Add new comment
- 581 views