Function codes implemented by exemplary PHP GD library

yzm.php  

? < PHP 

// open a session 
session_start (); 

// create a canvas 
$ Image = as imagecreatetruecolor ( 100 , 30 ); 

// background color 
$ bgcolor = imagecolorallocate ($ Image, 255 , 255 , 255 ); 
ImageFill ($ Image, 10 , 10 , $ bgcolor); 

$ captch_code = '' ; // store codes 


//  // randomly selected four digits 
for ($ I = 0 ; $ I < . 4 ; $ I ++ ) { 
$ fontSize = 15 ; 
$ fontcolorimagecolorallocate = ($ Image, RAND ( 0 , 120 ), RAND ( 0 , 120 ), RAND ( 0 , 120 )); // random color 
$ fontcontent = RAND ( 0 , . 9 ); 
$ captch_code. = $ fontcontent; 
$ X = (I * $ 100 / . 4 ) + RAND ( . 5 , 10 ); // random coordinate 
$ Y = RAND ( . 5 , 10 ); 
the imagestring (Image $, $ fontSize, $ X, Y $, $ fontcontent, $ fontcolor); 
} 


// alphanumeric codes mixing
 // for($i=0;$i<4;$i++) {
// $fontsize = 10; //
// $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));//??????
// $data = 'abcdefghijklmnopqrstuvwxyz1234567890'; //数据字典
// $fontcontent = substr($data, rand(0, strlen($data)), 1);
// $captch_code.=$fontcontent;
// $x = ($i * 100 / 4) + rand(5, 10);
// $y = rand(5, 10);
// imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
// }

$_SESSION['code']=$captch_code; //Record, to facilitate later Enter the code comparison 

// increase the interference points 
for ($ I = 0 ; $ I < 200 is ; $ I ++ ) { 
$ pointColor = imagecolorallocate ($ Image, RAND ( 50 , 200 is ), RAND ( 50 , 200 is ), RAND ( 50 , 200 is )); 
imagesetpixel ($ Image, RAND ( . 1 , 99 ), RAND ( . 1 , 29 ), $ pointColor); //
 } 

// increase the interference lines 
for ($ I = 0 ; $ I < . 3 ; $ I ++ ) { 
$ LineColor = imagecolorallocate ($ Image, RAND (80 , 280 ), RAND ( 80 , 220 ), RAND ( 80 , 220 )); 
imageline ($ Image, RAND ( . 1 , 99 ), RAND ( . 1 , 29 ), RAND ( . 1 , 99 ), RAND ( . 1 , 29 ), $ LineColor); 
} 

// output format 
header ( ' Content-type: image.png ' ); 
imagepng ($ image); 

// destroy images 
imagedestroy ($ image);

 

yzm.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="yzm_ok.php" >
<input type="text" placeholder="用户名" name="username"><br>

<input type="password" placeholder="密码" name="password"><br>

<input type="text" placeholder="验证码" name="yzm" class="captcha"><br>

<img src="yzm.php" onclick="this.src='yzm.php?id='+Math.random()"/>

<button type="submit">登录</button>
</form>
</body>
</html>

 

 

yzm_ok.php

? < PHP 
 session_start (); // open a session 
IF ( $ _POST [ "YZM"] == $ _SESSION [ 'code' ]) {
 echo "the verification is successful" ; 
} 
the else {
 echo "verification failure" ; 
}

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/lc2817/p/11315588.html