您的位置:首页 > 编程语言 > PHP开发

登陆验证码

2016-09-29 22:30 155 查看
<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

 <title>验证码使用</title>

</head>

<body>

<h1>用户登录</h1>

<form action="index.php" method="post">

<input type="text" name="mycheck">

<img src="session5.php" onclick="this.src='session5.php?aa='+Math.random()"/>

<button type="submit">提交</button>

</form>

<?php

if(isset($_POST['mycheck']))

{

 $mycheck=$_POST['mycheck'];

 session_start();

 echo $mycheck;

 echo $_SESSION['mycheck'];

 if($mycheck==$_SESSION['mycheck'])

 {

    echo "验证成功";

 }

 else{

  echo "验证失败";

 }

}

?>

</body>

</html>

//session.php文件

<?php

 session_start();

 $check="";

 for ($i=0; $i < 4; $i++) {

  $check.=dechex(rand(1,15));

 }

 //将随机验证码保存在session中

 $_SESSION['mycheck']=$check;

 //创建图片

 $img=imagecreatetruecolor(110, 30);

 //背景默认为黑色

 $bgcolor=imagecolorallocate($img, 0, 0, 0);

 imagefill($img, 0, 0, $bgcolor);

 //创建新的颜色

 $white=imagecolorallocate($img, 255, 255, 255);

 $blue=imagecolorallocate($img, 0, 0, 255);

 $red=imagecolorallocate($img, 255, 0, 0);

 $green=imagecolorallocate($img, 255, 0, 0);

 //画出干扰线段

 for ($i=0; $i <10 ; $i++) {

  imageline($img,rand(0,110),rand(0,30),rand(0,110), rand(0,30),

   imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)));

 }

 //画噪点

 imagestring($img, rand(1,5), rand(2,80), rand(2,10), $check, $white);

 header("Content-Type:image/png");

 imagepng($img);

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php