PHP - Generate Random Strings

In this tutorial we will create a Generate Random Strings using PHP. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Generate Random Strings</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-3"></div>
  18.                 <div class="col-md-6">
  19.                         <form method = "POST">
  20.                                 <div class="form-group">
  21.                                         <label>Enter some length</label>
  22.                                         <input type="number" min="1" max="12" name="length" class="form-control" require="required"/>
  23.                                 </div>
  24.                                 <?php include 'generate.php'?>
  25.                                 <center><button class="btn btn-info" name="generate"><span class="glyphicon glyphicon-random"></span> Generate</button></center>
  26.                         </form>
  27.                 </div>
  28.                
  29.         </div>
  30. </body>
  31. </html>

Creating the Main Function

This is the main function of the application. This code will display a randomly generate strings when click. To do that copy and write inside the text editor, then save it as generate.php.
  1. <?php
  2.         function GenerateString($length) {
  3.                 $strings = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  4.                 $stringLength = strlen($strings);
  5.                 $newStrings = '';
  6.                 for ($i = 0; $i < $length; $i++) {
  7.                         $newStrings .= $strings[rand(0, $stringLength - 1)];
  8.                 }
  9.                 return $newStrings;
  10.         }
  11.        
  12.         if(ISSET($_POST['generate'])){
  13.                 $length = $_POST['length'];
  14.                 echo '<center><h3 style="border:1px solid #000; padding:5px;" class="text-success">'.GenerateString($length).'</h3></center>';
  15.         }
  16. ?>
There you have it we successfully created Generate Random Strings using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment