php verification code not showing on screen

王林
Release: 2023-02-25 13:52:01
Original
3952 people have browsed it

php verification code not showing on screen

Reasons and solutions for the PHP verification code not being displayed:

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

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

3. The first line of PHP hides 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);   //释放图像资源
Copy after login

Recommended tutorial: PHP video tutorial

The above is the detailed content of php verification code not showing on screen. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!