设为首页收藏本站

就爱编程论坛

 找回密码
 注册

人人连接登陆

无需注册,直接登录

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

查看: 443|回复: 0
打印 上一主题 下一主题

PHP验证码实例代码应用详细教程 [复制链接]

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    无聊
    2025-5-27 03:37:20
  • 签到天数: 366 天

    [LV.9]以坛为家II

    论坛先锋 学习至圣 荣誉成员 论坛元老 活跃之星 终极领袖

    我玩的应用:

    跳转到指定楼层
    楼主
    发表于 2011-3-30 03:39:35 |只看该作者 |倒序浏览

    <?php   
       
        @header("content-type:text/html; charset=UTF-8");     
       
        //打开session     
       
        session_start();     
       
    ?>   
       
    <html>   
       
        <head>   
       
           <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
       
           <title>PHP验证码示例</title>   
       
        </head>   
       
        <body>   
       
           验证码:<br/>   
       
           <iframe id="iimg" height="100" width=300 src="img.php" frameborder="0" ></iframe>   
       
           <br/>   
       
           <input type=button value="看不清,换一张">   
       
           <br>   
       
           <form action="validate.php" method="post">   
       
               输入验证码:<input name="imgId" style="width:60">   
       
               <input type="submit" value="确定">   
       
           </form>   
       
        </body>   
       
    </html>   

    二、以下是PHP验证码生成页面,该页面在第一页面中被<img>调用
    <?php   
       
    Header("Content-type: image/gif");     
       
    session_start();     
       
    //验证码为随机字符,以下是算法     
       
    $randval;     
       
    for($i=0;$i<7;$i++){     
       
        $randstr = mt_rand(ord('A'),ord('Z'));     
       
        srand((double)microtime()*1000000);     
       
        $randv = mt_rand(0,10);     
       
        if($randv%2==0){     
       
           $randval.=mt_rand(0,10);     
       
        }else{     
       
           $randval.=chr($randstr);     
       
        }     
       
    }     
       
    //注册PHP验证码到session     
       
    session_register($randval);     
       
    //以下是绘制验证码图     
       
    $height = 50;//图高     
       
    $width = 100;//图宽     
       
    $im = ImageCreateTrueColor($width, $height);     
       
    $white = ImageColorAllocate($im, 255, 255, 255);     
       
    $blue = ImageColorAllocate($im, 0, 0, 64);     
       
    ImageFill($im, 0, 0, $white);     
       
    srand((double)microtime()*1000000000);     
       
    ImageLine($im, mt_rand(0,$width/3), mt_rand(0,$height/3), mt_rand($width/3,$width), mt_rand($height/3,$height), $blue);     
       
    srand((double)microtime()*1000000);     
       
    ImageLine($im, mt_rand($width/3,$width), mt_rand(0,$height/3), mt_rand(0,$width/3), mt_rand(0,$height/3), $blue);     
       
    srand((double)microtime()*1000000);     
       
    ImageString($im,16 , mt_rand(0,$width - strlen($randval) * 10), mt_rand(0,$height-12), $randval, $blue);     
       
    ImageGIF($im);     
       
    ImageDestroy($im);     
       
    /*     
       
    需要注意的是:为了支持以上绘图函数,我们必须在PHP载入GD2图形处理库,可修改 php.ini 文件中的     
       
    ;extension=php_gd2.dll     
       
    为     
       
    extension=php_gd2.dll     
       
    即开启GD2库     
       
    */     
       
    ?>   
    <?php   
       
        @header("content-type:text/html; charset=UTF-8");     
       
        //打开session     
       
        session_start();     
       
    ?>   
       
    <html>   
       
        <head>   
       
           <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
       
           <title>PHP验证码示例</title>   
       
        </head>   
       
        <body>   
       
           验证码:<br/>   
       
           <iframe id="iimg" height="100" width=300 src="img.php" frameborder="0" ></iframe>   
       
           <br/>   
       
           <input type=button value="看不清,换一张">   
       
           <br>   
       
           <form action="validate.php" method="post">   
       
               输入验证码:<input name="imgId" style="width:60">   
       
               <input type="submit" value="确定">   
       
           </form>   
       
        </body>   
       
    </html>   


    二、以下是PHP验证码生成页面,该页面在第一页面中被<img>调用

    <?php   
       
    Header("Content-type: image/gif");     
       
    session_start();     
       
    //验证码为随机字符,以下是算法     
       
    $randval;     
       
    for($i=0;$i<7;$i++){     
       
        $randstr = mt_rand(ord('A'),ord('Z'));     
       
        srand((double)microtime()*1000000);     
       
        $randv = mt_rand(0,10);     
       
        if($randv%2==0){     
       
           $randval.=mt_rand(0,10);     
       
        }else{     
       
           $randval.=chr($randstr);     
       
        }     
       
    }     
       
    //注册PHP验证码到session     
       
    session_register($randval);     
       
    //以下是绘制验证码图     
       
    $height = 50;//图高     
       
    $width = 100;//图宽     
       
    $im = ImageCreateTrueColor($width, $height);     
       
    $white = ImageColorAllocate($im, 255, 255, 255);     
       
    $blue = ImageColorAllocate($im, 0, 0, 64);     
       
    ImageFill($im, 0, 0, $white);     
       
    srand((double)microtime()*1000000000);     
       
    ImageLine($im, mt_rand(0,$width/3), mt_rand(0,$height/3), mt_rand($width/3,$width), mt_rand($height/3,$height), $blue);     
       
    srand((double)microtime()*1000000);     
       
    ImageLine($im, mt_rand($width/3,$width), mt_rand(0,$height/3), mt_rand(0,$width/3), mt_rand(0,$height/3), $blue);     
       
    srand((double)microtime()*1000000);     
       
    ImageString($im,16 , mt_rand(0,$width - strlen($randval) * 10), mt_rand(0,$height-12), $randval, $blue);     
       
    ImageGIF($im);     
       
    ImageDestroy($im);     
       
    /*     
       
    需要注意的是:为了支持以上绘图函数,我们必须在PHP载入GD2图形处理库,可修改 php.ini 文件中的     
       
    ;extension=php_gd2.dll     
       
    为     
       
    extension=php_gd2.dll     
       
    即开启GD2库     
       
    */     
       
    ?>   


    三、这是验证页面,提示PHP验证码是否通过验证

    ___FCKpd___2



    REQUEST['imgId'];     

       

        $imgId_req = strtoupper($imgId_req);     

       

        //验证该字符串是否注册了session变量     

       

        if (session_is_registered($imgId_req)) {     

       

           echo "<font color=blue >通过验证!</font>";     

       

        } else {     

       

           echo "<font color=red >验证错误!</font>";     

       

        }     

       

        //关闭session,以清除所有注册过的变量     

       

        session_destroy();     

       

    ?>   
    分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    分享分享0 收藏收藏0 支持支持0 反对反对0 分享到人人 转发到微博
    [img=http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=fRUcHhYWGAQ9GxIFEBwUEVMeEhA]http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_02.png[/img]

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册 人人连接登陆

    晴云孤魂's Blog|就爱编程搜帖|手机版|Archiver|就爱编程论坛     

    GMT+8, 2025-7-3 20:43 , Processed in 0.110440 second(s), 27 queries .

    Powered by Discuz! X2

    © 2001-2011 Comsenz Inc.

    回顶部