要求:
给定一个字符数据如,$char_array = 'a……zA……Z0……9';
可以写成函数呀,类呀等无所谓只要每次运行时生成一个6位的字符串就行,但字符串不可与前面所生成的字符串重复。
至与如何实现,是把每次运行的结果保存还是什么的,只要实现要求即可。
当然附有简单说明最好,不满足所说条件不给分。
急,100分。
给定一个字符数据如,$char_array = 'a……zA……Z0……9';
可以写成函数呀,类呀等无所谓只要每次运行时生成一个6位的字符串就行,但字符串不可与前面所生成的字符串重复。
至与如何实现,是把每次运行的结果保存还是什么的,只要实现要求即可。
当然附有简单说明最好,不满足所说条件不给分。
急,100分。
<?php
class rndPass{
var $vals; // For the String
var $PasswordLength; // Variable To Assign Password Length
function rndPass($passLen){ //Constructor To Assign Values
//If your would like to Allow Special Character in the Generatored Password.
//Please Add Special Charactes to the Variable $this->vals
$this->vals = "ABCDEFGHIJKLMNOPQRSTUVWXYZabchefghjkmnpqrstuvwxyz0123456789";
$this->PasswordLength = $passLen;
}
function PassGen(){ // Function to Generate Random Passowrd
if($this->PasswordLength > 4){
$i = 0;
$password = "";
while (strlen($password) < $this->PasswordLength) {
mt_getrandmax(); // Returns the maximum value that can be returned by a call rand
$num = rand() % strlen($this->vals);
$tmp = substr($this->vals, $num+4, 1);
$password = $password . $tmp;
$tmp ="";
}
return $password ;
}
else{
echo "Oops!! Generating a Password of Less than Four character is not Secure";
echo "\nPlease Try with More length";
}
}
}
//应用
$pass=new rndPass(6);
$tmppass=$pass->PassGen();
echo $tmppass;
?>
<?phpecho srandsalt();
/*
返回随机字符串或者数组
Params:
$count: 返回个数
$addchars: 增加其他随机字符
$arrayout: 是否数组输出
$unique: 是否允许重复
*/
function srandsalt($count = 6, $addchars = '', $arrayout = 0, $unique = 0)
{
$wfile = 'existsCode.inc';
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.$addchars;
if ($unique)
{
$tmp = range(0, strlen($chars)-1);
shuffle($tmp);
$keys = array_slice($tmp, 0, $count);
unset($tmp);
}
else
{
$keys = array_rand(range(0, strlen($chars)-1), $count);
}
$buff = array();
foreach($keys as $value)
{
$buff[] = $chars{$value};
}
//不要求返回数组
if (!$arrayout)
{
$buff = join('', $buff);
if(!file_exists($wfile)) {
fopen($wfile, 'a');
}
if(function_exists('file_get_contents')) {
$content = file_get_contents($wfile);
} else {
$fp = fopen($wfile, 'r');
$content = fread($fp, filesize($wfile));
fclose($fp);
}
$existsCode = explode("\n", $content);
if(in_array($buff, $existsCode)) {
$buff = srandsalt($count, $addchars, $arrayout, $unique);
} else {
$fp = fopen($wfile, 'a');
fwrite($fp, "{$buff}\n");
fclose($fp);
}
}
return $buff;
}
?>
数据库存储 效率肯定要低点
除非楼主只需要生成少量的
数据库操作每次生成随机码最少要查询一次数据库表 如果随机到重复 可能要二次查询 还要包括最少一次的插入操作如果数据量大的话 数据库查询也不好说文件存储 可以改成 strstr 效率可能要好点
<?phpecho srandsalt();
/*
返回随机字符串或者数组
Params:
$count: 返回个数
$addchars: 增加其他随机字符
$arrayout: 是否数组输出
$unique: 是否允许重复
*/
function srandsalt($count = 6, $addchars = '', $arrayout = 0, $unique = 0)
{
$wfile = 'existsCode.inc';
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.$addchars;
if ($unique)
{
$tmp = range(0, strlen($chars)-1);
shuffle($tmp);
$keys = array_slice($tmp, 0, $count);
unset($tmp);
}
else
{
$keys = array_rand(range(0, strlen($chars)-1), $count);
}
$buff = array();
foreach($keys as $value)
{
$buff[] = $chars{$value};
}
//不要求返回数组
if (!$arrayout)
{
$buff = join('', $buff);
if(!file_exists($wfile)) {
fopen($wfile, 'a');
}
if(function_exists('file_get_contents')) {
$content = file_get_contents($wfile);
} else {
$fp = fopen($wfile, 'r');
$content = fread($fp, filesize($wfile));
fclose($fp);
}
if(strstr($content, $buff."\n")) {
$buff = srandsalt($count, $addchars, $arrayout, $unique);
} else {
$fp = fopen($wfile, 'a');
fwrite($fp, "{$buff}\n");
fclose($fp);
}
}
return $buff;
}
?>
! 代表阶乘 n为
假设从5个人中抽取2个那么
C(n,m)=n!/[m!(n-m)!]=n*(n-1)*...*(n-m+1)/[1*2*...*m],如C(5,2)=[5*4]/[1*2]=10.