Here’s a random password and string generator function for PHP.
It is totally free of use.
randpass(); Will give you a 8 characters long randomized string with numbers and letters.
The function also takes these arguments:
$numchars <-- The length in characters for the returned string.
$digits <------ Should the generated password contain figures? 1/0
$letters <----- Should it contain letters? 1/0
$sensitive <--- Both upper and lower letters? 1/0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function randpass($numchars=8,$digits=1,$letters=1,$sensitive=0) { $dig = "12345678901234567890"; $abc = "abcdefghijklmnopqrstuvwxyz"; if($letters == 1) { $str .= $abc; } if($sensitive == 1) { $str .= strtoupper($abc); } if($digits == 1) { $str .= $dig; } for($i=0; $i < $numchars; $i++) { $randomized .= $str{rand() % strlen($str)}; } return $randomized; } |
To give an example of the code:
24 25 | echo "Generated password: \n"; echo randpass(12,1,1,1); |
The code above will result in:
Generated password: 4C8cly61h907
There are plenty of database space-saving functions out there and PHP has included some of them already. These two functions (or four actually) are already in the standard library which means that you got no reason to ignore them.
* Use ip2long() and long2ip() to store the IP adresses as Integers instead of storing them as strings, which will reduce the space from 15 bytes to 4 bytes. This will also increase search speed and make it easy to see if a ip falls within a specified range.
* Use gzcompress() and gzuncompress() to reduce the strings before you store them in a database. The gzcompress can compress plain-text up to 90%. The only reason why you shouldn’t use it is when you need full-text indexing capabilities.