服务器之家

服务器之家 > 正文

基于php验证码函数的使用示例

时间:2020-03-23 13:25     来源/作者:PHP教程网
  1. function _code($_code_length = 4, $_width = 75, $_height = 25){ 
  2.     for($i=0;$i<$_code_length;$i++){ 
  3.         $_nmsg .= dechex(mt_rand(0,15)); 
  4.     } 
  5.     $_SESSION["code"] = $_nmsg; 
  6.     $_img = imagecreatetruecolor($_width, $_height); 
  7.  
  8.     $_white = imagecolorallocate($_img, 250, 250, 250); 
  9.  
  10.     imagefill($_img, 0, 0, $_white); 
  11.  
  12.     $_gray = imagecolorallocate($_img, 196, 196, 196); 
  13.  
  14.     imagerectangle($_img, 0, 0, $_width-1, $_height-1, $_gray); 
  15.  
  16.     for ($i=0; $i < 6; $i++) {  
  17.         $_md_color = imagecolorallocate($_img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255)); 
  18.         imageline($_img, mt_rand(0,$_width), mt_rand(0, $_height),mt_rand(0,$_width), mt_rand(0, $_height), $_md_color); 
  19.     } 
  20.  
  21.     for ($i=0; $i < 50; $i++) {  
  22.         $_md_color = imagecolorallocate($_img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255)); 
  23.         imagestring($_img, 1, mt_rand(1,$_width-5), mt_rand(1, $_height-5), "*", $_md_color); 
  24.     } 
  25.  
  26.     for ($i=0; $i < $_code_length ; $i++) {  
  27.         $_md_color = imagecolorallocate($_img, mt_rand(0,102), mt_rand(0,102), mt_rand(0,102)); 
  28.         imagestring($_img, 5, $i * $_width/$_code_length+ mt_rand(1, 10), mt_rand(1, $_height/2), $_SESSION["code"][$i], $_md_color); 
  29.     } 
  30.  
  31.     header("Content-Type:image/png"); 
  32.  
  33.     imagepng($_img); 
  34.  
  35.     imagedestroy($_img); 
标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国 2021-05-08
返回顶部