您的位置:首页 > 编程语言 > PHP开发

PHP知识点学习总结

2016-05-19 18:20 751 查看

验证码制作

验证码功能:屏蔽机器请求,保障业务不受机器提交请求干扰

普通验证码制作

创建画布 imagecreatetrueccolor()[默认黑色的]

设置背景色取代黑色默认颜色imagecolorallocate()

填充背景色到画布上 imagefill()

为空白画布上添加字符 imagestring()

添加点干扰元素 imagesetpixel()

添加线干扰元素imageline()



验证码的动态校验

使用JS实现动态校验验证码

增加可点击的”换一个”文案

用JS选择器选取验证码图片

用JS选择器选取验证码图片

用JS修改验证码图片地址(修改src)

验证码的核心技术分析

底图的实现,并且添加干扰元素;依赖PHP图片处理GD

生成验证内容,简单的随机数生成,使用PHP函数mt_rand();随机数字+字母生成,需要ASCII码理论基础;随机中文内容生成,需要UTF-8编码理论基础;

验证内容保存在服务端;需要PHP操作SESSION基础;

验证内容的校验;需要前端Ajax基础;

Demo

captcha.php

<?php
//依赖GD扩展
$image=imagecreatetruecolor(100,30);
$bgcolor=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$bgcolor);//填充
for($i=0;$i<4;$i++){
$fontsize = 6;
$fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
$fontcontent = rand(0,9);//产生0~9之间的数字

$x = ($i*100/4+rand(5,10));
$y = rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
//点干扰
for($i=0;$i<200;$i++){
$pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
}
//线干扰
for($i=0;$i<3;$i++){
$linecolor=imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));
imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
}
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
?>


captcha.php

这里将验证码保存在session中

<?php
// 依赖GD扩展
session_start();
$image=imagecreatetruecolor(100,30);
$bgcolor=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$bgcolor);//填充
$fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
$captch_code = '';
for($i=0;$i<4;$i++){
$fontsize=6;
$fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));

$data = 'abcdefghijklmnopqrstuvwxyz0123456789';
$fontcontent = substr($data,rand(0,strlen($data)),1);
$captch_code .= $fontcontent;

$x = ($i*100/4) + rand(5,10);
$y = rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION['authcode'] = $captch_code;
//点干扰
for($i=0;$i<200;$i++){
$pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
}
//线干扰
for($i=0;$i<3;$i++){
$linecolor=imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));
imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
}

header('content-type:image/png');
imagepng($image);
imagedestroy($image);
?>


form.php

<?php
if(isset($_POST['authcode'])){
session_start();
//将验证码中的字母全部转化为小写再与session中的对比
if(strtolower($_POST['authcode']) == $_SESSION['authcode']){
echo "输入正确";
}else{
echo "输入错误";
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>确认验证码</title>
</head>
<body>
<form method="post" action="./form.php">
<p>验证码图片:<img src="./captcha1.php?r=<?php echo rand();?>" border="1"
width="100" height="30" id="captcha_img">
<!-- 动图更换验证码通过javascript修改 src地址,通过改变传入的参数,从而刷新验证码  -->
<a href = "javascript:void(0)" onclick = "document.getElementById('captcha_img').src = './captcha1.php?r='+Math.random()">换一个?</a>
</p>
<p>请输入图片中的内容:<input type="text" name="authcode" value="" placeholder="请输入验证码"></p>
<p><input type="submit" name="" value="提交" style="padding: 6px 20px;"></p>
</form>
</body>
</html>


php?r=<?PHP echo rand(); ?>,访问PHP文件传随机数,为防止有的浏览器可能使用缓存而不刷新验证码


文字验证码生成

captcha_text.php

<?php
session_start();
$image=imagecreatetruecolor(200,60);
$bgcolor=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$bgcolor);//填充

$fontface = "msyh.ttf";
$str = "独坐在翠绿的湖边碧水悠悠轻风柔拂环湖之青映底之蓝微晃细浪曲起的双膝顶寸单颌凝望远方山环天连光和云舒瞑瞑有耳天籁脆悦,纯粹佳畅倍爽之极脉络荡激心音蹦发至天涯至海角";
//因为每个汉字占3个字符
$strdb = str_split($str,3);
$captch_code = '';
for($i=0;$i<4;$i++){
$fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
$index = rand(0,count($strdb));
$cn = $strdb[$index];
$captch_code = $cn;
imagettftext($image, mt_rand(20,24), mt_rand(-60,60), (40*$i+20), mt_rand(30,35), $fontcolor, $fontface, $cn);
}
$_SESSION['authcode'] = $captch_code;
//点干扰
for($i=0;$i<200;$i++){
$pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
imagesetpixel($image,rand(1,199),rand(1,59),$pointcolor);
}
//线干扰
for($i=0;$i<3;$i++){
$linecolor=imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));
imageline($image,rand(1,199),rand(1,59),rand(1,199),rand(1,59),$linecolor);
}

header('content-type:image/png');
imagepng($image);
imagedestroy($image);
?>


form.php

<?php
if(isset($_POST['authcode'])){
session_start();
//将验证码中的字母全部转化为小写再与session中的对比
if(strtolower($_POST['authcode']) == $_SESSION['authcode']){
echo "输入正确";
}else{
echo "输入错误";
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>确认验证码</title>
</head>
<body>
<form method="post" action="./form.php">
<p>验证码图片:<img src="./captcha_text.php?r=<?php echo rand();?>" border="1"
width="200" height="60" id="captcha_img">
<!-- 动图更换验证码通过javascript修改 src地址,通过改变传入的参数,从而刷新验证码  -->
<a href = "javascript:void(0)" onclick = "document.getElementById('captcha_img').src = './captcha_text.php?r='+Math.random()">换一个?</a>
</p>
<p>请输入图片中的内容:<input type="text" name="authcode" value="" placeholder="请输入验证码"></p>
<p><input type="submit" name="" value="提交" style="padding: 6px 20px;"></p>
</form>
</body>
</html>


图片验证码生成



captcha_img.php

<?php
session_start();
$table=array(
'img1'=>'虎',
'img2'=>'猫',
'img3'=>'狗',
'img4'=>'马'
);
$index=rand(1,4);
$value=$table['img'.$index];
$_SESSION['authcode']=$value;

$filename=dirname('_FILE_').'\\img'.$index.'.jpg';//_FILE_要加单引号,否则可能会报错
$contents=file_get_contents( $filename);

header('content-type:image/jpg');
echo $contents;
?>


form.php

<?php
if(isset($_POST['authcode'])){
session_start();
//将验证码中的字母全部转化为小写再与session中的对比
if(strtolower($_POST['authcode']) == $_SESSION['authcode']){
echo "输入正确";
}else{
echo "输入错误";
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>确认验证码</title>
</head>
<body>
<form method="post" action="./form.php">
<p>验证码图片:<img src="./captcha_img.php?r=<?php echo rand();?>" border="1"
width="200" height="200" id="captcha_img">
<!-- 动图更换验证码通过javascript修改 src地址,通过改变传入的参数,从而刷新验证码  -->
<a href = "javascript:void(0)" onclick = "document.getElementById('captcha_img').src = './captcha_img.php?r='+Math.random()">换一个?</a>
</p>
<p>请输入图片中的内容:<input type="text" name="authcode" value="" placeholder="请输入验证码"></p>
<p><input type="submit" name="" value="提交" style="padding: 6px 20px;"></p>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: