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

产生验证码

PHP中文网
Release: 2016-05-25 17:15:42
Original
1053 people have browsed it

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

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template