Home php教程 PHP源码 产生验证码

产生验证码

May 25, 2016 pm 05:15 PM

php代码

<?php 
//Session保存路径
$sessSavePath = dirname(__FILE__)."/data/sessions/";
if(is_writeable($sessSavePath) && is_readable($sessSavePath)){ 
	session_save_path($sessSavePath);
}
$letter = "";
//获取随机数字
for ($i=0; $i<3; $i++) {
  $letter .= chr(mt_rand(48,57));
}
//获取随机字母
for ($i=0; $i<3; $i++) {
  $letter .= chr(mt_rand(65,90));
}
//重构字符顺序
$strs = str_split($letter);
shuffle ($strs);
$rndstring = "";
while (list (, $str) = each ($strs)) {
  $rndstring .= $str;
}
//获取随机字符
//$letras = &#39;ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&#39;;
//for($i=0; $i<6; $i++) {
	//$rndstring .= chr(mt_rand(48,90));
//	$rndstring .= $letras[rand(0, 35)];
//}

//如果支持GD,则绘图
if(function_exists("imagecreate"))
{
	//PutCookie("dd_ckstr",strtolower($rndstring),1800,"/");
	session_register(&#39;dd_ckstr&#39;);
	$_SESSION[&#39;dd_ckstr&#39;] = strtolower($rndstring);
	$rndcodelen = strlen($rndstring);
  //图片大小
  //$im = imagecreate(85,20);
  $im = imagecreatefromgif("code.gif");
  //字体
  $font_type = dirname(__FILE__)."/data/ant1.ttf";
  //背景颜色
  //$backcolor = imagecolorallocate($im,190,190,120); 
  //边框色
  //$iborder = ImageColorAllocate($im, 0x71,0x76,0x67); 

  //字体色
  //不支持 imagettftext
  $fontColor = ImageColorAllocate($im, 0xff,0xff,0xff); 
  
  //支持 imagettftext
  $fontColor2 = ImageColorAllocate($im, 0xff,0xff,0xff);
  //阴影
  $fontColor1 = ImageColorAllocate($im, 0x33,0x33,0x33); 
  
  //杂点背景线
  //$lineColor1 = ImageColorAllocate($im, 130,220,245);
  //$lineColor2 = ImageColorAllocate($im, 225,245,255);
  
  //背景线
  //for($j=3; $j<=16; $j=$j+3) imageline($im,2,$j,83,$j,$lineColor1);
  //for($j=2; $j<83; $j=$j+(mt_rand(3, 10))) imageline($im,$j,2,$j-6,18,$lineColor2);
 
  //边框
  //imagerectangle($im, 0, 0, 84, 19, $iborder);
  
  $strposs = array();
  //文字
  for($i=0;$i<$rndcodelen;$i++){
	  if(function_exists("imagettftext")){
	  	$strposs[$i][0] = $i*13+6;
	  	$strposs[$i][1] = mt_rand(15,18);
	  	imagettftext($im, 5, 5, $strposs[$i][0]+1, $strposs[$i][1]+1, $fontColor1, $font_type, $rndstring[$i]);
	  } else{
	  	imagestring($im, 5, $i*13+6, mt_rand(2, 4), $rndstring[$i], $fontColor);
	  }
  }
  
  
  //文字
  for($i=0;$ijpg ->png
  if(function_exists("imagejpeg")){
  	header("content-type:image/jpeg\r\n");
  	imagejpeg($im);
  }else{
    header("content-type:image/png\r\n");
  	imagepng($im);
  }
  ImageDestroy($im);

}else{ //不支持GD,只输出字母 ABCD	
	//PutCookie("dd_ckstr","abcd",1800,"/");
	session_register(&#39;dd_ckstr&#39;);
	$_SESSION[&#39;dd_ckstr&#39;] = "abcd";
	header("content-type:image/jpeg\r\n");
	header("Pragma:no-cache\r\n");
  header("Cache-Control:no-cache\r\n");
  header("Expires:0\r\n");
	$fp = fopen("./vdcode.jpg","r");
	echo fread($fp,filesize("./vdcode.jpg"));
	fclose($fp);
}
?>
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)