Home  >  Article  >  Backend Development  >  Solution to the problem that the verification code cannot be displayed in php

Solution to the problem that the verification code cannot be displayed in php

巴扎黑
巴扎黑Original
2018-01-09 11:28:097947browse

This article mainly introduces the relevant information about the reasons why the PHP verification code cannot be displayed and the solutions. Here is the explanation of the reasons why the PHP verification code cannot be displayed and the corresponding solutions. Friends in need can refer to the following

The reason why the PHP verification code cannot be displayed and the solution

1. If it is utf-8, it is possible that the BOM has not been cleared

2. In the Header("Content -type: image/PNG"); There was output before

3. The first line of PHP hides the code, such as spaces, carriage returns, etc. Solution code:


$image_width=70;           //设置图像宽度
$image_height=18;       //设置图像高度
$new_number=$_GET[num];
//$new_number=5;
$num_image=imagecreate($image_width,$image_height); //创建一个画布
imagecolorallocate($num_image,255,255,255);    //设置画布的颜色
$black=imagecolorallocate($num_image,0,0,0);
/**/for($i=0;$i<strlen($new_number);$i++){ //循环读取SESSION变量中的验证码
  $font=mt_rand(3,5);               //设置随机的字体
  $x=mt_rand(1,8)+$image_width*$i/4;        //设置随机字符所在位置的X坐标
  $y=mt_rand(1,$image_height/4);          //设置随机字符所在位置的Y坐标
  $color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));  //设置字符的颜色
  imagestring($num_image,$font,$x,$y,$new_number[$i],$color);     //水平输出字符
}
header("content-type:image/png");   //设置创建图像的格式
imagepng($num_image);     //生成PNG格式的图像
imagedestroy($num_image);   //释放图像资源

The above is the detailed content of Solution to the problem that the verification code cannot be displayed in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn