php的验证码代码
- 提问者网友:感性作祟
- 2021-04-27 19:58
百度和gole的代码没有一个正确的。郁闷死
- 五星知识达人网友:第幾種人
- 2021-04-27 20:24
<?php
class validate {
private $width = '80';//验证码的宽度
private $height = '20';//验证码的高度
private $randcode = '';//验证码, 无需赋值,后面会随机生成
private $num = '4';//验证码的字数
private $interferon = '80';//干扰素数量
private $line ='2';//线条干扰条数
private $im = '';//无需赋值,图片自动生成
private function conten_type(){
header("Content_type:image/gif");
}
private function session_star(){
session_start();
}
private function random(){
$this->randcode = strtoupper(substr(md5(rand()),0,$this->num));
return $this->randcode;
}
private function resession(){
$_SESSION['code'] = $this->randcode;
}
private function create_image(){
$this->im = imagecreate($this->width,$this->height);
imagecolorallocate ($this->im, rand(50,60), rand(150,200),rand(230,250));
return $this->im;
}
private function create_interferon(){
for($i=0;$i<$this->interferon;$i++){
$infcolor = imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($this->im,rand(0,80),rand(0,20),$infcolor);
}
}
private function create_line(){
for($j=0;$j<$this->line;$j++){
$lineColor = imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
imageline($this->im,rand(0,80),rand(0,20),rand(0,80),rand(0,20),$lineColor);
}
}
private function read_text(){
for($i=0;$i<$this->num;$i++){
$textColor = imagecolorallocate($this->im,rand(0,100),rand(0,100),rand(0,100));
$x = rand(($this->width/$this->num*$i),($this->width/$this->num)*($i+1)-10);
$y = rand(2,$this->height-13);
imagechar($this->im,rand(4,5),$x,$y,$this->randcode[$i],$textColor);
}
}
public function show_image(){
$this->session_star();
$this->conten_type();
$this->random();
$this->resession();
$this->create_image();
$this->create_interferon();
$this->create_line();
$this->read_text();
imagepng($this->im);
imagedestroy($this->im);
}
}
$va = new validate();
$va->show_image();
?>