验证码类
<?php
class captcha{
private $code ="qwertyuioplkjhgfdsazxcvbnmMNBVCXZASDFGHJKLPOIUYTREWQ1234567890";
private $whide;
private $height;
private $code_num;
private $select_num;
private $language;
private $font;
function __construct($language='Chinnese',$whide=100,$height=30,$num=4){
$this->language=$language;
$this->whide=$whide;
$this->height=$height;
$this->code_num=$num;
$this->font = './111.ttf';
$this->get_cod();
}
function captcha($language,$whide,$height,$num){
$this->__construct($language,$whide,$height,$num);
}
function get_img(){
$img = imagecreatetruecolor($this->whide,$this->height);
$bgcolor= imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
imagefill($img,0,0,$bgcolor);
$this->draw($img);
$this->code($img);
imagepng($img);
imagedestroy($img);
}
function draw($img){
for ($i=0; $i<50;$i++){
$print_color = imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200));
imagesetpixel($img,rand(0,$this->whide),rand(0,$this->height),$print_color);
}
for ($i=0; $i<10;$i++){
$line_color = imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200));
imageline($img,rand(0,$this->whide),rand(0,$this->height),rand(0,$this->whide),rand(0,$this->height),$line_color);
}
}
function code($img){
$color = imagecolorallocate($img,rand(20,100),rand(20,100),rand(20,100));
for ($i = 0; $i < $this->code_num; $i++) {
if ($this->language == 'English') {
imagestring($img, 5, 15 + 20 * $i, rand(0, 15), $this->select_num[$i], $color);
}else{
echo 111;
imagettftext($img,14,6,10,rand(0, 15),$color,$this->font,mb_substr($this->select_num,$i,1));
}
}
}
function get_cod(){
$str='';
if ($this->language == 'English'){
for ($i=0 ;$i<$this->code_num;$i++){
$str.=$this->code{rand(0,strlen($this->code)-1)};
}
}else{
$text = file_get_contents('./test.txt');
while(1){
$i = mt_rand(0,mb_strlen($text, 'utf8'));
$str .= mb_substr($text,$i,1, 'utf8');
if (mb_strlen($str,'utf8')== $this->code_num){
break;
}
}
}
session_start();
$_SESSION['codes'] = $str;
$this->select_num = $str;
}
}
html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>验证码验证</title>
<style>
form{
display: flex;
align-items: center;
}
</style>
</head>
<body>
<form action="index.php" method="post">
<img id="img" src="code.php" alt="" style="cursor:pointer"><span id="btn" style="cursor:pointer">看不清换一张!</span>
<input type="text" name="code">
<input type="submit" value="提交">
</form>
</body>
</html>
<script>
var img = document.getElementById('img');
var btn = document.getElementById('btn');
btn.onclick=function(){
img.src="code.php";
}
img.onclick=function(){
img.src="code.php";
}
</script>
引用 php
<?php
header('content-type:text/html;charset=utf8');
session_start();
var_dump($_POST['code'] == $_SESSION['codes']);
if ($_POST['code'] == $_SESSION['codes']){
exit('<script>alert("验证码正确!!")</script>');
}
引用 code
<?php
header('content-type:text/html;charset=utf8');
header('content-type:image/png;');
require './captcha.php';
$captcha = new captcha('English',100,30,4,111);
$captcha->get_img();