PHP写验证码

生成图片页面:index.php

<?php
 session_start();
 
 for($i=0;$i<4;$i++)
 {
  
  @$rand.=dechex(rand(1,15));
  
 }
 $im=imagecreatetruecolor(100,30);
 $bg=imagecolorallocate($im,0,0,0);
 $te=imagecolorallocate($im,255,255,255);
 
 imagestring($im,rand(1,6),rand(3,70),rand(0,15),$rand,$te);
 
 
 for($i=0;$i<3;$i++)
 {
 $tes=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
 imageline($im,0,rand(0,15),100,rand(0,15),$tes);
 }
 
 
 for($i=0;$i<200;$i++)
 {
  imagesetpixel($im,rand()%100,rand()%30,$tes); 
 }
 //$str=iconv("gbk","UTF-8","新年快乐");
 //imagettftext($im,20,0,20,20,$te,'convalescence.ttf',$str);
 
 header("Content-type: image/jpeg");
 imagejpeg($im);
 
 $_SESSION[check_pic]=$rand;
?>
 

显示图片页面:sub.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?php
 session_start();
 if(isset($_POST['check']))
 {
  if($_POST['check']!=$_SESSION['check_pic'])
   {
    echo "验证码有误".$_SESSION['check_pic'];
   }else
   {
    echo "验证成功".$_SESSION['check_pic'];
    }
  }

?>
<form action="" method="post">
<img src="index.php"><br>
<input type="text" name="check">
<input type="submit" value="提交">
</form>
</body>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值