Advertisement
CleverWebAdmin

Random Char Generator (Alpha-Numeric) v2.1

Nov 25th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1. /**
  2.  * PROJECT CLEVERWEB GENERAL LESSER LICENSE INFORMATION v3.1
  3.  * ------------------------------------------------------------
  4.  * Copyright (C) 2011-2012 Nicholas Jordon
  5.  * All rights reserved
  6.  * Package: Project CleverWeb
  7.  * http://ProjectCleverWeb.com
  8.  *
  9.  * License:
  10.  *   GNU LESSER GENERAL PUBLIC LICENSE VERSION 3
  11.  *
  12.  * This software is free software: you can redistribute it
  13.  * and/or modify it under the terms of the GNU Lesser
  14.  * General Public License as published by the Free Software
  15.  * Foundation, version 3 of the License, You may not use this
  16.  * software except in compliance with the license.
  17.  *
  18.  * You may obtain a copy of the license at:
  19.  *   http://www.gnu.org/licenses/lgpl-3.0.html
  20.  *
  21.  * Unless required by applicable law or agreed to in writing,
  22.  * software distributed under the license is distributed on
  23.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  24.  * KIND, either express or implied.
  25.  *
  26.  * Verfication Key: e450edcbe7703e40d9edcf19a18fb4bc
  27.  */
  28. function cw_randchar($length=NULL,$case=NULL,$spaces=NULL) {
  29.   // Removes "," chars if it is a number
  30.   if(!is_numeric($length)){
  31.     $temp_length = $length;
  32.     if(strstr($length,",")){
  33.       $length = preg_replace("/[,]*/","",$length);
  34.       if(!is_numeric($length)){
  35.         $length = strlen($temp_length);
  36.       }
  37.     }
  38.     elseif(!is_numeric($length)){
  39.       $length = strlen($length);
  40.     }
  41.   }
  42.   if($length==NULL||$length==0){
  43.     // Best password length
  44.     $length = 8;
  45.   }
  46.   // 2000 seems enough?
  47.   if($length>2000){
  48.     $length = 2000;
  49.   }
  50.   // numbers done twice so their % chance stays the same
  51.   $characters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  52.   $length = ceil($length);
  53.   if(stristr($case,"lower")){
  54.     $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
  55.     $case = true;
  56.   }
  57.   if(stristr($case,"upper")){
  58.     $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  59.     $case = true;
  60.   }
  61.   if($spaces==true){
  62.     // 8 spaces to make it seem more like encrypted words
  63.     $characters .= "        ";
  64.     if(!$case==true){
  65.       // 8 more if both capital and lower-case letters
  66.       $characters .= "        ";
  67.     }
  68.   }
  69.   if(!is_bool($spaces)){
  70.     // return error msg
  71.     /**
  72.      * NOTE: the error class, function and/or message is not published
  73.      * in this function but is respectfully included as part of this
  74.      * function as an "intellectual" object under CleverWeb's license.
  75.      */
  76.   }
  77.   // this part is done oddly due to it will otherwise error.
  78.   // it kept being one char place short randomly however not consistantly.
  79.   for ($count = 1; ; $count++) {
  80.     if (strlen($string)==$length) {
  81.       break;
  82.     }
  83.     $string .= $characters[mt_rand(0, strlen($characters))];
  84.   }
  85.   return $string;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement