当前位置:Gxlcms > PHP教程 > 解决phpsession验证码不现实的问题

解决phpsession验证码不现实的问题

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

本人一开始图片老显示不出来,显示的是一个裂了的图片,经查询发现可能由以下几种可能造成

1)在header("Content-type:image/png");前加如下一段代码

  1. ini_set('display_errors', 'Off');
本人就是由上面的问题造成。

附上生成验证码的图片的代码

  1. <!--?php
  2. ini_set('display_errors', 'Off');
  3. header("Content-type:image/png");
  4. session_start();
  5. $_SESSION['code'] = 'null';
  6. //初始化
  7. $width = '58';
  8. $height = '22';
  9. //$code = '';
  10. $tmptext ='';
  11. $bordercolor= '';
  12. for($i=0;$i<4;$i++)
  13. {
  14. $tmptext = rand(0,9);
  15. $code .= $tmptext;
  16. }
  17. $_SESSION['code'] = $code;
  18. //以下三句诗让浏览器不缓存
  19. @header("Expires:-1");
  20. @header("Cache-Control:no-store,private,posc-check=0,pre-check=0,max-age=0",FALSE);
  21. @header("Pragma:no-cache");
  22. if(function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized')
  23. && function_exists('imagecolorallocate') && function_exists('imagesetpixel')
  24. && function_exists('imagechar') && function_exists('imagecreatefromgif')
  25. && function_exists('imagepng')
  26. )
  27. {
  28. $im = imagecreate($width, $height);
  29. $backgroundcolor = imagecolorallocate($im, 255, 255, 255);
  30. $numorder = array(1,2,3,4);
  31. /**shuffle将数组打乱*/
  32. shuffle($numorder);
  33. /**array_flip返回一个被反转的数组,键值被处理值*/
  34. $numorder = array_flip($numorder);
  35. $x='';
  36. $y='';
  37. $text_color = '';
  38. for($i=1;$i<=4;$i++)
  39. {
  40. $x = $numorder[$i] * 13 + mt_rand(0,4) -2;
  41. $y = mt_rand(0,3);
  42. $text_color = imagecolorallocate($im, mt_rand(50,255), mt_rand(50,255), mt_rand(50,255));
  43. /**将字符画在$im图中,5表示字体的大小 $x+5 */
  44. imagechar($im, 5, $x+5, $y+3, $code[$numorder[$i]], $text_color);
  45. }
  46. $linenums = mt_rand(10,32);
  47. for($i=0;$i<=$linenums;$i++)
  48. {
  49. $linecolor = imagecolorallocate($im, 255, mt_rand(0,255), mt_rand(0,255));
  50. $linex = mt_rand(0,255);
  51. $liney = mt_rand(0,255);
  52. imageline($im, $linex, $liney, $linex+mt_rand(0,4)-2, $liney+mt_rand(0,4)-2, $linecolor);
  53. }
  54. for($i=0;$i<40;$i++)
  55. {
  56. $pointcolor = imagecolorallocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
  57. imagesetpixel($im, mt_rand(0,$width), mt_rand(0,$height), $pointcolor);
  58. }
  59. $bordercolor = imagecolorallocate($im, 150, 150, 150);
  60. imagerectangle($im, 0, 0, $width-1, $height-1, $bordercolor);
  61. imagepng($im);
  62. imagedestroy($im);
  63. }?-->



2)变量没初始化


3)在header("Content-type:image/png");前加入

ob_clean();



第二三中方法不确保可以解决问题,仅供参考。

人气教程排行