PHP Verification Code picture cannot output solution

Source: Internet
Author: User

PHP Verification code picture can not output

FileName global_func.php

function _code () {

Run Verification Code

Number of code with the notation

$_rnd_code=4;

For ($i =0 $i <$_rnd_code; $i + +) {

$_nmsg.=dechex (Mt_rand (0, 15));

}

Save in session

$_session[' Code ']=$_nmsg;

Long and high

$_width=75;

$_height=25;

Create a picture

$_img=imagecreatetruecolor ($_width,$_height);

White

$_white=imagecolorallocate ($_img, 255, 255, 255);

Fill

Imagefill ($_img, 0, 0, $_white);

$_flag=false;

if ($_flag) {

Black, border

$_black=imagecolorallocate ($_img, 0,0,0);

Imagerectangle ($_img, 0,0, $_width-1,$_height-1,$_black);

}

Draw 6 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,mt_rand (0,$_width), Mt_rand (0,$_height), Mt_rand (0,$_width), Mt_rand (0,$_height), $_rnd_color);

}

Immediately snowflakes

for ($i; $i <100; $i + +) {

$_rnd_color=imagecolorallocate ($_img,mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));

Imagestring ($_img,1, Mt_rand (1, $_width), Mt_rand (1,$_height), ' * ', $_rnd_color);

}

Output Verification Code

for ($i =0; $i

$_rnd_color=imagecolorallocate ($_img, Mt_rand (0), Mt_rand (0,), Mt_rand (0, 200));

Imagestring ($_img,5, $i *$_width/$_rnd_code+mt_rand (1,10), Mt_rand (1,$_HEIGHT/2), $_session[' Code ' [$i],$_rnd_ color);

}

Output image

Header (' content-type:image/png ');

Imagepng ($_IMG);

Destroyed

Imagedestroy ($_IMG);

}

This if I call global_func.php the function _code () under this file; The mistake is a red fork.

code.php

_code ();

This is what I write directly in the code.php can be used.

code.php

Number of code with the notation

$_rnd_code=4;

For ($i =0 $i <$_rnd_code; $i + +) {

$_nmsg.=dechex (Mt_rand (0, 15));

}

Save in session

$_session[' Code ']=$_nmsg;

Long and high

$_width=75;

$_height=25;

Create a picture

$_img=imagecreatetruecolor ($_width,$_height);

White

$_white=imagecolorallocate ($_img, 255, 255, 255);

Fill

Imagefill ($_img, 0, 0, $_white);

$_flag=false;

if ($_flag) {

Black, border

$_black=imagecolorallocate ($_img, 0,0,0);

Imagerectangle ($_img, 0,0, $_width-1,$_height-1,$_black);

}

Draw 6 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,mt_rand (0,$_width), Mt_rand (0,$_height), Mt_rand (0,$_width), Mt_rand (0,$_height), $_rnd_color);

}

Immediately snowflakes

for ($i; $i <100; $i + +) {

$_rnd_color=imagecolorallocate ($_img,mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));

Imagestring ($_img,1, Mt_rand (1, $_width), Mt_rand (1,$_height), ' * ', $_rnd_color);

}

Output Verification Code

for ($i =0; $i

$_rnd_color=imagecolorallocate ($_img, Mt_rand (0), Mt_rand (0,), Mt_rand (0, 200));

Imagestring ($_img,5, $i *$_width/$_rnd_code+mt_rand (1,10), Mt_rand (1,$_HEIGHT/2), $_session[' Code ' [$i],$_rnd_ color);

}

Output image

Header (' content-type:image/png ');

Imagepng ($_IMG);

Destroyed

Imagedestroy ($_IMG);

This verification code I run directly does not encapsulate the form of a function can be used to verify the code all normal.

But after I use the function _code () The call to _code () in HTML shows a red fork. I don't know why.

------Solution--------------------

Will not be header (' content-type:image/png '); The reason,

The BOM header affects the file type expected by the browser

------Solution--------------------

Even if it's written as a function, the call is fine.

  

function _code () {

Run Verification Code

}

?>

Another page

  

Include ("./test/test.php");

_code ();

------Solution--------------------

You will know why if you turn on the error message in PHP.

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.