|
最近在看PHP100的视频教程,呵呵,虽然以前编程天天都在用PHP,但是很多知识都是一知半解,看了PHP100的视频教程之后发现自己了解的东西真的很少。上面的视频教程很不错,推荐大家去看看。
今天看的是PHP生成验证码那节,按照教程做的,运行时却发现报错,如图:
源码如下:
for($i = 0; $i < 4; $i++){
$rand .= dechex(rand(1, 15)); //dechex将10进制转为16进制
}
$img = imagecreatetruecolor(60, 20); //创建图片
$bgcolor = imagecolorallocate($img, 180, 221, 247); //创建图片背景颜色
imagefill($img,0,0,$bgcolor);
//添加随机线条
for($i = 0; $i < rand(0, 5); $i++){
$linecolor = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
imageline($img, rand(0, 60), rand(0, 20), rand(0, 60), rand(0, 20), $linecolor);
}
//添加随机噪点
for($i = 0; $i < rand(0, 200); $i++){
$pixelcolor = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($img, rand(0, 60), rand(0, 20), $pixelcolor);
}
$color = imagecolorallocate($img, 255, 255, 255); //文字颜色
imagestring($img, 6, 10, 3, $rand, $color); //向图片写入文字
header('content-type:image/png');
imagepng($img);
网上找了一些相关资料,但基本上都说是在header前有输出,可是我什么也没有输出啊。于是到PHP100官方论坛找答案,发现真有人问,原来是$rand没有定义就是用了,只要在使用前加$rand = "'就可以了。为什么教程中没有报错呢?估计是关闭了apache的警告提示。
其实在看视频教程时我就在想难道$rand没有定义就可以使用吗?后来看到演示没有问题就没有再去想了。尽信书不如无书!求学时一定要亲自实践,必要时需要抱有怀疑态度。
本站原创,转载请标明:来自龙铭洪博客(http://blog.csdn.net/)
最近在看PHP100的视频教程,呵呵,虽然以前编程天天都在用PHP,但是很多知识都是一知半解,看了PHP100的视频教程之后发现自己了解的东西真的很少。上面的视频教程很不错,推荐大家去看看。
今天看的是PHP生成验证码那节,按照教程做的,运行时却发现报错,如图:
源码如下:
for($i = 0; $i < 4; $i++){
$rand .= dechex(rand(1, 15)); //dechex将10进制转为16进制
}
$img = imagecreatetruecolor(60, 20); //创建图片
$bgcolor = imagecolorallocate($img, 180, 221, 247); //创建图片背景颜色
imagefill($img,0,0,$bgcolor);
//添加随机线条
for($i = 0; $i < rand(0, 5); $i++){
$linecolor = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
imageline($img, rand(0, 60), rand(0, 20), rand(0, 60), rand(0, 20), $linecolor);
}
//添加随机噪点
for($i = 0; $i < rand(0, 200); $i++){
$pixelcolor = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($img, rand(0, 60), rand(0, 20), $pixelcolor);
}
$color = imagecolorallocate($img, 255, 255, 255); //文字颜色
imagestring($img, 6, 10, 3, $rand, $color); //向图片写入文字
header('content-type:image/png');
imagepng($img);
网上找了一些相关资料,但基本上都说是在header前有输出,可是我什么也没有输出啊。于是到PHP100官方论坛找答案,发现真有人问,原来是$rand没有定义就是用了,只要在使用前加$rand = "'就可以了。为什么教程中没有报错呢?估计是关闭了apache的警告提示。
其实在看视频教程时我就在想难道$rand没有定义就可以使用吗?后来看到演示没有问题就没有再去想了。尽信书不如无书!求学时一定要亲自实践,必要时需要抱有怀疑态度。 |
|