+ -
当前位置:首页 → 问答吧 → 无聊的验证码儿,直接调用

无聊的验证码儿,直接调用

时间:2007-11-13

来源:互联网

直接调用就可以了
比如说:把下面的代码保存为verifyimg.php
那么可以在模板页面或者html页面直接使用
<img src='verifyimg.php' id='verify' ><a href='#'  onclick="document.getElementById('verify').src='verifyimg.php?No='+math.random();" >换个验证码</a>
生成的图片如下:
verify.png (236 Bytes)
2007-11-12 20:05
verify2.png (239 Bytes)
2007-11-12 20:05
verify3.png (248 Bytes)
2007-11-12 20:05

招初级phper的麻烦看下我的签名.看我写过的Class请点击这里
复制PHP内容到剪贴板
PHP代码:

<?php
session_start();
class image{
    var $Width = 50;
    var $Height = 20;
    var $Text=array(
        '0','1','2','3','4','5','6','7','8','9',
        'A','B','C','D','E','F','G','H','I','J',
        'K','L','M','N','O','P','Q','R','S','T',
        'U','V','W','X','Y','Z');
    function image(){
        if(!empty($_GET['W'])){$this->Width=intval($_GET['W']);}
         if(!empty($_GET['H'])){$this->Height=intval($_GET['H']);}
        header("Content-type: image/png");
        $IM = @imagecreate($this->Width,$this->Height); //创建一个空白图像
        $RGB = $this->rand_color();
        $Background_Color = imagecolorallocate($IM, $RGB[0], $RGB[1], $RGB[2]);//填充背景
        //$RGB = $this->rand_color();
        $RCO = imagecolorallocate($IM, $RGB[2], $RGB[1], $RGB[2]);
        $RGB = $this->rand_color();
        $RCT = imagecolorallocate($IM, $RGB[2], $RGB[1], $RGB[0]);
        $Style = array($RCO,$RCO,$RCO,$RCO,$RCO,$RCO,$RCO,$RCO,$RCT,$RCT,$RCT,$RCT,$RCT,$RCT,$RCT,$RCT);
        imagesetstyle($IM,$Style);
        imageline($IM,0,0,$this->Width/rand(1,3),rand(1,$this->Height),IMG_COLOR_STYLED);//画一条线
        imageline($IM,$this->Width/rand(1,4),0,$this->Width,$this->Height,IMG_COLOR_STYLED);//画一条线
        imageline($IM,0,rand(0,$this->Height/2),$this->Width,rand(0,$this->Height),IMG_COLOR_STYLED);//画一条线

        $TC = imagecolorallocate($IM,255,255,255);
        $Text = $this->Text[rand(0,35)];
        $_SESSION['_VERIFY']=$Text;
        imagestring($IM,4,rand(0,$this->Width/4),rand(2,$this->Height/3),$Text,$TC);

        //$RGB = $this->rand_color();
        $TC = imagecolorallocate($IM,0,0,0);
        $Text = $this->Text[rand(0,35)];
        $_SESSION['_VERIFY'].=$Text;
        imagestring($IM,4,$this->Width/2-5,$this->Heigth/2+rand(0,5),$Text,$TC);

        $RGB = $this->rand_color();
        $TC = imagecolorallocate($IM,$RGB[0],$RGB[2],$RGB[1]);
        $Text = $this->Text[rand(0,35)];
        $_SESSION['_VERIFY'].=$Text;
        imagestring($IM,5,$this->Width-$this->Width/4,$this->Heigth/2+rand(1,7),$Text,$TC);
        imagepng($IM);
        imagedestroy($IM);
    }
    
    function rand_color(){
        $Red = rand(0,255);
        $Green = rand(0,255);
        $Blue = rand(0,255);
        return array($Red,$Green,$Blue);
    }
}
new image();
?>

[ 本帖最后由 lazy 于 2007-11-12 20:35 编辑 ]

作者: lazy   发布时间: 2007-11-12

首两个字符为白,黑是有原因的,目的是为了防止别人用取单色走迷宫走出验证码来
背景和三条线跟最后的一个字符是起干扰作用(不知道这样又没有效),当然也有可能干扰到用户...

作者: lazy   发布时间: 2007-11-12

要了

作者: dujiangtao   发布时间: 2007-11-12

不错。

作者: hoday   发布时间: 2007-11-15

鼓励原创精品。

作者: luzhou   发布时间: 2007-11-15

支持LZ

作者: promaker   发布时间: 2007-11-15

用antispam就行了
无聊也别做无意义的事啊

作者: cyrus1986910   发布时间: 2007-11-15

作者: dzjzmj   发布时间: 2007-11-16

支持原创呵...

作者: chcn001   发布时间: 2007-11-16

收下,学习

作者: adleyliu   发布时间: 2007-11-16

引用:
原帖由 cyrus1986910 于 2007-11-15 15:05 发表
用antispam就行了
无聊也别做无意义的事啊
antispam是啥东东啊?

作者: vsfree   发布时间: 2007-11-26

同上!! antispam是啥啊?

作者: CFC4N   发布时间: 2007-11-29

昵称: 312344505  时间: 2008-1-10 14:33

ding
study!

作者: 312344505   发布时间: 2008-01-10

热门下载

更多