首頁 >後端開發 >php教程 >php點擊可刷新驗證碼

php點擊可刷新驗證碼

墨辰丷
墨辰丷原創
2018-06-06 10:48:502803瀏覽

本篇主要介紹php點擊可刷新驗證碼,有興趣的朋友參考下,希望對大家有幫助。

驗證碼類別檔案CreateImg.class.php

<?php 
 
class ValidationCode 
{ 
private $width,$height,$codenum; 
public $checkcode;   //产生的验证码 
private $checkimage;  //验证码图片 
private $disturbColor = &#39;&#39;; //干扰像素 
 
function __construct($width=&#39;80&#39;,$height=&#39;20&#39;,$codenum=&#39;4&#39;) 
{ 
  $this->width=$width; 
  $this->height=$height; 
  $this->codenum=$codenum; 
} 
function outImg() 
{ 
  //输出头 
  $this->outFileHeader(); 
  //产生验证码 
  $this->createCode(); 
 
  //产生图片 
  $this->createImage(); 
  //设置干扰像素 
  $this->setDisturbColor(); 
  //往图片上写验证码 
  $this->writeCheckCodeToImage(); 
  imagepng($this->checkimage); 
  imagedestroy($this->checkimage); 
} 
 
private function outFileHeader() 
{ 
  header ("Content-type: image/png"); 
} 
 
private function createCode() 
{ 
  $this->checkcode = strtoupper(substr(md5(rand()),0,$this->codenum)); 
} 
 
private function createImage() 
{ 
  $this->checkimage = @imagecreate($this->width,$this->height); 
  $back = imagecolorallocate($this->checkimage,255,255,255); 
  $border = imagecolorallocate($this->checkimage,0,0,0);  
  imagefilledrectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$back); // 白色底 
  imagerectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$border);  // 黑色边框 
} 
 
private function setDisturbColor() 
{ 
  for ($i=0;$i<=200;$i++) 
  { 
  $this->disturbColor = imagecolorallocate($this->checkimage, rand(0,255), rand(0,255), rand(0,255)); 
  imagesetpixel($this->checkimage,rand(2,128),rand(2,38),$this->disturbColor); 
  } 
} 
 
private function writeCheckCodeToImage() 
{ 
  for ($i=0;$i<=$this->codenum;$i++) 
  { 
  $bg_color = imagecolorallocate ($this->checkimage, rand(0,255), rand(0,128), rand(0,255)); 
  $x = floor($this->width/$this->codenum)*$i; 
  $y = rand(0,$this->height-15); 
  imagechar ($this->checkimage, rand(5,8), $x, $y, $this->checkcode[$i], $bg_color); 
  } 
} 
function __destruct() 
{ 
  unset($this->width,$this->height,$this->codenum); 
} 
} 
?>

#包含檔案imgcode.php

<?php 
session_start(); 
require_once(&#39;CreateImg.class.php&#39;); 
$image = new ValidationCode(&#39;80&#39;,&#39;20&#39;,&#39;4&#39;);  //图片长度、宽度、字符个数 
$image->outImg(); 
$_SESSION[&#39;validationcode&#39;] = $image->checkcode; //存贮验证码到 $_SESSION 中 
?>

前台檔案demo.php

?php 
session_start(); 
$test = $_POST[&#39;test&#39;]; 
$test = strtoupper(trim($test)); 
$submit = $_POST[&#39;submit&#39;]; 
if(isset($submit)){ 
if($test==$_SESSION[&#39;validationcode&#39;]){ 
  echo &#39;true&#39;; 
} else { 
  echo &#39;false&#39;; 
} 
} 
?> 
 
<html> 
<head> 
<title>Image</title> 
<meta http-equiv="content-type" content="text/html;charset=utf-8"> 
<script language="javascript"> 
function newgdcode(obj,url) { 
obj.src = url+ &#39;?nowtime=&#39; + new Date().getTime(); 
//后面传递一个随机参数,否则在IE7和火狐下,不刷新图片 
} 
</script> 
<body> 
<img src="imgcode.php" alt="看不清楚,换一张" align="absmiddle" style="cursor: pointer;" onclick="javascript:newgdcode(this,this.src);" /> 
<form method="POST" name="form1" action="image.php"> 
<input type="text" name="test"> 
<br /> 
<input type="submit" name="submit" value="提交"> 
</form> 
</body> 
</head> 
</html>

總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

PHP匯出word的方法

#PHP jpgraph安裝及基本用法

php自訂分頁類別的原理

#

以上是php點擊可刷新驗證碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn