In PHP website development, the verification code can effectively protect our form is not maliciously submitted, but if not using formula verification code or Chinese character verification code, only the use of simple letters or digital verification code, such a verification code scheme is really safe?
We know that simple numbers or letters to verify the code is very easy to crack, but the formula verification code or Chinese character verification code is not easy to be cracked, so we recommend that when using the verification code, as far as possible with the formula verification code or Chinese character verification code.
Here are two types of verification code I wrote, useful friends can refer to the following:
1. Formula Verification Code:
Copy Code code as follows:
<?php
Session_Start ();
Header ("Content-type:image/png");
$num 1 = mt_rand (0,9);//First digit number
$num 2 = Mt_rand (1,9);//second digit number
$type _str = "+-*";//Method String Collection
$type = substr ($type _str,rand (0,2), 1);//Random method
$change = Mt_rand (1,3);
if ($change ==1) {
$code = "$num 1$type$num2=";
$result = "$verifyCode = $num 1$type$num2;";
eval ($result);
$_session[' authnum_session ' = $verifyCode;
}elseif ($change ==2) {
$result = "$verifyCode = $num 1$type$num2;";
eval ($result);
$code = $num 1. $type. " _= ". $verifyCode;
$_session[' authnum_session ' = $num 2;
}elseif ($change ==3) {
$result = "$verifyCode = $num 1$type$num2;";
eval ($result);
$code = "_". $type. $num 2. " = ". $verifyCode;
$_session[' authnum_session ' = $num 1;
}
$im = Imagecreate (68,28);
$black = Imagecolorallocate ($im, 0,0,0);
$white = Imagecolorallocate ($im, 255,255,255);
$gray = Imagecolorallocate ($im, 200,200,200);
$red = Imagecolorallocate ($im, 255, 0, 0);
Imagefill ($im, 0,0, $white);
Imagestring ($im, 5, 8, $code, $black);
For ($i =0 $i <70; $i + +) {
Imagesetpixel ($im, Mt_rand (0), Mt_rand (0), $black);
Imagesetpixel ($im, Mt_rand (0), Mt_rand (0), $red);
Imagesetpixel ($im, Mt_rand (0), Mt_rand (0), $gray);
}
Imagepng ($im);
Imagedestroy ($im);
?>
2. Chinese Character Verification Code:
Copy Code code as follows:
<?php
Session_Start ();
$ch _str= " One is in the not big this is the main people on the ground for a labor to move domestic to me to he will do to the school subordinate on the annual order of righteousness into the Ministry of the public can come into the peer said species of life-like leather and many children after the small machine also through the force line of the power of the high volume long party to the real home of the determination of the depth of water physics and chemistry of the two political Miyoshi 10 war without agricultural sex before the anti-body road map of the new open in the knot Things from when two also gifted team batches such as should be shaped to make the heart-like dry all direction to change the point of fertility and the interior of the part of the daily profit by the pressure of the gas industry for the whole number of fruit-phase guiding the base or month Mao ran it's not the least of it. Mi Qun-Lin's head is only four horses, but a gram and male hole leading into the seat of the emotional device and fly crude oil put up the title of the quality of the area to test a live audience is very teaching decision Hereby, the strong polar soil of the stone has been divided into the root of the total number of straight group to make cut nine you take the west holding the total material for re-election of the view of the seven Mody Mountain Cheng Hundred newspaper more see will be true Heat Conservation Committee has to repair the branch of the Vichan a few first Lao special six type with an east-south increase of the wind back to the southern canton, the north of the road to make a car to do business by the whole step of the class set number Levin installed that is not known as the axis of the solid solid speed Slavs set up the weaving calendar To find out the mouth broken condition of the gold boundary products to the end of the clear to the million accurate book surgery factory must be away from the sea and the children of the green only to testify to the low cross eight of the near note to do cloth door iron need to talk about the county soldiers to remove the teeth thousand wins the leaf rate of small shadow and white lattice effect on the blade of the push-and-drop cutter this election raises the German word to check the difference half enemy to start the piece to apply the sound to receive the Chinese to be prepared the name red to continue the sign difficult to deposit the body tight liquid sends the angle to reduce the floor The potential end sense to the divine he cun the fitting of non-engaged sub-grinding Group fire section of the Huang in accordance with the value of the United States to take the early morning wheat cutting letter row of the sound of the hit Su Zhangmi harm Hou Cao Tree fertilizer on the right side of the city strict diameter snail inspection left page resistant to su the English quickly called the bad move about Babbitt province black Wuppe with Hati only needle how to plant Beijing to help raise the king's eyes she grabbed the seedling-containing seedlings to discuss the source of the acid old but sufficient short stroke Xuan the first foot wave bearing powder to practise the fish with the test Engraved on enough Manfe to lose the bag to promote the branch of the Zhou Yan division Cuschun Super-negative sand seal to reduce the poverty reduction Yangyangiang mu-zhi-yuan-mu-wood-speaking ball towards medical school ancient rice song listen to the only transmission slip station another Wei word drum just write Liu Weilio fan for a set of friends limit more than a roll to create a law rain let the bone far help the beginning of the skin sowing the death of Poison Circle Wei Ti Xun control to call Cloud mutual with split grain mother practice Plug Steel top strategy double stay in the shield of the late silk female scattered welding work The cold penetration of the house of the plant, the light of the strong 0 room, the double deficiency of the heart and the lack of the pump, the Li Huanpan of the Shong, the magnetic-male-like trap, the slave-side run-cover swing, the contact star Pine, and the Xing-yi-guan-ji frame wide winter chapter wet bias eat hold valve mine fortress responsibility ripe hard price turn odd a pre-grade review read back co-damage cotton invading ash although spear thick mud open the egg box palm oxygen love stop once dissolved camp end of the net Meng Qian to do Russia shrink The sand retreat Chen to seek the strength of the weapon carrying cell young which peeling the clutch is still fresh bar rough medium drilling weak feet afraid of salt at the end of Yin Feng Printing bee urgent to take the spread of the Rang of the core margin of the central Wu-yuan-hui-zhi-Xu Night township long do you think the Lan Ying Groove B? Confucianism kill steam phosphorus difficult crystal plug fire Huan iron to complement our Bud Yongwa pour Matrix Carbon performance Granville-attached teeth Bud Yong WA Oblique irrigation Eugeshun pig Ocean rot Please, if the tail beam strong and violent vegetables spike Chu The Chinese and the green dragged the cattle to dye both the autumn to forge the jade summer to cure the Kinguizhou visit Blow RongCopper along the rollers call for the dry awakening of the head of the brain and the penetration of the gap furnace shell sulfur coal ying-cast adhesion to the thin blessing of the longitudinal choice of the fu Reyen smoke sentence pure gradually tillage run ze slow planting Rutch fan boundary tide across the cone Hitch lost ship false light man Zhehwai cut and put the tribute to the strength of the instrument to sink the sins of the ancestors of the car wear goods Zimping draw Feed Dragon Store keep build room song Cold Xi elder brother Wash and wipe out the belly of the mirror of the women and the fat Zhuang rub insurance praise clock shake the handle arguing bamboo valley selling chaos Theo Ober to catch up with the net of the frontal wall and find out the way to get rid of the town of Resistance to help the key to the Fu Qing Poly-around mo busy dance So Gu glue Yanghu nail benevolence sound track broken extended lamp to avoid the pan-Yong frequency Emperor Lluja Jie Ganno almost the constitution the island attacked who Honche the spot to know Ling Egg closed baby-release milk giant private silver Ychintan Dulle insulation bending performance recruit Shaoxing hu Expiratory Pain Peak 0 Wood spring afternoon jump Shang Dingqin slightly chasing beam depletion alkali excavation blade pile Herho chest balance and diligent film posts in the case of seedling-------------cut--------------------------------------------Lu Ah Jian Zun bean pull to thumper around seam police pick dirty ice Cambodia mouth what rice plastic send Zhao shout Mat Kang by the garden cavity ordered fragrant meat Brother house sensitive Sun Ling Ling cheat Hugh borrow Dan du ear planing Tiger pen Dilute kun tea drops shallow embrace Cave cover Lun Niang ton of immersion sleeve bead female mother purple play Tower hammer earthquake years old appearance Jie Yan Yan Po Strong appeal brush ruthless suddenly disaster make Joe Tang leak smell shen melt chlorine barren stem men who rob like pulp side Glass also loyal sing Mongolia to catch a lock, especially by Uzhi light permit rebel livestock capture rust sweep Bilibao core ye Jian Jing jiang calcium shoulder teng withered throw track Hall mix dad 祝励 Ken Wine rope Poor pond dry bubble bag lang feed aluminum Soft Canal of the old trade manure wall to the last of the ink to prevent and unload the air fog Crown C Street Lai Pui-chi------------------to squeeze the second suspension of the San-sen candy Sacred Concave pottery Words late silkworm million moment;
$len = Mb_strlen ($ch _str, "utf-8");//length of Chinese characters
$str = Array ();
For ($i =0 $i <4; $i + +) {
$pos = Mt_rand (0, $len-5);//Start position
$str [] = Mb_substr ($ch _str, $pos, 1, "utf-8");
}
$authnum _session = Implode ("", $str);
$_session[' authnum_session ' = $authnum _session; Record to session
Header ("Content-type:image/png");
The length and height of the picture
$image _x=100;
$image _y=50;
$im = Imagecreate ($image _x, $image _y);
Here to take pictures of white background
$bkg = Imagecolorallocate ($im, 255,255,255);
Display the font style, this is to put the file in the corresponding directory, if you do not have the file to the window font file to find a bar.
$FNT = "Simhei.ttf";
Assign some color to an image
$white =imagecolorallocate ($im, 234,185,95);
Draw an elliptical arc on the picture, specifying the next punctuation
Imagearc ($im, 8, 170, $white);
Imagearc ($im, 180, 7,50, 175, $white);
Draw a line segment on the picture, specifying the next punctuation
Imageline ($im, 20,20,180,30, $white);
Imageline ($im, 20,18,170,50, $white);
Imageline ($im, 25,50,80,50, $white);
Number of chaotic points
$noise _num=3000;
$line _num=50;
The colors of various confusing characters
$rectangle _color=imagecolorallocate ($im, 0XAA,0XAA,0XAA);
$noise _color=imagecolorallocate ($im, 0x00,0x00,0x00);
$font _color=imagecolorallocate ($im, 0x00,0x00,0x00);
for ($i =0; $i < $noise _num; $i + +)
{
Draw a single pixel at a coordinate point, which is defined as black.
Imagesetpixel ($im, Mt_rand (0, $image _x), Mt_rand (0, $image _y), $noise _color);
}
for ($i =0; $i < $line _num; $i + +)
{
$line _color=imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255));
Draw a line between two coordinates and the color is defined above.
Imageline ($im, Mt_rand (0, $image _x), Mt_rand (0, $image _y), Mt_rand (0, $image _x), Mt_rand (0, $image _y), $line _color);
}
For ($i =0 $i <4; $i + +)
{
Imagettftext ($im, Rand (18,20), Rand (0,20), rand ($image _X/4) * $i + $image _x/100, ($image _x/4) * $i + $image _X/8), rand ($ image_y/2+ $image _Y/10, $image _y/2+ $image _y/5), $font _color, $fnt, $str [$i]);
}
Imagepng ($im);
Imagedestroy ($im);
?>
Font file: Simhei.rar Click here to download the site.
Attention:
The Chinese character verification code executes separately, then obtains the session to discover the authentication code and the sessions content is inconsistent. However, when the SRC attribute in the IMG tag refers to the Chinese character verification code file, and then gets the session, the contents of the two are consistent.
Interested friends can further improve on this.