Solve the Problem of unrealistic php session verification code.

Source: Internet
Author: User
Tags php session shuffle

Solve the Problem of unrealistic php session verification code.

At the beginning, the image is not displayed, but a split image is displayed. It may be caused by the following reasons:

1) Add the following code before header ("Content-type: image/png ");

ini_set('display_errors', 'Off');
I am caused by the above problems.

The code of the image that generates the verification code is attached.

<? Php ini_set ('display _ errors ', 'off'); header ("Content-type: image/png"); session_start (); $ _ SESSION ['code'] = 'null'; // initialize $ width = '58 '; $ height = '22'; // $ code = ''; $ tmptext = ''; $ bordercolor =''; for ($ I = 0; $ I <4; $ I ++) {$ tmptext = rand (0, 9 ); $ code. = $ tmptext;} $ _ SESSION ['code'] = $ code; // The following three poems prevent the browser from caching @ header ("Expires:-1 "); @ header ("Cache-Control: no-store, private, posc-check = 0, pre-check = 0, max-age = 0", FALSE); @ heade R ("Pragma: no-cache"); if (function_exists ('imagecreate') & function_exists ('imagecolorset') & function_exists ('imagecopyresized ') & function_exists ('imagecolorallocate') & function_exists ('imagesetpixel ') & function_exists ('imagechar') & function_exists ('imagecreatefromgif ') & function_exists ('imagepng ') {$ im = imagecreate ($ width, $ height); $ backgroundcolor = imagecolorallocate ($ im, 255,255, 25 5); $ numorder = array (,);/** shuffle will disrupt the array */shuffle ($ numorder);/** array_flip will return an array to be reversed, key value processed */$ numorder = array_flip ($ numorder); $ x = ''; $ y =''; $ text_color = ''; for ($ I = 1; $ I <= 4; $ I ++) {$ x = $ numorder [$ I] * 13 + mt_rand (0, 4)-2; $ y = mt_rand (50,255); $ text_color = imagecolorallocate ($ im, mt_rand (50,255), mt_rand (50,255), mt_rand )); /** draw characters in the $ im diagram. 5 indicates the font size $ x + 5 */imagechar ($ im, 5, $ x + 5, $ y + 3, $ code [$ nu Morder [$ I], $ text_color);} $ linenums = mt_rand (10, 32); for ($ I = 0; $ I <= $ linenums; $ I ++) {$ linecolor = imagecolorallocate ($ im, 255, mt_rand (0,255), mt_rand (0,255); $ linex = mt_rand (0,255); $ liney = mt_rand (0,255 ); imageline ($ im, $ linex, $ liney, $ linex + mt_rand ()-2, $ liney + mt_rand ()-2, $ linecolor );} for ($ I = 0; $ I <40; $ I ++) {$ pointcolor = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255) ); Imagesetpixel ($ im, mt_rand (0, $ width), mt_rand (0, $ height), $ pointcolor);} $ bordercolor = imagecolorallocate ($ im, 150,150,150 ); imagerectangle ($ im, 0, 0, $ width-1, $ height-1, $ bordercolor); imagepng ($ im); imagedestroy ($ im) ;}?>



2) the variable is not initialized.


3) add header ("Content-type: image/png ");

Ob_clean ();



Method 2 and 3 do not guarantee that the problem can be solved, which is for reference only.

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.