PHP outputs the session verification code and the picture are not synchronized, and the picture is always a quick step to solve! Solution

Source: Internet
Author: User
PHP outputs the session verification code and the picture are not synchronized, and the picture is always a quick step to solve! PHP outputs the session verification code and the picture are not synchronized, and the picture is always a quick step to solve! PHPcode & lt ;? Session_start (); functionrandom ($ len) {the session verification code output by PHP is not synchronized with the image. The image is always a quick step to solve!
PHP outputs the session verification code and the picture are not synchronized, and the picture is always a quick step to solve!

PHP code
  
  


PHP code
  


------ Solution --------------------
This problem is very strange. The best way is to separate it.
Try this.
Checkcode. class. php
PHP code
  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 verification code width is public $ width = 130; // the verification code height is public $ height = 50; // set the font address private $ font; // set the font color public $ font_color; // Set the random generation factor public $ charset = 'abcdef GhkmnprstuvwyzABCDEFGHKLMNPRSTUVWYZ23456789 '; // set the background color public $ background =' # EDF7FF '; // Number of characters generating the verification code public $ code_len = 4; // The font size public $ font_size; // verification code private $ code; // Image memory private $ img; // private $ x_start where the x axis of the text starts; function _ construct ($ fontpath) {$ this-> font = $ fontpath;}/*** generates 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 ;}/ *** get verification code */public function get_code () {return strtolower ($ this-> code);}/*** generate image */public function doimage () {$ code = $ this-> creat_code (); $ this-> img = imagecreatetruecolor ($ this-> width, $ This-> height); if (! $ This-> font_color) {$ this-> font_color = imagecolorallocate ($ this-> img, rand (0,156), rand (0,156), rand (0,156 ));} else {$ this-> font_color = imagecolorallocate ($ this-> img, hexdec (substr ($ this-> font_color, 1, 2), hexdec (substr ($ this-> font_color, 3, 2), hexdec (substr ($ this-> font_color, 5, 2);} // sets the background color $ background = imagecolorallocate ($ this-> img, hexdec (substr ($ this-> background, 1, 2), hexdec (substr ($ This-> background, 3, 2), hexdec (substr ($ this-> background, 5, 2); // draw a cabinet and set the background color. Imagefilledrectangle ($ this-> img, 0, $ this-> height, $ this-> width, 0, $ background); $ this-> creat_font (); $ this-> creat_line (); $ this-> output ();}/*** generate text */private function creat_font () {$ x = $ this-> width/$ this-> code_len; for ($ I = 0; $ I <$ this-> code_len; $ I ++) {imagettftext ($ this-> img, $ this-> font_size, rand (-30, 30), $ x * $ I + rand (1.4), $ this-> height, $ this-> font_color, $ this-> font, $ this-> code [$ I]); if ($ I = 0) $ this-> x_start = $ x * $ I + 5;}/*** draw line */private function creat_line () {imagesetthickness ($ this-> img, 3 ); $ xpos = ($ this-> font_size * 2) + rand (-5, 5); $ width = $ this-> width/2.66 + rand (3, 10 ); $ height = $ this-> font_size * 2.14; if (rand (0,100) % 2 = 0) {$ start = rand ); $ ypos = $ this-> height/2-rand (10, 30); $ xpos + = rand (5, 15);} else {$ start = rand (180,246 ); $ ypos = $ this-> height/2 + rand (10, 30);} $ end = $ start + rand (75,110); imagearc ($ this-> img, $ xpos, $ ypos, $ width, $ height, $ start, $ end, $ this-> font_color); if (rand (1,75) % 2 = 0) {$ start = rand (45,111); $ ypos = $ this-> height/2-rand (10, 30); $ xpos + = rand (5, 15 );} else {$ start = rand (200,250); $ ypos = $ this-> height/2 + rand (10, 30);} $ end = $ start + rand (75,100 ); imagearc ($ this-> img, $ this-> width *. 75, $ ypos, $ width, $ height, $ start, $ end, $ this-> font_color);}/*** output image */private function output () {header ("content-type: image/png \ r \ n"); imagepng ($ this-> img); imagedestroy ($ this-> img );}}

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.