PHP outputs the session verification code and the picture are not synchronized, and the picture is always a quick step to solve! At the end of this post, Xdawei edited PHP & nbsp; output session & nbsp; the verification code is not synchronized with the image. The image is always a quick step! & Lt ;? Session_start (); the session verification code output by fun PHP is not synchronized with the image. The image is always a quick step!
At the end of this post, Xdawei edited the PHP output session verification code at 20:07:42 on and the picture was not synchronized. the picture was always a quick step to solve!
session_start();
function random($len)
{
$srcstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
mt_srand();
$strs="";
for($i=0;$i<$len;$i++){
$strs.=$srcstr[mt_rand(0,35)];
}
return strtoupper($strs);
}
$str=random(4);
$width = 50;
$height = 25;
@header("Content-Type:image/png");
$_SESSION["captcha"] = $str;
//echo $str;
$im=imagecreate($width,$height);
$back=imagecolorallocate($im,0xFF,0xFF,0xFF);
$pix=imagecolorallocate($im,187,230,247);
$font=imagecolorallocate($im,41,163,238);
mt_srand();
for($i=0;$i<1000;$i++)
{
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
}
imagestring($im, 5, 7, 5,$str, $font);
imagerectangle($im,0,0,$width-1,$height-1,$font);
imagepng($im);
imagedestroy($im);
$_SESSION["captcha"] = $str;
?>
------ Solution --------------------
This problem is very strange. The best way is to separate it.
Try this.
Checkcode. class. php
/**
* Generate a verification code
* Class usage
* $ Checkcode = new checkcode ();
* $ Checkcode-> doimage ();
* // Get verification
* $ _ SESSION ['code'] = $ checkcode-> get_code ();
Session_start ();
Include './checkcode. class. php ';
$ Checkcode = new checkcode ('C: \ WINDOWS \ Fonts \ ARIAL. ttf ');
$ Checkcode-> doimage ();
$ _ SESSION ['code'] = $ checkcode-> get_code ();
*/
Class checkcode {
// Verification code width
Public $ width = 130;
// High verification code
Public $ height = 50;
// Set the font address
Private $ font;
// Set the font color
Public $ font_color;
// Set the random generation factor
Public $ charset = 'abcdefghkmnprstuvwyzabcdefghklmnprstuvwyz23456789 ';
// Set the background color
Public $ background = '# EDF7FF ';
// Number of characters for generating the verification code
Public $ code_len = 4;
// Font size
Public $ font_size = 20;
// Verification code
Private $ code;
// Image Memory
Private $ img;
// Place where the x axis of the text starts
Private $ x_start;
Function _ construct ($ fontpath ){
$ This-> font = $ fontpath;
}
/**
* Generate a random verification code.
*/
Protected function creat_code (){
$ Code = '';
$ Charset_len = strlen ($ this-> charset)-1;
For ($ I = 0; $ I <$ this-> code_len; $ I ++ ){
$ Code. = $ this-> charset [rand (1, $ charset_len)];
}
$ This-> code = $ code;
}
/**
* Obtain the verification code
*/
Public function get_code (){
Return strtolower ($ this-> code );
}
/**
* Generating images
*/
Public function doimage (){
$ Code = $ this-> creat_code ();
$ This-> img = imagecreatetruecolor ($ this-> width, $ this-> height );
If (! $ This-> font_color ){