• PHP (20140513)


    验证验证码的PHP代码:

    利用随机数随机生成四位数的验证码。

     1 <?php
     2 
     3 session_start();
     4 $arr = array(
     5     'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x',
     6     'y','z','0','1','2','3','4','5','6','7','8','9'
     7 );
     8 $rand = "";
     9 for($i=1;$i<=4; $i++){
    10     $rand .= $arr[rand(0,count($arr)-1)];
    11 }
    12 $_SESSION['check_pic'] = $rand;
    13 //生成图片
    14 $im = imagecreatetruecolor(100,30);
    15 //生成颜色,当第一次调用生成颜色的方法,是生成背景颜色
    16 $bg = imagecolorallocate($im,0,0,0);
    17 //第二次调用这个方法,是可以生成图片上面的文字或其他样式的颜色
    18 $te = imagecolorallocate($im,255,255,255);
    19 
    20 //在图片上面生成文字
    21 imagestring($im,rand(1,5),rand(3,70),rand(3,15),$rand,$te);
    22 //要把php当成图片输出,必须给文件一个头申明
    23 
    24 ob_clean();
    25 header("Content-type:image/jpeg");
    26 //最终生成图片
    27 imagejpeg($im);
    28 
    29 ?>

    验证验证码是否正确的代码:

     1 <html>
     2 <head>
     3     <meta http-equiv="Content-Type" 
     4 
     5 content="text/html;charset=UTF-8"/>
     6 </head>
     7 <body>
     8 
     9 <?php
    10 
    11 session_start();
    12 if(isset($_POST['check'])){
    13     if($_POST['check'] == $_SESSION['check_pic']){
    14         echo "验证成功";
    15     }else{
    16         echo "验证失败";
    17     }
    18 }
    19 ?>
    20 
    21 <form action="check2.php" method="post">
    22     <input type="text" name="check"/>
    23     <img src="check1.php" alt="" onclick="refreshImg()" 
    24 
    25 id="chk" style="cursor: pointer"/>
    26     <br/>
    27 
    28     <input type="submit" value="提交"/>
    29 </form>
    30 <script>
    31 function refreshImg(){
    32 //避免浏览器认为一直访问的同一个页面,所以用随机数传一个
    33 
    34 值,让它以为访问的是不同页面
    35     var rand = Math.round(Math.random()*10000);
    36     var chk = document.getElementById("chk");
    37     chk.src = "check1.php?num="+rand;
    38 }
    39 </script>
  • 相关阅读:
    Python--面向对象编程(2)
    Python--面向对象编程(1)
    Python--常用模块
    Python--函数
    Hadoop综合大作业
    hive基本操作与应用
    MapReduce作业
    熟悉HBase基本操作
    熟悉常用的HDFS操作
    爬虫大作业(对电影的爬取)
  • 原文地址:https://www.cnblogs.com/sunshine-c/p/3726797.html
Copyright © 2020-2023  润新知