image.php
<?php
header("Content-Type:image/jpeg");

session_start();

//产生四位的随机验证码
$trueCode = "";
$code = "";
for($i=0;$i<4;$i++)
{
$r = rand(0,9);
$trueCode .= $r;//真实验证码

$code .= $r."";//显示验证码 

} $_SESSION["trueCode"] = $trueCode;

$img = imagecreatetruecolor(70,25);//创建一张图片,并指定宽、高

//画背景
$bg = imagecolorallocate($img,rand(100,255),rand(100,255),rand(100,255));//设置字体色
imagefilledrectangle($img,0,0,70,25,$bg);

$bg = imagecolorallocate($img,rand(0,150),rand(0,150),rand(0,150));
imagestring($img,5,5,5,$code,$bg);//把随机数画到图片

//画线
for($i=0;$i<2;$i++)
{
$lineColor = imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
imageline($img,rand(0,70),rand(0,25),rand(0,70),rand(0,25),$lineColor);
}

//画点
for($i=0;$i<100;$i++)
{
$pointColor = imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($img,rand(0,70),rand(0,25),$pointColor);
}

imagejpeg($img);

?>
index.php 中的验证码判断
if($userName !=Null)
{
if($checkCode == $trueCode)
{
$result = Manager::checkLogin($userName,$password);
if($result == Null)
{
echo "<script type='text/javascript'>";
echo "alert('用户名或密码不正确!');";
echo "window.location='admin/index.php';";
echo "</script>";
}
else
{
$_SESSION["userMsg"] = $result;
echo "<script type='text/javascript'>";
echo "alert('正在进入后台管理...');";
echo "window.location='admin/index.php';";
echo "</script>";
}
}
else
{
echo "<script type='text/javascript'>";
echo "alert($userName);";
echo "window.location='admin/index.php';";
echo "</script>";
}
}
<div class="loginDiv21">
         <input type="text" name="checkCode" size="8" maxlength="4"><img id="myImg" onclick="changeImg()" style="cursor: pointer;"
         src="image.php" title="看不清换一张" alt="看不清换一张" align="absmiddle">         
 </div>验证码总是不判断直接跳过求解决