Home >php教程 >php手册 >一款php生成验证码实现代码

一款php生成验证码实现代码

WBOY
WBOYOriginal
2016-06-13 09:48:391028browse

生成验证码代码 一款php生成验证码实现代码,把自定义了session的目录,这样就不是php.ini里默认的session文件保存路径了,如下面session_save_path,可重新定义目录。

生成验证码代码
一款php教程生成验证码实现代码,把自定义了session的目录,这样就不是php.ini里默认的session文件保存路径了,如下面

session_save_path,可重新定义目录。
*/
$sesssavepath = dirname(__file__)."/../data/sessions/";
if(is_writeable($sesssavepath) && is_readable($sesssavepath)){ session_save_path($sesssavepath); }
session_start();
$vstr = '';
for($i=0; $i if(function_exists("imagecreate")){
 $ntime = time();
 if(empty($_session['verifycode_last']) || empty($_session['verifycode']) || ($ntime - $_session['verifycode_last'] > 5)){
  $_session['verifycode'] = strtolower($vstr);
  $_session['verifycode_last'] = $ntime;
 }
 $vstr = $_session['verifycode'];
 $vstrlen = strlen($vstr);
 $img = imagecreate(50,20);
 imagecolorallocate($img, 255,255,255);
 $line1 = imagecolorallocate($img,240,220,180);
 $line2 = imagecolorallocate($img,250,250,170);
 for($j=3;$j   imageline($img,2,$j,48,$j,$line1);
 }
 for($j=2;$j   imageline($img,$j,2,$j-6,18,$line2);
 }
 $bordercolor = imagecolorallocate($img, 0x99,0x99,0x99);
 imagerectangle($img, 0, 0, 49, 19, $bordercolor);
 $fontcolor = imagecolorallocate($img, 48,61,50);
 for($i=0;$i   $bc = mt_rand(0,1);
  $vstr[$i] = strtoupper($vstr[$i]);
  imagestring($img, 5, $i*10+6, mt_rand(2,4), $vstr[$i], $fontcolor);
 }

 header("pragma:no-cachern");
 header("cache-control:no-cachern");
 header("expires:0rn");

 if(function_exists("imagejpeg")){
  header("content-type:image/jpegrn");
  imagejpeg($img);
 }else{
  header("content-type:image/pngrn");
  imagepng($img);
 }
 imagedestroy($img);
 exit();
}

Statement:
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
Previous article:php生成验证码代码Next article:php 创建图像实例