PHP - Generate Random Strings
Submitted by razormist on Thursday, August 9, 2018 - 11:34.
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...
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Generate Random Strings</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-3"></div>
- <div class="col-md-6">
- <form method = "POST">
- <div class="form-group">
- <label>Enter some length</label>
- <input type="number" min="1" max="12" name="length" class="form-control" require="required"/>
- </div>
- <?php include 'generate.php'?>
- <center><button class="btn btn-info" name="generate"><span class="glyphicon glyphicon-random"></span> Generate</button></center>
- </form>
- </div>
- </div>
- </body>
- </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.- <?php
- function GenerateString($length) {
- $strings = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $newStrings = '';
- for ($i = 0; $i < $length; $i++) {
- }
- return $newStrings;
- }
- $length = $_POST['length'];
- echo '<center><h3 style="border:1px solid #000; padding:5px;" class="text-success">'.GenerateString($length).'</h3></center>';
- }
- ?>
Add new comment
- 59 views