PHP生成图片验证码demo【OOP面向对象版本】
下面是我今天下午用PHP写的一个生成图片验证码demo,仅供参考。
这个demo总共分为4个文件,具体代码如下:
1、code.html中的代码:
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>登录、注册验证码生成</title> </head> <body> <!-- * @Description 网站登录/注册验证码生成类 * @Author 赵一鸣 * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html * @Date 2016年10月6日 --> <form action="checkcode.php" method="post"> <input type="text" name="code" /><br/> <img src="/static/imghwm/default1.png" data-src="showcode.php" class="lazy" onclick="this.setAttribute('src','showcode.php?'+Math.random())" / alt="PHP가 이미지 확인 코드 데모를 생성합니다. [OOP 객체 지향 버전]" > <span>看不清?点击图片即可切换验证码</span><br/> <input type="submit" name="sub" value="登录/注册" /> </form> </body> </html>
2、createcode.class.php中的代码:
<?php /** * @Description 网站登录/注册验证码生成类 * @Author 赵一鸣 * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html * @Date 2016年10月6日 */ class Createcode{ //画布资源 public $img; //画布宽度 private $img_width; //画布高度 private $img_height; //画布颜色 private $img_bgcolor; //验证码文字内容 private $str_content; //生成的验证码内容 private $code_content; //验证码颜色 private $code_content_color; //构造函数 public function __construct($img_width,$img_height,$str_content,$code_content_color){ if($this->gdcheck()){ $this->img_width = $img_width; $this->img_height = $img_height; $this->str_content = $str_content; $this->code_content_color = $code_content_color; $this->get_code(); $this->session_code(); } } //生成画布 public function get_img(){ //定义画布 $this->img = imagecreatetruecolor($this->img_width, $this->img_height); //画布背景色 $this->img_bgcolor = imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //给画图填充背景色 imagefill($this->img, 0, 0, $this->img_bgcolor); //取得画布的宽高 $img_width = imagesx($this->img); $img_height = imagesy($this->img); //画布中插入验证码 imagestring($this->img, 5, ($this->img_width/3), ($this->img_height/2.5), $this->code_content, imagecolorallocate($this->img, hexdec(substr($this->code_content_color, 1,2)), hexdec(substr($this->code_content_color, 3,2)), hexdec(substr($this->code_content_color, 5,2)))); //画布中插入像素点 $this->get_pix(); //画布中插入直线 $this->get_line(); //画布显示 header('Content-type:image/png'); imagepng($this->img); } //生成验证码 private function get_code(){ $str_content_len = strlen($this->str_content); for($i=0;$i<4;$i++){ $this->code_content .= substr($this->str_content, mt_rand(0,$str_content_len-1),1); } } //生成像素点 private function get_pix(){ for($j=0;$j<300;$j++){ $image_pix .= imagesetpixel($this->img, mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255))); } return $image_pix; } //生成直线 private function get_line(){ for($l=0;$l<2;$l++){ $img_line .= imageline($this->img, mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255))); } return $img_line; } //session存储验证码 private function session_code(){ session_start(); $_SESSION['code'] = $this->code_content; } //判断程序是否支持GD库 private function gdcheck(){ if(extension_loaded('gd')){ return true; }else{ return false; exit(); } } }
3、checkcode.php中的代码:
<?php /** * @Description 网站登录/注册验证码生成类 * @Author 赵一鸣 * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html * @Date 2016年10月6日 */ header('Content-type:text/html;charset="utf-8"'); session_start(); if($_POST['code']!=''){ if($_SESSION['code']==$_POST['code']){ echo '<script type="text/javascript"> alert("验证码填写成功"); history.go(-1); </script>'; }else{ echo '<script type="text/javascript"> alert("验证码填写失败"); history.go(-1); </script>'; } }
4、showcode.php中的代码:
<?php /** * @Description 网站登录/注册验证码生成类 * @Author 赵一鸣 * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html * @Date 2016年10月6日 */ function __autoload($classname){ include strtolower($classname).'.class.php'; } //定义验证码的取值范围 $str_content = 'abcdefghijklmnopqrstuvwxyz0123456789'; //验证码文字颜色 $code_content_color = '#ffffff'; //初始化对象 $code = new Createcode(100,30,$str_content,$code_content_color); $code->get_img();
原文地址:http://www.zymseo.com/php/334.html
转载请注明出处!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

안전한 시험 브라우저
안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

Dreamweaver Mac版
시각적 웹 개발 도구

WebStorm Mac 버전
유용한 JavaScript 개발 도구

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

mPDF
mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.
