PHP学习笔记(8)验证码使用session对比

时间:2021-04-19 16:47:08

PHP学习笔记(8)验证码使用session对比

知识点:

1. session获取其他页面的变量:

(1)先在画验证码php里开启session_start(),$_SESSION['随便起名']=验证码字符串,

(2)再在submit提交到action里的php里,开启session_start(),$str = $_SESSION['刚才随便起的名'],这样这个$str就等于验证码php里的‘验证码字符串’了,相当于用session中间过度了一下。

(3)然后与get得到的输入框里的验证码比较。

2. 两个验证码同时转大写。

3. 把数组里的验证码4个字母转成字符串,implode('',$arr)。

4. js的location重定向。

test.php

 <?php
session_start();
$vstring = $_SESSION["vstring"];
$vcode = $_GET["vcode"];
$username = $_GET["username"];
//$password = $_GET["password"];
// echo "$username";
// echo "$password";
// echo "$vcode";
echo($vstring);
//验证码全部转大写
$vcode = strtoupper($vcode);
$vstring = strtoupper($vstring);
if ($vcode==$vstring) {
echo "验证码正确!";
//js重定向
echo "<script>location='http://www.baidu.com'</script>";
}else{
echo "验证码错误!";
echo "<script>location='zhuce.php'</script>";
} ?>

yanzhengma.php

 <?php
//开启session
session_start();
// ob_clean();
header("content-type:image/png");
$width = 110;
$height = 40;
$img = imagecreatetruecolor($width, $height);
//$string = "hello";
//7种颜色,存入数组
$red = imagecolorallocate($img, 255, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
$aaa = imagecolorallocate($img, 255, 255, 0);
$bbb = imagecolorallocate($img, 0, 255, 255);
$ccc = imagecolorallocate($img, 255, 0, 255);
$colors = array($white,$red,$green,$blue,$aaa,$bbb,$ccc);
//颜色换成随机组成的RGB,每次循环都生成一次
$color = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
//画点
for ($i=0; $i < 10; $i++) {
$color1 = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($img, mt_rand(0,$width), mt_rand(0,$height), $color1);
}
//划线
for ($i=0; $i < 4; $i++) {
$color2 = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
imageline($img, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $color2);
}
//生成4位验证码
$a1 = range(0, 9);
$a2 = range(a, z);
$a3 = range(A, Z);
$a4 = array_merge($a1,$a2,$a3);
//改用shuffle打断顺序,array_slice取出前4个字母数字。不然如果用mt_rand在循环中每次取一个,还要生成字符串,不好比对
shuffle($a4);
$a5 = array_slice($a4,0,4);
$a6 = implode('', $a5);
//把验证码存到session
$_SESSION['vstring'] = $a6;
$num = 4;
$fontsize = 20;
for ($i=0; $i < 4; $i++) {
$color3 = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
imagettftext($img, $fontsize, mt_rand(-30,30), $width/$num*$i+5, 30, $color3, "Fonts/msyh.ttf", $a5[$i]);
}
imagepng($img);
?>

zhuce.php

 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="test.php" method = "get">
<table>
<tr>
<td>用户名:</td>
<td><input type="textarea" name="username" runat="server"><br/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"><br/></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name = "vcode" runat="server"><br/></td>
</tr>
<tr>
<td></td>
<td align="center" valign="center"><img src="yanzhengma.php" id = "yanzhengma" ></form></td>
</tr>
<tr>
<td><input type="submit" value="提交" ></td>
</tr>
<tr>
<td><input type="reset" value="重置"></td>
</tr>
</table>
<?php
$var = '
<script type="text/javascript">
onload = function(){
var yanzhengma = document.getElementById("yanzhengma");
yanzhengma.onclick = function(){
this.src = "yanzhengma.php?"+Math.random();
};
}
</script>
';
echo $var ?>
</body>
</html>