Verification code problems

Source: Internet
Author: User
Verification code question session_start ();
// Number of random codes
$ _ Rnd_code = 4;

// Create a random code
For ($ I = 0; $ I <$ _ rnd_code; $ I ++ ){
$ _ Nmsg. = dechex (mt_rand (0, 15 ));
}

// Save it in the session
$ _ SESSION ['code'] =$ _ nmsg;

// Set the length and height
$ _ Width = 75;
$ _ Height = 25;

// Create an image
$ _ Img = imagecreatetruecolor ($ _ width, $ _ height );

// White
$ _ White = imagecolorallocate ($ _ img, 255,255,255 );

// Fill
Imagefill ($ _ img, 0, 0, $ _ white );

// Black border
$ _ Black = imagecolorallocate ($ _ img, 0, 0 );
Imagerectangle ($ _ img, 0, 0, $ _ width-1, $ _ height-1, $ _ black );

// Random six lines
For ($ I = 0; $ I <6; $ I ++ ){
$ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imageline ($ _ img, rand (0, $ _ width), rand (0, $ _ height), rand (0, $ _ width), rand (0, $ _ height), $ _ rnd_color );

}
// Output the verification code

For ($ I = 0; $ I Imagestring ($ _ img, 5, $ I * $ _ width/$ _ rnd_code + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ['code'] ['$ I'], $ _ black );

}
The four characters output from a verification code result are all the same. let's take a look at what's going on.









Reply to discussion (solution)

Imagestring ($ _ img, 5, $ I * $ _ width/$ _ rnd_code + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ['code'] ['$ I'], $ _ black );
This should be the case
Imagestring ($ _ img, 5, $ I * $ _ width/$ _ rnd_code + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ['code'] [$ I], $ _ black );

(! ) Notice: Undefined variable: _ nmsg in \ demo. php on line 10
Call Stack
# Time Memory Function Location
1 0.0006 146856 {main} () .. \ demo. php: 0

(! ) Warning: Illegal string offset '$ I' in \ demo. php on line 42
Call Stack
# Time Memory Function Location
1 0.0006 146856 {main} () .. \ demo. php: 0

(! ) Warning: Illegal string offset '$ I' in \ demo. php on line 42
Call Stack
# Time Memory Function Location
1 0.0006 146856 {main} () .. \ demo. php: 0

(! ) Warning: Illegal string offset '$ I' in \ demo. php on line 42
Call Stack
# Time Memory Function Location
1 0.0006 146856 {main} () .. \ demo. php: 0

(! ) Warning: Illegal string offset '$ I' in \ demo. php on line 42
Call Stack
# Time Memory Function Location
1 0.0006 146856 {main} () .. \ demo. php: 0



Change it to this.

 

Thank you for making the money.

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.