Php registration code. how can this problem be solved?

Source: Internet
Author: User
Tags imagejpeg
Php registration code: the registration code of a website cannot be displayed. & nbsp; firefox & nbsp; see the following question & nbsp; (the server returns & nbsp; texthtml, & nbsp; php5.4) in the local environment & nbsp; change & nbsp; php & nbsp; to 5.3 & nbsp; to display the registration code. & Nbsp; the server returns & nbsp; imagejpeg below is the department debugging code to help analyze the php registration code
The registration code of a website cannot be displayed
In firefox, we can see the following problems (the server returns text/html, php5.4)

In the local environment, change php to 5.3 to display the registration code. The server returns image/jpeg

The following is the department debugging code. Why can't the verification code be properly displayed in php5.3 or later versions?

Session_register ("valicode ");
$ Width = 50; // define the length and width of the image first.
$ Height = isset ($ _ REQUEST ['height'])? $ _ REQUEST ['height']: 24;
$ Rand_str = "";
For ($ I = 0; $ I <4; $ I ++ ){
$ Rand_str. = chr (mt_rand (48, 57 ));
}
If (function_exists ("imagecreate ")){

Echo "fuction image create exists"; // added by stone on 20140724
$ _ SESSION ["valicode"] = strtolower ($ rand_str); // register a session

$ Img = imagecreate ($ width, $ height); // generate an image
Imagecolorallocate ($ img, 255,255,255); // image background color, ImageColorAllocate 1st definition color PHP considers it as background color
$ Black = imagecolorallocate ($ img, 127,157,185 );

For ($ I = 1; $ I <= 50; $ I ++) {// The Snowflake effect is displayed on the background.
Imagestring ($ img, 1, mt_rand (1, $ width), mt_rand (1, $ height), "#", imagecolorallocate ($ img, mt_rand (200,255 ), mt_rand (200,255), mt_rand (200,255 )));
}

For ($ I = 0; $ I <4; $ I ++) {// add text
Imagestring ($ img, mt_rand (2, 5), $ I * 10 + 6, mt_rand (2, 5), $ rand_str [$ I], imagecolorallocate ($ img, mt_rand (0,100 ), mt_rand (0,150), mt_rand (0,200 )));
}

Imagerectangle ($ img, $ width-1, $ height-1, $ black); // enclose the image with a black rectangle.

If (function_exists ("imagejpeg ")){
Ob_clean ();
Header ("content-type: image/jpeg \ r \ n"); imagejpeg ($ img );
} Else {
Ob_clean ();
Header ("content-type: image/png \ r \ n"); imagepng ($ img );
}

Imagedestroy ($ img );

} Else {
Echo "function image create NOT exists"; // added by stone on 20140724
$ _ SESSION ["valicode"] = "1234 ";
Ob_clean ();
Header ("content-type: image/jpeg \ r \ n ");
$ Fp = fopen ("./valicode.jpg", "r ");
Echo fread ($ fp, filesize ("./validate.jpg "));
Fclose ($ fp );
}

?>


------ Solution --------------------
Echo "fuction image create exists"; // added by stone on 20140724
Echo "function image create NOT exists"; // added by stone on 20140724
You can just remove the two sentences.

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.