• 2007-09-25

    php随机验证码(兼容ie&ff) - [技术空间]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://www.blogbus.com/lily64-logs/10032662.html

    写了个php随机验证码,其中用带参调用的方式来取消图片缓存。存档。
     
    validcode.htm
    <b o d y onload="i=0;document.getElementsByName('mytext')[0].value='';document.getElementsByName('mytext')[0].focus()">
    <form action=check.php method=post>
    <img src="validcode.php?id=-1" title="click to change" style="cursor:pointer" onclick=eval('this.src="validcode.php?id='+i+++'"')>
    <input type=text name=mytext>
    <input type=submit value=submit>
    </form>
     
    validcode.php
    <?php
    session_start();
    $width=100;
    $height=40;
    $img=imagecreatetruecolor($width,$height);
    $times=6;
    $arr1=range("a","z");
    $arr2=range(0,9);
    $arr3=range("A","Z");
    $arr=array_merge($arr1,$arr2,$arr3);
    $keys=array_rand($arr,$times);
    $str="";
    foreach($keys as $i)
    $str.=$arr[$i];
    $_SESSION["randValid"]=$str;
    for($i=0;$i<$times*2;$i++)
    {
    $color=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
    imageline($img,rand(0,$width),rand(0,$height),rand(0,$width),rand(0,$height),$color);
    $color=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
    imagesetpixel($img,rand(0,$width),rand(0,$height),$color);
    }
    $color=imagecolorallocate($img,225,225,255);
    imagestring($img,5,24,10,$str,$color);
    header("content-type:image/png");
    imagepng($img);
    imagedestroy($img);
    ?>
     
    check.php
    <?php
    session_start();
    $validCode=trim($_POST["mytext"]);
    if($validCode==$_SESSION["randValid"])
    echo "successed!";
    else
    echo "<s c r i p t>alert('try again pls!');window.location.href='validcode.htm';</s c r i p t>";
    ?>

    分享到: