查看效果:php验证码5
php 代码如下:

<?php
session_start();

$authnum = random(6);
$_SESSION['yan'] = $authnum;

Header("Content-type: image/PNG");

$im = imagecreate(55, 18);
$red   = ImageColorAllocate($im, 153, 51, 0);
$white = ImageColorAllocate($im, 255, 204, 0);
$gray  = ImageColorAllocate($im, 102, 102, 0);

imagefill($im, 55, 18, $red);

for ($i = 0; $i < strlen($authnum); $i++) {
    imagestring($im, 6, 13 * $i + 4, 1, substr($authnum, $i, 1), $white);
}

for ($i = 0; $i < 100; $i++) {
    imagesetpixel($im, rand() % 55, rand() % 18, $gray);
}

ImagePNG($im);
ImageDestroy($im);

function random($length) {
    $hash  = '';
    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    $max   = strlen($chars) - 1;

    for ($i = 0; $i < $length; $i++) {
        $hash .= $chars[mt_rand(0, $max)];
    }

    return $hash;
}