效果如图:

?
文件都放到了同一文件夹下。
?
CreateImage.class.php
1 | <?phpclass ValidationCode{ private $width , $height , $codenum ; public $checkcode ;
|
登录后复制
?
checks.php
?
1 | <?phpini_set( 'display_errors' , 'off' );session_start();header( "content-type:image/png" );
|
登录后复制
?
index.php
?
1 | <?phpheader( "content-type:text/html; charset=utf-8" );session_start(); if (! empty ( $_POST [ "Submit" ])){ $checks = $_POST [ "checks" ]; if ( $checks == "" ){ echo "<script> alert('验证码不能为空');window.location.href='index.php';</script>" ;} if ( $checks == $_SESSION [check_checks]){ echo "<script> alert('用户登录成功!');window.location.href='index.php';</script>" ;} else { echo "<script> alert('您输入的验证码不正确!');window.location.href='index.php';</script>" ;}}?><html><head><meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" ><title>rand函数的应用</title><style type= "text/css" ><!--.STYLE1 { font-size: 12px; color: #FFFFFF; font-weight: bold;}.style2 {font-weight: bold; font-size: 12px;}--></style></head><body><form name= "form" method= "post" action= "" ><input type= "hidden" name= "Submit" id= "Submit" value= "1" /> <table width= "1003" border= "0" cellspacing= "0" cellpadding= "0" > <tr> <td width= "168" height= "169" background= "images/index_01.gif" > </td> <td width= "685" background= "images/index_02.gif" > </td> <td width= "150" background= "images/index_03.gif" > </td> </tr> <tr> <td width= "168" height= "311" background= "images/index_04.gif" > </td> <td background= "images/index_05.gif" ><table width= "675" height= "169" border= "0" cellpadding= "0" cellspacing= "0" > <tr> <td height= "43" align= "center" valign= "baseline" > </td> <td align= "center" valign= "middle" > </td> <td align= "center" valign= "baseline" > </td> </tr> <tr> <td width= "382" height= "24" align= "center" valign= "baseline" > </td> <td width= "207" height= "24" valign= "middle" ><span class = "style2" >用户名</span><span class = "STYLE1" > <input name= "txt_user" id= "txt_user" style= "height:20px " size= "10" > </span></td> <td width= "86" height= "24" align= "center" valign= "baseline" > </td> </tr> <tr> <td height= "24" align= "center" valign= "baseline" > </td> <td height= "24" valign= "middle" ><span class = "style2" >密码</span><span class = "STYLE1" > <input name= "txt_pwd" type= "password" id= "txt_pwd" style= "FONT-SIZE: 9pt; height:20px" size= "10" > </span></td> <td height= "24" align= "center" valign= "baseline" > </td> </tr> <tr> <td height= "24" align= "center" valign= "baseline" > </td> <td height= "24" valign= "middle" ><span class = "style2" >验证码</span><span class = "STYLE1" > <input name= "checks" size= "6" style= "height:20px " > <img src= "/static/imghw/default1.png" data-src= "./checks.php" class = "lazy" style= "max-width:90%" style= "max-width:90%" border= "0" align= "bottom" alt= "php验证码和不能正常显示解决办法" ></span> </td> <td height= "24" align= "center" valign= "baseline" > </td> </tr> <tr> <td height= "40" align= "center" valign= "baseline" > </td> <td align= "center" valign= "baseline" > <input type= "submit" id= "Submit" name= "Submit" value= "登录" /></td> <td align= "center" valign= "baseline" > </td> </tr> </table></td> <td background= "images/index_06.gif" > </td> </tr> <tr> <td height= "100" > </td> <td> </td> <td> </td> </tr> </table></form></body></html>
|
登录后复制
?
************************************容易出现的问题*************
?
?
?
1.未开启php_gd2.dll? 去php.ini将其前面的“ ;”去掉
2.上面的checks.php中
3.字符编码改为统一的
4.最容易忽略的一点:
? 你本机上的环境是将报错提示全部打开的,所以在输出时有系统的未定义提示信息也跟着一起输出了,这样生成的图片肯定就不正常了,方法有两,在ValidationCode.php和checks.php文件开头加上ini_set('display_errors',?'Off');??就ok了
?
?
?
**********************************************************
?