PHP output session verification code and the picture is not synchronized, the picture is always fast one step, solve

Source: Internet
Author: User
Tags set background
PHP output session verification code and the picture is not the same step, the picture is always one step faster, solve!
This post was last edited by Xdawei on 2012-04-18 20:07:42

PHP output Session verification code and the picture is not the same step, the picture is always one step faster, 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);
}



@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--------------------
The problem is very strange, the best way is to separate.
Try this one.
checkcode.class.php

/**
* Generate 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 {
The width of the verification code
Public $width = 130;

Verification Code of the high
Public $height = 50;

Set the address of the font
Private $font;

Set the font color
Public $font _color;

Set the random generation factor
Public $charset = ' abcdefghkmnprstuvwyzABCDEFGHKLMNPRSTUVWYZ23456789 ';

Set Background color
Public $background = ' #EDF7FF ';

Generate Verification Code character count
Public $code _len = 4;

Font size
Public $font _size = 20;

Verification Code
Private $code;

Image memory
Private $img;

Where the text x-axis begins
Private $x _start;

function __construct ($fontpath) {
$this->font = $fontpath;
}
/**
* Generate 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;
}

/**
* Get Verification Code
*/
Public Function Get_code () {
Return Strtolower ($this->code);
}

/**
* Create picture
*/
Public Function Doimage () {
$code = $this->creat_code ();
$this->img = Imagecreatetruecolor ($this->width, $this->height);
if (! $this->font_color) {
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.