1. php的验证码提示怎样制作
一般制作验证码会按照下面的几步走:
一:创建出来一个图片,通常我们成为源,可以用imagecreatetruecolor()这个函数搞定
二:给这个源 添加背景色,同时设置文本显示的颜色,GD库函数为我们提供了imagecolorallocate()函数
三:材料弄好了,我们要给它添点内容了,就是我们随机生成的数字或者字母,甚至可以是它们的组合,这里我们可以选择两个函数 imagettftext()、imagesrting(),这两个函数的不同,我们会在后面讲解。
例:
<?php
session_start();//开启session,用来记录获得的验证码,这个函数要写在程序的开头,不然会出现错误
header(“Content-type :image/gif”);//把文件的返回类型设为image/gif格式,这个格式可以输出图片
$codelen=4;//设置你要让用户输入字符的个数,一般为4,过长用户体验不好。
$charset =”ABCDEFGHKLMNPRSTUVWYZ23456789″;//我们可以尽量把一些难以辨认的字符去掉,比如阿拉伯数字0和字母o,这也是提高用户体验的一种方法。
$code =”;
for($i=0;$i<$codelen;$i++){//用for循环得到4个随机的字符,在这里用到了mt_rand,这个函数比rand的效率要高的多,建议大家用这个
$code .=$charset{mt_rand(0,strlen($charset)-1)};
}
$_SESSION['code']=$code;//下篇关于session验证的文章将会用到
$width = 80;
$height = 40;
$im = imagecreatetruecolor($width,$height);//用imagecreatetruecolor()函数来建立一个新的图片,里面的两个数值分别是宽度和高度,这是制作验证码的第一步
$bg = imagecolorallocate($im,255,255,0); //图片背景的颜色,这里是第二步
$textcolor = imagecolorallocate($im,255,0,0);//文字的颜色
imagefill($im,0,0,$bg);//给图片填充背景色
//好了上面的铺垫任务做的差不多了,现在关键就是让字符显示在图片上,这里有两种方法我们一一介绍。
$font =”ggbi.ttf”;//如果你有字体的话 就填上字体的相对路径,如果没有就留空。下面的两个用法我会一一讲解。
if($font!==”"){
for($num=0;$num<4;$num++){
imagettftext($im,mt_rand(12,16),(mt_rand(0,75)+330)%360,5+15*$num,20+mt_rand(2,5),$textcolor,$font,$code[$num]);//这里是第三步
}
}
else{
for($num=0;$num<4;$num++){
imagestring($im,5,10+15*$num,10+mt_rand(0,5),$code[$num],$textcolor);
}
}
header(“Content-type: image/jpeg”);
imagejpeg($im);
?>
2. 请问PHP生成验证码的类怎么写
<?php
classCode{
//1.定义各个成员有宽、高、画布、字数、类型、画类型
private$width;//宽度
private$height;//高度
private$num;//验证码字数
private$imgType;//生成图片类型
private$Type;//字串类型1,2,3三个选项1纯数字2纯小写字母3大小写数字混合
private$hb;//画布
public$codestr;//验证码字串
publicfunction__construct($height=20,$num=4,$imgType="jpeg",$Type=1){
$this->width=$num*20;
$this->height=$height;
$this->num=$num;
$this->imgType=$imgType;
$this->Type=$Type;
$this->codestr=$this->codestr();
$this->zuhe();
}
//2.定义随机获取字符串函数
privatefunctioncodestr(){
switch($this->Type){
case1://类型为1获取1-9随机数
$str=implode("",array_rand(range(0,9),$this->num));
break;
case2://类型为2获取a-z随机小写字母
$str=implode("",array_rand(array_flip(range(a,z)),$this->num));
break;
case3://类型为3获取数字,小写字母,大写字母混合
for($i=0;$i<$this->num;$i++){
$m=rand(0,2);
switch($m){
case0:
$o=rand(48,57);
break;
case1:
$o=rand(65,90);
break;
case2:
$o=rand(97,122);
break;
}
$str.=sprintf("%c",$o);
}
break;
}
return$str;
}
//3.初始化画布图像资源
privatefunctionHb(){
$this->hb=imagecreatetruecolor($this->width,$this->height);
}
//4.生成背景颜色
privatefunctionBg(){
returnimagecolorallocate($this->hb,rand(130,250),rand(130,250),rand(130,250));
}
//5.生成字体颜色
privatefunctionFont(){
returnimagecolorallocate($this->hb,rand(0,100),rand(0,100),rand(0,100));
}
//6.填充背景颜色
privatefunctionBgColor(){
imagefilledrectangle($this->hb,0,0,$this->width,$this->height,$this->Bg());
}
//7.干扰点
privatefunctionganrao(){
$sum=floor(($this->width)*($this->height)/3);
for($i=0;$i<$sum;$i++){
imagesetpixel($this->hb,rand(0,$this->width),rand(0,$this->height),$this->Bg());
}
}
//8.随机直线弧线
privatefunctionhuxian(){
for($i=0;$i<$this->num;$i++){
imageArc($this->hb,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand(0,$this->height),rand(0,360),rand(0,360),$this->Bg());
}
}
//9.写字
privatefunctionxiezi(){
for($i=0;$i<$this->num;$i++){
$x=ceil($this->width/$this->num)*$i;
$y=rand(1,$this->height-15);
imagechar($this->hb,5,$x+4,$y,$this->codestr[$i],$this->Font());
}
}
//10.输出
privatefunctionOutImg(){
$shuchu="image".$this->imgType;
$header="Content-type:image/".$this->imgType;
if(function_exists($shuchu)){
header($header);
$shuchu($this->hb);
}else{
exit("GD库没有此类图像");
}
}
//11.拼装
privatefunctionzuhe(){
$this->Hb();
$this->BgColor();
$this->ganrao();
$this->huxian();
$this->xiezi();
$this->OutImg();
}
publicfunctiongetCodeStr(){
return$this->codestr;
}
}
$a=newCode();
$a->getCodeStr();
?>
3. PHP制作注册页面验证码
使用view-source:查看源码会有错误提示,所以导致图片无法正常输出
4. php怎么实现验证码的
验证码功能机制实现思路
常规的验证码实现:
a、产生一张png的图片
b、为图片设置背景色
c、设置字体颜色和样式
d、产生4位数的随机的验证码
e、把产生的每个字符调整旋转角度和位置画到png图片上
f、加入噪点和干扰线防止注册机器分析原图片来恶意注册
g、输出图片
h、释放图片所占内存
i、将验证码保存到session或是数据库
j、将和输入的验证码进行对比
短信(邮箱)验证码机制:
a、产生4-6位数的随机的验证码
b、把产生的每个字符保存到session或是数据库
c、将验证码发送到用户的手机(邮箱)
d、用户在规定时间内进行输入
e、将验证码从session或是数据库中取出
f、将和输入的验证码进行对比验证
5. php验证码怎么实现
1.新建code.php验证码生成文件
在此之前必须打开php的GD库,修改php.ini文件的配置,取消extension=php_gd2.dll前面的分号。代码如下:
<?php
session_start();
//生成验证码图片
Header("Content-type: image/PNG");
$im = imagecreate(44,18);
$back = ImageColorAllocate($im, 245,245,245);
imagefill($im,0,0,$back); //背景
srand((double)microtime()*1000000);
//生成4位数字
for($i=0;$i<4;$i++){
$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));
$authnum=rand(1,9);
$vcodes.=$authnum;
imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
}
for($i=0;$i<100;$i++) //加入干扰象素
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()p , rand()0 , $randcolor);
}
ImagePNG($im);
ImageDestroy($im);
$_SESSION['Checknum'] = $vcodes;
?>
2. 显示验证码图片
在需要显示验证码的页面中加入
<input type="text" name="passcode" >
<img src="code.php">
3.判断并获取验证码的值
验证码是通过第一步骤代码中的$_SESSION['Checknum'] = $vcodes;赋的值,所以验证码的值存在$_SESSION['Checknum']当中。在验证页面,使用以下代码,
...
session_start();//启动会话
$code=$_POST["passcode"];
if( $code == $_SESSION["Checknum"])
{...}即可完成验证码登录。
运行截图:
望采纳,谢谢
6. 如何用PHP生成验证码
打开php gd库(你看下phpinfo是否打开,或者直接运行imagecreate,生成验证码)
然后利用session记录验证码(页面传递)
7. 我的php代码中登陆界面加一个验证码,如何实现
php登陆页面+验证码的实现,参考如下:
1、首先新建一个php站点;
8. 怎么用PHP写个验证码
首先,当用户打开页面时随机产生一个session,然后根据这个值生成验证码图片。
第二,将验证码图片显示到表单上。
第三,当用户提交时表单时,比较session里的值与表单中验证码的值进行比较。
简单的实现过程:http://www.nowamagic.net/php/php_CheckCode.php
复杂的验证码图片生成:http://www.admin5.com/article/20080314/75984.shtml