您尚未登录,请登录后浏览更多内容! 登录 | 立即注册

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 15660|回复: 0
打印 上一主题 下一主题

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php  l+ N( Y- q/ [2 n
  1. <?php
    5 z+ E8 s( ~! R4 _1 I! y. k) A
  2. session_start();
    , c1 s! ?4 q& }2 O
  3. // 丢弃输出缓冲区的内容 **
    / H) q9 m% z+ V/ e7 h3 Y+ a# T
  4. ob_clean();
    2 ]7 I0 X' `: I  {6 t+ v9 _
  5. header ('Content-Type: image/png');
    / z# ^( B/ P8 h  c$ p
  6. // 1 创建画布
    4 O6 E' c4 `1 x; X7 d1 {" M: |! C
  7. $image=imagecreatetruecolor(100, 30);
    5 k4 W) P) i. ~4 |2 y8 \% b
  8. // 2 画一个矩形并填充背景颜色为白色
    : {, Z- m3 a0 l. L
  9. $color=imagecolorallocate($image, 255, 255, 255);6 K* D  P  ]( }5 v3 U: c. n- R
  10. imagefill($image, 20, 20, $color);! w8 m) X6 l* {. z0 ^1 x
  11. // for($i=0;$i<4;$i++){
    ! w1 G, ]& T( ?1 k8 d1 b% z
  12.     // $font=6;
    2 {/ ?' C- E) e1 |' J' E
  13.     // $x=rand(5,10)+$i*100/4;: E5 L# Y6 D1 T4 e* g6 x
  14.     // $y=rand(8, 15);
      e1 e; ?$ a. x3 b$ s) s9 X
  15.     // $string=rand(0, 9);/ s5 o# l! H8 w* f
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    3 D6 I' c5 S! Z4 C4 q$ F, m  m
  17.     // imagestring($image, $font, $x, $y, $string, $color);5 ~3 v5 \/ b5 S: q1 D' [
  18. // }
    ; t% d, E, _" l' [, W% s
  19. $code='';8 K/ i! w5 T/ O# X, F
  20. for($i=0;$i<4;$i++){0 C) e' [- w- {  ?" B+ d2 }4 U
  21.     $fontSize=8;0 X4 |8 c: v8 x" \' I
  22.     $x=rand(5,10)+$i*100/4;
    & A' J% U8 X2 P" ~8 U7 h2 O
  23.     $y=rand(5, 15);
    * [  B: E: S* }7 s
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';9 v8 t6 H" S3 ]! |' x) J- c6 E1 k
  25.     $string=substr($data,rand(0, strlen($data)),1);1 V8 i" S1 n! ]% k8 }
  26.     $code.=$string;
    * {+ n! G" H! f+ A9 m: D. c
  27.         // 3.2 加载自定义字体/ H3 F& J( ?7 _
  28.         $font_file = '1.ttf';; K; ]0 R9 H6 F. l' g# \
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));) H) a+ y$ H  g" |. y6 h3 Z
  30.         // 把文字填充到画布
    ; F% H) Z( N9 g& u7 s( n7 C) ]
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);. f% I2 P  W' |
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    % J8 w: @  b3 V" K5 m8 q# p! y9 u
  33. }
    $ w. C8 C0 x% E. q  R' m
  34. $_SESSION['code']=$code;//存储在session里
    9 X. M; m$ c/ ?
  35. for($i=0;$i<200;$i++){
    $ O; s5 e+ V1 e5 {
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
      E; [. P$ [' h# j, ^7 x
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    0 d+ {1 B4 {* ^% k9 J0 D- u
  38. }
    9 S  m" Z2 x3 f  s1 y. F! @# d
  39. for($i=0;$i<2;$i++){* m3 q  L5 g; ]9 j- S# N" C. n* S
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    ( U% L1 X$ _! c. b$ X  Q& M+ C
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    / ~3 i! R- W9 w, @% z" g: l7 R
  42. }
    # K" T: n- O$ V( @) j
  43. imagepng($image);$ t0 ~8 n/ `3 Y4 F& p7 E6 j1 M
  44. imagedestroy($image);
    4 t8 j( X  T) O# d7 {, k- |

  45. 5 C! U/ v& U9 S
  46. ?>
复制代码

/ X( i/ S7 a7 Aform.php
+ d& r  S) }- t) v6 ^
  1. <?php
    : h! S3 b7 H* ~* g' u4 x* G
  2.     if(isset($_REQUEST['code'])){+ v, p  c% M1 P
  3.          session_start();0 ?; ?+ i4 M: |7 v2 l4 [& h
  4.         if($_REQUEST['code']==$_SESSION['code']){
    $ |. B; M- ~$ q/ u
  5.             echo "<font color='red'>输入正确</font>";) N4 l- Z7 B( B) @6 M
  6.         }else{
    9 C/ B7 l4 i& i3 N" c
  7.             echo "<font color='red'>输入错误</font>";9 o7 A" B- z, b# S; m+ Z9 f
  8.         }. Y; R, n2 S1 V4 m
  9.     }! [" `. F) m! I
  10. ?>
    9 l: `1 v+ e: g5 e
  11. <!DOCTYPE html>
      [5 P) E3 T& T# g# W( F7 P% T
  12. <html>" b: U+ V% s. `5 g6 T5 H
  13.     <head>6 @2 u/ e" d# W
  14.         <meta chartset="UTF-8" />
    % v9 X- J' R' U/ W9 D
  15.         <style type="text/css" rel="stylesheet">4 Q; {, D2 K$ B" |
  16.             a{
    " O1 p+ Z* c# E# H9 A8 z# {
  17.                 text-decoration:none;
    $ W6 w! u/ }9 J& a* ^8 D
  18.                 font-size:30px;
    5 J% R& @, A" z$ b) |3 h6 ]% m
  19.                 color:blue;% O% d5 [- F( @+ C7 q
  20.             }7 `5 X* F/ R$ H$ e+ }
  21.             a:hover{" b9 m% ~4 F# U& a
  22.                 text-decoration:underline;
    4 Z; l4 W! _+ Q" X
  23.             }1 G; L! \6 Z& z# g, [8 g
  24.         </style>: C, v0 R2 |3 S8 J* _
  25.     </head>4 s9 w8 J* n0 O6 C8 f
  26.     <body>
    8 Y& i8 }3 I7 U
  27.         <form action="form.php" method="get">
    ( N, }8 O% z7 E9 W( v/ Y; r9 g$ U
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    3 ]5 c' S; {, e$ M. m. E1 T; [4 ~3 P
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />8 z. `" T- j: |9 C" N
  30.             请输入验证码: <input name="code" /><br />
    + J  _  b8 [% r
  31.             <input type="submit" value="提交" />  D3 _5 G4 v$ D  ?& M% v
  32.         </form>5 h& X% @2 f7 `4 G
  33.     </body>; S% e! V' }; D7 K( r3 ^
  34.     <script type="text/javascript">
    # F" A" V4 V" E2 Y# W8 t% D' P
  35.         
    4 m7 Q' v) ?/ x
  36.         function changeCode(){
    $ n, N: _' Q0 }# r* L2 z$ s0 o
  37.             var img=document.getElementById('img');
    7 e9 f- w9 `! M* f/ |
  38.             //img.src='index.php?r='+Math.random();1 Z$ o1 e% }# C
  39.             img.setAttribute('src','index.php?r='+Math.random());
    ' r9 I+ E! {7 K$ j
  40.         }
    1 |3 p1 D! u& R3 z+ r% ]& o
  41.     </script>
    5 {( p! L) k2 c2 U1 I- l2 I
  42. </html>
复制代码

* i* S) t: R4 z0 ^0 t8 T( J7 P4 [, p9 {/ Q' O" c$ g
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-17 22:04 , Processed in 0.056756 second(s), 20 queries .

Copyright © 2001-2026 Powered by cncml! X3.2. Theme By cncml!