验证码

作者: 大菜鸟呀 | 来源:发表于2018-08-20 20:38 被阅读3次

HTML


<body>
<form action="zhuce.php" method="POST">
    <label>
        <p>用户名:</p>
        <input type="text" name="username">
    </label>
    <label>
        <p>密码:</p>
        <input type="password" name="password">
    </label>
    <label>
    
        <p>验证码:</p>
        <img src="yanzhengma.php" id="yzm"  onclick="javascript:this.src=this.src+'?time='+Math.random();"><br>
        <!--<img src="yanzhengma.php" id="yzm"  onclick="yzmround()"><br>-->
        <input type="text"  name="yzm" autocomplete="off">

    </label>    
    <label>
        <input type="submit" value="提交">
    </label>

</form>
</body>
<script>
    
    function yzmround(){

        var yzm=document.getElementById('yzm')
            yzm.src='yanzhengma.php?'+Math.random()
        
    }
</script>

验证码PHP

<?php
session_start(); 
$img=imagecreatetruecolor(100, 35);

$bcak=imagecolorallocate($img, 238, 232, 170);
$write=imagecolorallocate($img, 0, 0, 255);
$greens=imagecolorallocate($img, 144, 238, 144);



imagefill($img, 0, 0, $bcak);
//随机验证码
$arr=array_merge(range(0,9),range(A,Z),range(a,z));
shuffle($arr);
$str=join(' ',array_slice($arr, 0,4));

$yzms=implode('',explode(' ', $str));
$_SESSION['yzm']=strtolower($yzms);

//验证码
imagettftext($img, 15, 5, 10, 30, $write, 'msyh.ttc', $str);

//干扰素
for ($i=0; $i < 10; $i++) {
    $x1= mt_rand(0,100);
    $y1= mt_rand(0,35);
    $x2= mt_rand(0,100);
    $y2= mt_rand(0,35);
    imageline($img, $x1, $y1, $x2, $y2, $greens);
}
header('content-type:image/png');
imagepng($img);

imagedestroy($img);

 ?>

接收PHP

<?php
session_start(); 

echo "<pre>";
print_r($_POST);
echo "</pre>";
$yzms=$_SESSION['yzm'];

if($_POST['yzm']==$yzms){
    echo "1";
}else{
    echo "<script>location.href='yzm.php';</script>";
}



 ?>

相关文章

网友评论

      本文标题:验证码

      本文链接:https://www.haomeiwen.com/subject/lxhriftx.html