首页 | 相关下载 | 我的相册 | 网页制作 | 音频制作 | 视频制作 | 会员登录 | 会员投稿
 当前位置:首页>>网页制作>>正文
Php验证码(php校验码)程序
文章出处:锐利时空   发布时间:2006-10-26  点击次数:
 

 以下代码为验证程序片断:


PHP代码:--------------------------------------------------------------------------------
  //randCode为页面根据图像输入的验证码输入框
    $pageRandCode = $_POST['randCode'];
    session_start();
    if($_SESSION["verifyCode"] != $pageRandCode) {
        error("验证码不正确!");//error为处理错误的子函数
    }

--------------------------------------------------------------------------------


  以下代码为输出图像程序,调用url形为:http://www.xxx.com/verifyCode.php?width=100&height=20&font=5;这些参数可不输入,都有默认值。


PHP代码:--------------------------------------------------------------------------------

<?
// ========================== 文件说明 ==========================//
// 文件说明:PHP验证码(PHP校验码)程序
// --------------------------------------------------------------//
// 程序作者:Sindo(锐利工作室)
// --------------------------------------------------------------//
// 程序版本:0.1
// --------------------------------------------------------------//
// 程序主页:<a href="http://www.wreny.com" target="_blank">http://www.wreny.com</a>
// --------------------------------------------------------------//
// Copyright (C) Wreny Studio. All Rights Reserved.
// ==============================================================//
    //随机字符串种子,可以换成字母或其他英文字符
    $glbVerifySeed = "1234567890";
    main();
       
    function main() {
        session_start();
       
        $verifyCode = getRandomCode();
        $_SESSION["verifyCode"] = $verifyCode;
        $imgWidth = $_REQUEST["width"];
        $imgHeight = $_REQUEST["height"];
        $imgFont = $_REQUEST["font"];
       
        if($imgWidth == "") $imgWidth = 80;
        if($imgHeight == "") $imgHeight = 18;
        if($imgFont == "") $imgFont = 6;
        doOutputImg($verifyCode, $imgWidth, $imgHeight, $imgFont);
    }
   
    //获取随机数字字符串
    function getRandomCode($length=6) {
        global $glbVerifySeed;
       
        $bgnIdx = 0;
        $endIdx = strlen($glbVerifySeed)-1;
       
        $code = "";
        for($i=0; $i<$length; $i++) {
            $curPos = rand($bgnIdx, $endIdx);
            $code .= substr($glbVerifySeed, $curPos, 1);
        }
       
        return $code;
    }
   
    //输出校验码图像
    function doOutputImg($string, $imgWidth, $imgHeight, $imgFont,
        $imgFgColorArr=array(0,0,0), $imgBgColorArr=array(255,255,255)) {
        $image = imagecreatetruecolor($imgWidth, $imgHeight);

        //用白色背景加黑色边框画个方框
        $backColor = imagecolorallocate($image, 255, 255, 255);
        $borderColor = imagecolorallocate($image, 0, 0, 0);
        imagefilledrectangle($image, 0, 0, $imgWidth - 1, $imgHeight - 1, $backColor);
        imagerectangle($image, 0, 0, $imgWidth - 1, $imgHeight - 1, $borderColor);

        $imgFgColor = imagecolorallocate ($image, $imgFgColorArr[0], $imgFgColorArr[1], $imgFgColorArr[2]);
        doDrawStr($image, $string, $imgFgColor, $imgFont);
        doPollute($image, 64);

        header('Content-type: image/png');
        imagepng($image);
        imagedestroy($image);
    }

    //画出校验码
    function doDrawStr($image, $string, $color, $imgFont) {
        $imgWidth = imagesx($image);
        $imgHeight = imagesy($image);
       
        $count = strlen($string);
        $xpace = ($imgWidth/$count);
       
        $x = ($xpace-6)/2;
        $y = ($imgHeight/2-8);
        for ($p = 0; $p<$count;  $p ++) {
            $xoff = rand(-2, +2);
            $yoff = rand(-2, +2);
            $curChar = substr($string, $p, 1);
            imagestring($image, $imgFont, $x+$xoff, $y+$yoff, $curChar, $color);
            $x += $xpace;
        }

        return 0;
    }
   
    //画出一些杂点
    function doPollute($image, $times) {  
        $imgWidth = imagesx($image);
        $imgHeight = imagesy($image);
        for($j=0; $j<$times; $j++) {
            $x = rand(0, $imgWidth);
            $y = rand(0, $imgHeight);
           
            $color = imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255));
            imagesetpixel($image, $x, $y, $color);
        }
    }
?>


 

[返回顶部↑]  [推荐好友] [查看评论]  
用户名: 新注册) 密码: 匿名评论 [查看评论]  发表评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 
↑文章搜索
  关键字:  
  范  围:  
  开始搜索  
※相关文章※
 

◎浅谈收费网站遇到的威胁
◎用PHP发送有附件的电子邮件
◎用PHP制作的意见反馈表
◎“自动转向”技术
◎[PHP]关于时间计算
◎iframe元素的用法
◎日历代码(含农历)

 
※热点文章※
  ·下拉菜单实例代码
·日历代码(含农历)
·动态下拉菜单实例代码
·让你的网站快速被搜索引擎
·iframe元素的用法
·要GOOGLE百分百收录的一种
·浅谈收费网站遇到的威胁
 
Copyright ? 2005 老谢工作室 All Rights Reserved
版权为 老谢工作室www.laoxie-studio.com 所有 未经同意不得复制或镜像
站长:焚香听雨 QQ:378292227 鄂ICP备05024977号