产生验证码

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);
}
?>
ログイン後にコピー

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)