方式一(变色版):
<html>
<head><meta charset="UTF-8"/><title></title><script src="jquery-2.0.2.min.js"></script>
</head>
<body>
<?php
header("content_type:text/html;charset=utf-8");
/* //以点的形式来连接变量$str1='hello';$str2='world';echo $str1.$str2;*///验证码一
// $str="请输入验证码";
// $rand=mt_rand(1000, 9999);
//echo $str.$rand;//验证码二
//$str="请输入验证码:";
$str="";
$str.="<span style='color:rgb(".mt_rand(0,255).",".mt_rand(0,255).",".mt_rand(0,255).")'>".mt_rand(0, 9)."</span>";
$str.="<span style='color:rgb(".mt_rand(0,255).",".mt_rand(0,255).",".mt_rand(0,255).")'>".mt_rand(0, 9)."</span>";
$str.="<span style='color:rgb(".mt_rand(0,255).",".mt_rand(0,255).",".mt_rand(0,255).")'>".mt_rand(0, 9)."</span>";
$str.="<span style='color:rgb(".mt_rand(0,255).",".mt_rand(0,255).",".mt_rand(0,255).")'>".mt_rand(0, 9)."</span>";
//echo $str;
?><DIV><span>请输入验证码:</span><input type="text" id="yzm" /><span id="zymzhi"><?php echo $str; ?></span></DIV><input type="submit" id="sub" οnclick="check()"/>
</body>
<SCRIPT type="text/javascript">
function check(){var str1=$("#zymzhi").text();var str2=$("#yzm").val();if(str1==str2){alert("OK");}else{alert("false");}
}
</SCRIPT>
</html>
方式二:(随机数类)
<?php
class RandString{//这个属性表示随机数的长度private $length;//type:1:数字类型;2:字母;3:数字+字母private $type;public function __construct($length,$type){$this->length=$length;$this->type=$type;}public function randNum(){switch ($this->type){case 1:
// $arr=array_rand(range(0, 9),$this->length);//从数组中取出一个或多个随机的单元
// return join($arr);//join表示将数组拼接成字符串;return join(array_rand(range(0, 9),$this->length));break;case 2:
// array_merge合并一个或多个数组
// array_flip数组中的键值对进行转换
// $array=array_merge(range('a', 'z'),range('A', 'Z'));
// $array=array_flip($array);
// $arr=array_rand($array,$this->length);
// return join($arr);//return join(array_rand(array_flip(array_merge(range('a', 'z'),range('A', 'Z'))),$this->length));break;case 3:return join(array_rand(array_flip(array_merge(range(0,9),range('a', 'z'),range('A', 'Z'))),$this->length));break;}}
}$strNum=new RandString(4, 3);//参数一:随机数长度;参数二:随机数类型{1:数字类型;2:字母;3:数字+字母
echo $strNum->randNum();