首页 > 语言 > PHP > 正文

php验证码

2024-09-04 11:32:20
字体:
来源:转载
供稿:网友

网上搜罗也可以搜罗一大堆,但是代码都不是很优美,我个人就这么认为,因为刚开始写php,经常要去官网看看文档,php的文档真是个宝库,只要你花点时间看下,你就会发现很多优美的程序方法。这个验证码是我组织的:

 

<?phpsession_start();// Generate a random character stringfunction rand_str($length = 32, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'){    // Length of character list    $chars_length = (strlen($chars) - 1);    // Start our string    $string = $chars{rand(0, $chars_length)};       // Generate random string    for ($i = 1; $i < $length; $i = strlen($string))    {        // Grab a random character from our list        $r = $chars{rand(0, $chars_length)};               // Make sure the same two characters don't appear next to each other        if ($r != $string{$i - 1}) $string .=  $r;    }       // Return the string    return $string;}$authnum_session=rand_str(4);$_SESSION["authnum_session"]=$authnum_session;//生成图片$im=imagecreate(60,20);$blank=imagecolorallocate($im,0,0,0);$white=imagecolorallocate($im,255,255,255);$gray=imagecolorallocate($im,200,200,200);imagefill($im,68,30,$gray);$li=imagecolorallocate($im,220,220,220);for($j=0;$j<3;$j++){ imageline($im,rand(0,30),rand(0,21),rand(20,40),rand(0,21),$li);}for($z=0;$z<90;$z++){ imagesetpixel($im,rand()%70,rand()%30,$gray);}imagestring($im,5,12,5,$authnum_session,$white);header("Content-Type:image/png");imagepng($im);?>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表