Joke Collection Website - Blessing messages - How PHP generates the verification code of addition and subtraction algorithm

How PHP generates the verification code of addition and subtraction algorithm

& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)

Namespace Move \ Component;

/**

* @ Author Huo Feng

*

* modified the addition and subtraction verification class.

* use the example verify code::get( 1, 2);

* verify the sample verify code:: check ($ code);

*/

Category verification code

{

/**

* php verification code

*/

Public static function get($ one, $two, $ prefix ='', $font_size = 28)

{

//file header ...

ob _ get _ clean();

header(“Content-type:image/png; charset = utf-8;” );

//Create true color white paper

$ width = $ font _ size * 5;

$ height = $ font _ size+ 1;

$ im = @ image create true color ($ width, $ height) or die ("image creation failed");

//Get the background color

$ background _ color = imagecolorrallocate($ im,255,255,255);

//Fill background color

image fill(im,0,0,$ background _ color);

//Get the border color

$ border _ color = imagecolorrallocate($ im,200,200,200);

//Draw a rectangle with a border color of 200,200,200.

image rectangle($ im,0,0,$width - 1,$height - 1,$ border _ color);

//Display the background line by line, and use 1 or 0 in full screen.

for($ I = 2; $ i & lt$ height-2; $ i++){

//Get random tones

$ line _ color = imagecolorrallocate($ im,rand(200,255),rand(200,255),rand(200,255));

//Draw a line

imageline($ im,2,$i,$width - 1,$i,$ line _ color);

}

//Set the printed text.

$ firstNum = $ one

$ secondNum = $ two

$ actionStr = $ firstNum & gt$secondNum? '-' : '+';

//Get the 1 th random character.

$ imstr【0】【“s“】= $ first num;

$ imstr【0】【“x“】= rand(2,5);

$ imstr【0】【“y“】= rand( 1,4);

//Get the second random word

$ imstr【 1】【“s“】= $ actionStr;

$ imstr【 1】【“x“】= $ imstr【0】【“x“】+$ font _ size- 1+rand(0, 1);

$ imstr【 1】【“y“】= rand( 1,5);

//Get the third random word

$ imstr【2】【“s“】= $ second num;

$ imstr【2】【“x“】= $ imstr【 1】【“x“】+$ font _ size- 1+rand(0, 1);

$ imstr【2】【“y“】= rand( 1,5);

//Get the third random word

$ imstr【3】【“s“】=‘=‘;

$ imstr【3】【“x“】= $ imstr【2】【“x“】+$ font _ size- 1+rand(0, 1);

$ imstr【3】【“y“】= 3;

//Get the third random word

$ imstr【4】【“s“】=‘? ‘。 ;

$ imstr【4】【“x“】= $ imstr【3】【“x“】+$ font _ size- 1+rand(0, 1);

$ imstr【4】【“y“】= 3;

//Text

$ text =““;

//Write a random string

for($ I = 0; $ i & lt5; $ i++){

//Get a random dark color

$ text _ color = imagecolorrallocate($ im,rand(50, 180),rand(50, 180),rand(50, 180));

$ text. = $ imstr【$ I】【“s“】;

//Draw words

imagechar($ im,$font_size,$ imstr【$ I】【“x“】,$ imstr【$ I】【“y“】,$ imstr【$ I】【“s“】,$ text _ color);

}

session _ start();

$ _ session [$ prefix. verify code‘】= $ first num & gt; $secondNum? ($ first num-$ second num):($ first num+$ second num);

//Show pictures

ImagePng($ im);

//Destroy the map

image destroy($ im);

}

Common static function check ($ TERM $code)

{

if(trim($ _ SESSION【$ prefix。 verify code‘】)= = trim($ code)){

Return true

} Otherwise {

Returns false

}

}