登录  /  注册
博主信息
博文 41
粉丝 2
评论 0
访问量 27800
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
php之session实战
月光下,遗忘黑暗
原创
545人浏览过

小项目

验证码session使用

  1. public static function create(){
  2. // 配置
  3. $config = self::_getCodeConfig();
  4. //创建画布
  5. $image = imagecreatetruecolor($config['width'],$config['height']);
  6. //背景颜色
  7. $bgcolor=imagecolorallocate($image,255,255,255);
  8. imagefill($image,0,0,$bgcolor);
  9. $captch_code = '';//存储验证码
  10. $captchCodeArr = str_split($config['codeStr']);
  11. //随机选取4个候选字符
  12. for($i=0;$i<$config['codeCount'];$i++){
  13. $fontsize = $config['fontsize'];
  14. $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));//随机颜色
  15. $fontcontent = $captchCodeArr[rand(0,strlen($config['codeStr'])-1)];
  16. $captch_code.=$fontcontent;
  17. $_x = $config['width']/$config['codeCount'];
  18. $x=($i*(int)$_x)+rand(5,10); //随机坐标
  19. $y=rand(5,10);
  20. imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); // 水平地画一行字符串
  21. //imagefttext($im, $size, 2, $size * (0.5 + $i * 1.1), $size * 1.2, $font, Env::get('root_path') . 'public/static/fonts/COOPBL.TTF', $authnum);
  22. }
  23. session_start();
  24. $_SESSION['code']=$captch_code;
  25. //增加干扰点
  26. if($config['disturbPoint']){
  27. for($i=0;$i<$config['pointCount'];$i++){
  28. $pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
  29. imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
  30. }
  31. }
  32. //增加干扰线
  33. if($config['disturbLine']){
  34. for($i=0;$i<$config['lineCount'];$i++){
  35. $linecolor=imagecolorallocate($image,rand(80,280),rand(80,220),rand(80,220));
  36. imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
  37. }
  38. }
  39. //输出格式
  40. header('Content-Type:image/png');
  41. imagepng($image);
  42. //销毁图片
  43. imagedestroy($image);
  44. }
批改老师:灭绝师太灭绝师太

批改状态:合格

老师批语:封装到类中比较好, 可以再壮阔你的类, 完成验证码验证
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学