Pages

Thursday, November 12, 2015

Easiest way to generate a random password in PHP

In many web app, there are alot of them use random password generator for their user such as 2 step validation, phone verification, email verification and more. By using str_shuffle() in PHP become more handy to create random password:

 * note that..this is not a secure random password.. please use other encryption method or use java randomLib
  1. <?php
  2. function characterSuffle($len = 6) {
  3.     $list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
  4.     $str = substr(str_shuffle($list), 0, $len );
  5.     return $str;
  6. }
  7. echo characterSuffle();
  8. echo characterSuffle(8);
  9. echo characterSuffle(16);
  10. echo characterSuffle(32);
  11. ?>

No comments:

Post a Comment