当前位置:Gxlcms > PHP教程 > PHP中仿制ecshop验证码实例

PHP中仿制ecshop验证码实例

时间:2021-07-01 10:21:17 帮助过:5人阅读

这篇文章主要介绍了PHP中仿制 ecshop验证码实例,非常不错,具有参考借鉴价值,需要的朋友可以参考下

仿制ecshop验证码的代码如下所示:

  1. <?php
  2. //仿制ecshop验证码(四位大写字母和数字、背景)
  3. //处理码值(四位大写字母和数字组成)
  4. //所有的可能的字符集合
  5. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  6. $chars_len = strlen($chars); //集合长度
  7. //随机选取
  8. $code_len = 4;//验证码长度
  9. $code=''; //验证码值初始化
  10. for($i=0;$i<$code_len;++$i){
  11. //随机取得一个字符下标
  12. $rand_index = mt_rand(0,$chars_len-1);
  13. //利用字符串的下标操做,获得选择的字符
  14. $code .= $chars[$rand_index];
  15. }
  16. //echo $code;
  17. //存储于session中(用于校验)
  18. session_start();
  19. $_SESSION['code'] = $code;
  20. //验证码图像(已知的背景图片)
  21. //处理背景
  22. $bg_file= './captcha/captcha_bg' . mt_rand(1,5). '.jpg';
  23. //依据该图片,创建画布
  24. $image = imagecreatefromjpeg($bg_file);
  25. //简单的将字符串写在画布上的函数(imageString();)
  26. //imageString(画布,字体,位置X, 位置y,字符串内容,颜色);
  27. //字体:imagestring函数,使用的内置字体。由1-5表示。位置由字符串左上角的坐标决定。颜色也是需要预先分配好的。imagecolorallocate();
  28. //分配字体颜色(随机分配黑色或者白色)
  29. if(mt_rand(0,1)==1){
  30. $str_color = imagecolorallocate($image,0,0,0); //黑色
  31. }else{
  32. $str_color = imagecolorallocate($image,255,0xff,255);//白色
  33. }
  34. //内置5号字体
  35. $font = 5;
  36. //位置
  37. //画布大小
  38. $image_w = imagesx($image);
  39. $image_h = imagesy($image);
  40. //获得字体的宽和高
  41. $font_w = imagefontwidth($font);
  42. $font_h = imagefontheight($font);
  43. //获得字符串的宽高
  44. $str_w = $font_w * $code_len;
  45. $str_h = $font_h;
  46. //计算位置
  47. $str_x = ($image_w-$str_w) / 2;
  48. $str_y = ($image_h-$str_h) / 2;
  49. //字符串
  50. imagestring($image,$font,$str_x,$str_y,$code,$str_color);
  51. //
输出和销毁画布 header("content-type:image/jpeg"); imagejpeg($image); imagedestroy($image);

封装验证码工具类:

  1. //验证码工具类(将所有和验证码操作相关的全部封装到该类中)
  2. class Captcha{
  3. /*生成验证码*/
  4. public function makeImage($code_len=4){
  5. //仿制ecshop验证码(四位大写字母和数字、背景)
  6. //处理码值(四位大写字母和数字组成)
  7. //所有的可能的字符集合
  8. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  9. $chars_len = strlen($chars); //集合长度
  10. //随机选取
  11. $code=''; //验证码值初始化
  12. for($i=0;$i<$code_len;++$i){
  13. //随机取得一个字符下标
  14. $rand_index = mt_rand(0,$chars_len-1);
  15. //利用字符串的下标操做,获得选择的字符
  16. $code .= $chars[$rand_index];
  17. }
  18. //echo $code;
  19. //存储于session中(用于校验)
  20. @session_start();
  21. $_SESSION['code'] = $code;
  22. //验证码图像(已知的背景图片)
  23. //处理背景
  24. $bg_file= TOOL . './captcha/captcha_bg' . mt_rand(1,5). '.jpg';
  25. //依据该图片,创建画布
  26. $image = imagecreatefromjpeg($bg_file);
  27. //简单的将字符串写在画布上的函数(imageString();)
  28. //imageString(画布,字体,位置X, 位置y,字符串内容,颜色);
  29. //字体:imagestring函数,使用的内置字体。由1-5表示。位置由字符串左上角的坐标决定。颜色也是需要预先分配好的。imagecolorallocate();
  30. //分配字体颜色(随机分配黑色或者白色)
  31. if(mt_rand(0,1)==1){
  32. $str_color = imagecolorallocate($image,0,0,0); //黑色
  33. }else{
  34. $str_color = imagecolorallocate($image,255,0xff,255);//白色
  35. }
  36. //内置5号字体
  37. $font = 5;
  38. //位置
  39. //画布大小
  40. $image_w = imagesx($image);
  41. $image_h = imagesy($image);
  42. //获得字体的宽和高
  43. $font_w = imagefontwidth($font);
  44. $font_h = imagefontheight($font);
  45. //获得字符串的宽高
  46. $str_w = $font_w * $code_len;
  47. $str_h = $font_h;
  48. //计算位置
  49. $str_x = ($image_w-$str_w) / 2;
  50. $str_y = ($image_h-$str_h) / 2;
  51. //字符串
  52. imagestring($image,$font,$str_x,$str_y,$code,$str_color);
  53. //
输出和销毁画布 header("content-type:image/jpeg"); imagejpeg($image); imagedestroy($image); } }

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

ECSHOP中实现ajax弹窗登录功能

详解Ecshop后台如何添加新功能及权限设置

ECSHOP中Deprecated: preg_replace()报错的解决办法

以上就是PHP中仿制 ecshop验证码实例的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行