Skip to main content

Generate Random String or Password Using PHP

If you want to generate random numbers, it is possible by using PHP’s rand function. But if you want to generate random string, it is not possible by using rand function. Because rand function only returns numbers as integer between two values.  I coded a simple PHP function that works exactly same as rand function but the deference is it returns string. You can use it in image captcha function, random password generator or anywhere else.


randString Function

/**
* Random String Generator
*
* @param optional int $min
* @param optional int $max
*
* @return string
*/

function randString($min = 5, $max = 8){
 # get random character length between minimum and maximum length
 $length = rand($min, $max);
 $string = '';
 # character index [0-9a-zA-Z]
 $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 # loop random times defined by $length
 for ($i=0; $i < $length; $i++) {
  # get random character index
  $string .= $index[rand(0, strlen($index) -1)];
 }
 return $string;
}

Usage

echo randString(4, 8);

Output

NO2xDPR
Tips: You can remove vowels (a, e, i, o, u) from alphabet index. It will help to generate meaningless string and it will also help to prevent from bad words. Also you can add some special characters (like @, #, $, % & ...). It help to generate strong password.

Comments

  1. Nice script! Thanks a lot!

    ReplyDelete
  2. Dapatkan disini , karena Obat Pelangsing slimming Capsule Di Apotik belum tersedia , karena slimming capsule tidak di jual di sembarang temapat termasuk di apotik pun tujuannya untuk menjaga ke aslian produk tersebut, karena sekarang banyak perusahaan yang meniru produk produk herbal sehingga herbal yang asli khasiatnya bisa di rusak oleh produk yang palsu. Untuk itu hati hati dengan slimming capsule

    and obat vig power capsule

    ReplyDelete
  3. try this simple code : http://www.sanwebcorner.com/2017/08/generate-password-to-textbox-using-php.html

    ReplyDelete

Post a Comment

Popular posts from this blog

Multiple File Upload with PHP

Sometimes we need to allow our users to upload multiple file upload. On my previous post I was shown the basic of simple file upload with PHP. Today I am going to show you how to allow users to upload multiple files. It is almost similar like simple file upload but we need to do some modification with html markup and php code. multiple attribute is needed to add on the html markup. Major web browsers like Firefox, Chrome, Safari and IE 9+ support this attribute. Now I am going to show you how to upload multiple file with PHP and basic HTML form.

Live Comment System with jQuery Ajax PHP and MySQL

Recently I have been made a post that focused on how to submit form without page refreshing. That post shows the basic functionality of using jQuery Ajax. On the same follow this post will describe how we can create a comment system to post an instant comment without page refreshing. Let's start...

Resize Image While Uploading with PHP

On my previous post I was shown an example how to upload file with php and html. It was based on php simple image upload functionality. Today I am going to show you little bit advance of image uploading. Suppose if you run a website and want to allow users to upload image files than you need to think little bit about server's space and bandwidth. For this purpose you need to do some optimization with image files. Image resize while uploading is one of them. All major website do it in the same way. If a user uploads a 5mb image file they resize it in different sizes and store on there server. It helps them to seed up there website and reduce there budget. Now take a look, how we can do it ourself.