php 验证码
来源: 慕课网教程
产生一张图片
<?php $image = imagecreatetruecolor(300,200); $white = imagecolorallocate($image, 255,255,255); $black = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $white); header(‘content-type: image/png‘); imagepng($image); imagedestroy($image); ?>
<?php $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.‘pic0.jpg‘; $contents = file_get_contents($filename); header(‘content-type: image/jpg‘); echo $contents; ?>
添加数字
<?php $image = imagecreatetruecolor(300,200); $white = imagecolorallocate($image, 255,255,255); $black = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $white); // 随机数字 for ($i = 0; $i < 4; $i++) { $fontsize = 6; $fontcontent = rand(0, 9); $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120)); $x = $i * 100/4 + rand(5, 10); $y = rand(5, 10); imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } header(‘content-type: image/png‘); imagepng($image); imagedestroy($image); ?>
干扰点和干扰线
for ($i = 0; $i < 150; $i++) { $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200)); imagesetpixel($image, rand(1, 99), rand(1, 29), $pointcolor); } for ($i = 0; $i < 4; $i++) { $linecolor = imagecolorallocate($image, rand(150, 200), rand(150, 200), rand(150, 200)); imageline($image, rand(1, 99), rand(1, 20), rand(1, 99), rand(1, 20), $linecolor); }
实现字母和数字的验证码
$data = ‘3456789abcdefghjkmnpqrstuvwxy‘; // 去掉 0 和 o, 1 和 l $len = strlen($data); for ($i = 0; $i < 4; $i++) { $fontsize = 8; $fontcontent = $data[rand(0, $len)]; $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120)); $x = $i * 100/4 + rand(5, 10); $y = rand(5, 10); imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); }
验证登陆
verify.php
<?php session_start(); $image = imagecreatetruecolor(100,30); $white = imagecolorallocate($image, 255,255,255); $black = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $white); $captch_code = ‘‘; $data = ‘3456789abcdefghjkmnpqrstuvwxy‘; $len = strlen($data); for ($i = 0; $i < 4; $i++) { $fontsize = 8; $fontcontent = $data[rand(0, $len)]; $captch_code .= $fontcontent; $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120)); $x = $i * 100/4 + rand(5, 10); $y = rand(5, 10); imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } $_SESSION[‘authcode‘] = $captch_code; for ($i = 0; $i < 150; $i++) { $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200)); imagesetpixel($image, rand(1, 99), rand(1, 29), $pointcolor); } for ($i = 0; $i < 4; $i++) { $linecolor = imagecolorallocate($image, rand(150, 200), rand(150, 200), rand(150, 200)); imageline($image, rand(1, 99), rand(1, 20), rand(1, 99), rand(1, 20), $linecolor); } header(‘content-type: image/png‘); imagepng($image); imagedestroy($image); ?>
form.php
<?php if (isset($_REQUEST[‘authcode‘])) { session_start(); if (strtolower($_REQUEST[‘authcode‘]) == $_SESSION[‘authcode‘]) { echo ‘<font color="#00C"> succees </font>‘; } else { echo ‘<font color="#C00"> fail </font>‘; } exit(); } ?> <!DOCTYPE html> <html lang="en-US"> <head> <meta charset="utf-8" /> <title>test</title> </head> <body> <form method="post" action="<?php echo $_SERVER[PHP_SELF]; ?>"> <p>验证码图片: <img border="1" src="./verify.php?r=<?php echo rand(); ?>" width="120px"></p> <p><input type="text" size="10" name="authcode" value=""></p> <p><input type="submit" value="submit"> </p> </form> </body> </html>
实现刷新
<?php if (isset($_REQUEST[‘authcode‘])) { session_start(); if ($_REQUEST[‘authcode‘] == $_SESSION[‘authcode‘]) { echo ‘<font color="#00C"> succees </font>‘; } else { echo ‘<font color="#C00"> fail </font>‘; } exit(); } ?> <!DOCTYPE html> <html lang="en-US"> <head> <meta charset="utf-8" /> <title>test</title> </head> <body> <form method="post" action="<?php echo $_SERVER[PHP_SELF]; ?>"> <p> 验证码图片: <img id="captcha_img" border="1" src="./verify.php?r=<?php echo rand(); ?>" width="120px"> <a href="javascript:void(0)" onclick="document.getElementById(‘captcha_img‘).src=‘./verify.php?r=‘+Math.random()">换一个?</a> </p> <p><input type="text" size="10" name="authcode" value=""></p> <p><input type="submit" value="submit"> </p> </form> </body> </html>
图片验证码
form_img.php
<?php session_start(); $table = array( ‘pic0‘ => ‘dog‘, ‘pic1‘ => ‘cat‘, ‘pic2‘ => ‘fish‘, ‘pic3‘ => ‘bird‘, ); $index = rand(0, 3); $value = $table[‘pic‘.$index]; $_SESSION[‘authcode‘] = $value; $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.‘pic‘.$index.‘.jpg‘; $contents = file_get_contents($filename); header(‘content-type: image/jpg‘); echo $contents; ?> <?php if (isset($_REQUEST[‘authcode‘])) { session_start(); if ($_REQUEST[‘authcode‘] == $_SESSION[‘authcode‘]) { echo ‘<font color= "#f00";>succeed</font>‘; } else { echo ‘<font color= "#00f";>fail</font>‘; } exit(); } ?> <!DOCTYPE html> <html lang="en-US"> <head> <meta charset="utf-8" /> <title>test</title> </head> <body> <form methos="post" action="<?php echo $_SERVER[PHP_SELF]; ?>"> <p>图片: <img id="captcha_img" src="./verify_img.php?r=<?php echo rand(); ?>" width="200" height="100"> <a href="javascript:void(0)" onclick="document.getElementById(‘captcha_img‘).src=‘./verify_img.php?r=‘+Math.random()">换一个</a> </p> <p><input type="text" name="authcode" size="10"></p> <input type="submit" value="submit"> </form> </body> </html>
verify_img.php
<?php session_start(); $table = array( ‘pic0‘ => ‘dog‘, ‘pic1‘ => ‘cat‘, ‘pic2‘ => ‘fish‘, ‘pic3‘ => ‘bird‘, ); $index = rand(0, 3); $value = $table[‘pic‘.$index]; $_SESSION[‘authcode‘] = $value; $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.‘pic‘.$index.‘.jpg‘; $contents = file_get_contents($filename); header(‘content-type: image/jpg‘); echo $contents; ?>
时间: 2024-10-13 22:19:12