Title:Random String FunctionsContributor: Jeffrey Paul (aka sneak)
Last Update: Friday March 31 00:22 EDT 2000
<?php
/*********************************************************************
* string randstring(int $length) *
*********************************************************************
* This function returns a random string of length $length. It *
* uses ascii values 48-57, 65-90, and 97-122 (all inclusive), and *
* will accept values of 1 to 65535 for length. It's output *
* contains a-z, A-Z, and 0-9. This is suitable for some password *
* applications and such. (no whitespace) *
*********************************************************************
* Sample: *
* 6rI9m664D8cjF6i3l2R9mb21WO387VK9q04K8VGEqm906bbPTBedx1SEof4d5pcD *
* PI0lm9uesw4F5cv8sjS5XNi58t7NCh48Zk9x9P95Lp2cHOl8NA611y5TCWxI2hIO *
* bue099apD1kkhG03eh1B7F02gVEImuHb457XNGakp8I7k248WEG1xaa5kxZA64s1 *
* 7xU0nW7HF41Vz5y9un8VfM1r6RFWr5uAf1yg2zRB8F45fI1sns6cN770MxG4uYoM *
* 7EmbgoTD4GbsFC7oSW20Rsm2179HWp6k90vQhT3eb6uIqRJqcze78V7Qfig5KyZh *
*********************************************************************/
function randstring ($length) {
if(!ereg("^[0-9]+$", $length))
return(-1);
if($length < 1 || $length > 65536)
return(-1);
srand((double)microtime()*1000000);
$string = '';
while(strlen($string) != $length) {
$type = rand(1,3);
if($type == 1) $string = $string . chr(rand(48,57));
if($type == 2) $string = $string . chr(rand(65,90));
if($type == 3) $string = $string . chr(rand(97,122));
}
return($string);
}
/*********************************************************************/
/**********************************************************************
* string goodrandstring(int $length) *
**********************************************************************
* This function returns a random string of length $length. It uses *
* ascii values 33 to 126 inclusive, and will accept values of 1 to *
* 65535 for length. Sample output follows. It's good for *
* creating secure passwords. It does not output spaces, tabs, or *
* returns (all easily printable non-high ascii minus whitespace). *
**********************************************************************
* Sample: *
* *
* /^z>QY"TnE&Ic0bB3R5>8U<`Jepmuc*[Zx>E*1@{(%f~m *
* 5XK(D\\b?a)@nsrpsMZ+vFl;vz=5V&H;/*B\\89g7]q4[B)nlF|vMBp&9E *
* "+y^wk"<EI.nET?^%Y^4l1iIG}3f&h]{+QBDJI,/rHs{:?N&a *
* mp;r$+0CE-|A_\\a,FQuso$_;iN<d5|T>(_$36%"5~4_XmB|oJrjY\\ *
* w<T]j6ux%'K(=y?p1|t+Xa4s:=?'_5L7otxtCB1S>>$}Nh^P4eta: *
* <L}2YJho"iL?1QKb#Ten5BmO5pqU2/{_NWp^Kum6wqb]inQ?eDPTu5.u7] *
* iTHn*`kz/=}ePHB *
**********************************************************************/
function goodrandstring ($length) {
if(!ereg("^[0-9]+$", $length))
return(-1);
if($length < 1 || $length > 65536)
return(-1);
srand((double)microtime()*1000000);
$string = '';
while(strlen($string) != $length) {
$string = $string . chr(rand(33,126));
}
return($string);
}
/*********************************************************************/
/**********************************************************************
* string bestrandstring(int $length) *
**********************************************************************
* This function returns a random string of length $length. It uses *
* all ascii values from 1 to 255 (no null bytes). It can deal with *
* length values from 1 to 65535. Don't use this for passwords or *
* anything like that, as most of these will break terminals and *
* browsers and the such. There is no sample because if you think *
* you need one this function isn't for you. *
**********************************************************************/
function bestrandstring ($length) {
if(!ereg("^[0-9]+$", $length))
return(-1);
if($length < 1 || $length > 65536)
return(-1);
srand((double)microtime()*1000000);
$string = '';
while(strlen($string) != $length) {
$string = $string . chr(rand(1,255));
}
return($string);
}
/*********************************************************************/
?>
Anyone who wishes to make additions or changes to this
PHP Tip email them to webmaster@linuxguruz.org
This document is Copyright (c) 1999, 2000 by LinuxGuruz