天空是蓝色的

做好软件为中国 #gcc -c helloworld.c -o helloworld.o //编译目标文件 #gcc helloworld.o -o helloworld //编译成可执行exe #helloworld //运行exe
数据加载中……
[转载]几个PHP函数
作者主页 http://phpu.blogchina.com/

 

获取数字随机数 

之前用过的一些函数,最近会陆续做些整理。

获得最小和最大值之间随机数,位数不足补零


 #-------------------------------------------
 # 获得最小和最大值之间随机数,位数不足补零
 #-------------------------------------------
 
Function getRandNumber ($fMin, $fMax
) { 
 
srand((double)microtime()*1000000
);
 
$fLen = "%0".strlen($fMax)."d"
;
  Return
sprintf($fLen, rand($fMin,$fMax
));
 }


判断数字大小

判断数字的大小是否在某个范围内,如果仅仅是判断是否为数字无需使用。


 #---------------------------------------------------------- 
 # 判断数字大小,无大小的判断可以用系统带的 is_numeric($str)
 #----------------------------------------------------------
 Function isNumber ($fNum
, $fMin="", $fMax="") { 
  if ( eregi(
"^[0-9]+$", $fNum) ) { 
   if ( 
"" == $fMax && "" == $fMin ) { 
   Return True
;
   } elseif ( "" == $fMin && $fNum <= $fMax ) { 
   Return True
;
   } elseif ( "" == $fMax && $fNum >= $fMin ) { 
   Return True
;
   } elseif ( $fNum >= $fMin && $fNum <= $fMax ) { 
   Return True
;
   } else { 
   Return False
;
   }
  } else { 
  Return False
;
  }
 } 

 随机生成图像验证码 
根据以前的验证码生成修改而来,需要GD库的支持
特点:
1 安全性较强,随机背景加随机像素点绘制
2 生成四位随机数字(可重定义)
3 验证码存入session,直接取值即可比较
4 可以带参数调用。(默认是54×22的png图像)
       t 图像格式 png gif jpeg 等
       w 图像宽度
       h 图像高度

调用方式如下:

<img src
="verify.php?t=gif&w=60&h=25" border="0" alt="">

代码如下:
<?PHP
 #----------------------------------------------
 # 文    件: verify.php
 # 功    能: 认证码显示
 #           Liu21st
,liu21st@126.com
 # 最后修改: 
2004-05-07 10:57:30 Liu21st
 #----------------------------------------------
  
 $type
=($_GET['t'])?($_GET['t']):'png';
 $width=($_GET['w'])?($_GET['w']):54;
 $height=($_GET['h'])?($_GET['h']):22;
 session_start();
 Header("Content-type: image/".$type);
 srand((double)microtime()*1000000);
 
 $randval 
= sprintf("%04d", rand(0,9999));
 session_register('session_verify');
 if ( is_array($_SESSION) ) { 
  $_SESSION
['session_verify'] = $randval;
  $session_verify =$_SESSION['session_verify'];
 }
 else { 
  $session_verify 
= $randval;
 }
 
if ( $type!
='gif' && function_exists('imagecreatetruecolor')) { 
$im 
= @imagecreatetruecolor($width,$height);
 }else { 
  $im 
= @imagecreate($width,$height);
 }
 $r 
= Array(225,255,255,223);
 $g = Array(225,236,237,255);
 $b = Array(225,236,166,125);
 
 $key 
= rand(0,3);
  
 $backColor 
= ImageColorAllocate($im, $r[$key],$g[$key],$b[$key]);//背景色(随机)
 $borderColor = ImageColorAllocate($im, 0, 0, 0);//边框色
 $pointColor = ImageColorAllocate($im, 0, 255, 255);//点颜色
 
 @imagefilledrectangle($im
, 0, 0, $width - 1, $height - 1, $backColor);
 @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
 $stringColor = ImageColorAllocate($im, 255,51,153);
 for($i=0;$i<=10;$i++){ 
  $pointX = rand(2,$width-2);
  $pointY = rand(2,$height-2);
  @imagesetpixel($im, $pointX, $pointY, $pointColor);
 }
 
 @imagestring($im
, 5, 8, 3, $randval, $stringColor);
 $ImageFun='Image'.$type;
 $ImageFun($im);
 @ImageDestroy($im);
 
?> 

 PHP随机密码生成 
产生随机字串,可用来自动生成密码。
特点:
1. 可以指定密码包含数字或字符,默认为混和模式
2. 指定随意密码长度,默认长度为6位



代码如下:
#-------------------------------------------
# 产生随机字串,可用来自动生成密码 
# 默认长度6位 字母和数字混合
# $format ALL NUMBER CHAR 字串组成格式
#-------------------------------------------
 function randStr($len
=6,$format='ALL') { 
 switch($format) { 
 case 'ALL':
 $chars
='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~'; break;
 case 'CHAR':
 $chars
='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~'; break;
 case 'NUMBER':
 $chars
='0123456789'; break;
 default :
 $chars
='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~'; 
 break;
 }
 mt_srand((double)microtime()*
1000000*getmypid()); 
 $password="";
 while(strlen($password)<$len)
    $password.
=substr($chars,(mt_rand()%strlen($chars)),1);
 return $password;
 } 

posted on 2005-12-30 15:30 bluesky 阅读(390) 评论(0)  编辑  收藏 所属分类: PHP




标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交