Home > Backend Development > PHP Tutorial > A PHP generated verification code implementation code_PHP tutorial

A PHP generated verification code implementation code_PHP tutorial

WBOY
Release: 2016-07-13 10:45:43
Original
767 people have browsed it

Generate verification code A PHP verification code generation code is implemented, and the session directory is customized, so that it is not the default session file saving path in php.ini. For example, session_save_path below can redefine the directory.

Generate verification code
A PHP tutorial generates verification code implementation code and customizes the session directory so that it is not the default session file saving path in php.ini, as shown below

session_save_path, the directory can be redefined.
*/
$sesssavepath = dirname(__file__)."/../data/sessions/";
if(is_writeable($sesssavepath) && is_readable($sesssavepath)){ session_save_path($sesssavepath); }
session_start();
$vstr = '';
for($i=0; $i<4; $i++) $vstr .= chr(mt_rand(65,90));
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<=16;$j=$j+3){
imageline($img,2,$j,48,$j,$line1);
}
for($j=2;$j<52;$j=$j+(mt_rand(3,6))){
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<$vstrlen;$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();
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633002.htmlTechArticleGenerate verification code code A PHP generation verification code implementation code, customize the session directory, so that it is not The default session file saving path in php.ini is as follows: session_save_...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template