Blogger Information
Blog 41
fans 2
comment 0
visits 29231
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php之session实战
月光下,遗忘黑暗
Original
574 people have browsed it

小项目

验证码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. }
Correcting teacher:灭绝师太灭绝师太

Correction status:qualified

Teacher's comments:封装到类中比较好, 可以再壮阔你的类, 完成验证码验证
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post