今天看了李炎恢老是 的php练习一下验证码生成,发现php东西也是很多。学习起来虽然没有java那么复杂,不过里面函数还是很难记住里面的内容。
函数的编程,里面函数太多了,要记住不容易。最近还是学习后端的内容。
作为一个一直学习前端的,现在重新去学习web的内容,还是比较吃力。
比起第一次学ios的时候,还是有着那种感觉。
好,明天还要继续学习。
<?php
session_start();
//随机码个数
$_codeNum = 4;
//生成验证码
$_nmsg ="";
for($i =0 ;$i<$_codeNum;$i++)
{
$_nmsg .= dechex(mt_rand(0, 15));
}
$_SESSION['code'] = $_nmsg;
//创建一张图像
$width = 75;
$height = 25;
$_img = imagecreatetruecolor($width, $height);
//白色
$_white = imagecolorallocate($_img, 255, 255, 255);
imagefill($_img, 0, 0, $_white);
//黑色
$_black = imagecolorallocate($_img, 0, 0, 0);
imagerectangle($_img, 0, 0, $width-1, $height-1, $_black);
//随机画出六个不同颜色的线条
for ($i=0; $i <6 ; $i++) {
$_rnd_color = imagecolorallocate($_img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imageline($_img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $_rnd_color);
}
随机雪花
$flag = false;
if($flag){
for ($i=0; $i <100 ; $i++) {
$_rnd_color = imagecolorallocate($_img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
imagestring($_img, 1, mt_rand(1, $width), mt_rand(1, $height/2), "*", $_rnd_color);
}
}
//输出验证码
for ($i=0; $i < strlen($_SESSION['code']); $i++) {
$_rnd_color = imagecolorallocate($_img, mt_rand(0, 100), mt_rand(0, 150), mt_rand(0, 200));
imagestring($_img,5,$i*$width/$_codeNum+mt_rand(1, 10),mt_rand(1, $height/2),$_SESSION['code'][$i],$_rnd_color);
}
//输出图像
header("Content-Type:image/png");
imagepng($_img);
//销毁
imagedestroy($_img);
?>