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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php& n( x9 B$ Z" j% n2 h7 ~/ |
  1. <?php" r5 {9 a4 i! @9 Y: [, @
  2. session_start();
    ) X0 v8 h6 o4 k2 j+ g
  3. // 丢弃输出缓冲区的内容 **
    ( s  |  d. W5 b4 Z
  4. ob_clean();9 |3 U$ _8 s& d5 g0 E  o6 _
  5. header ('Content-Type: image/png');' ~1 a. |3 r* p/ I# z
  6. // 1 创建画布
    5 L* T1 d) Q9 w" w- a
  7. $image=imagecreatetruecolor(100, 30);: t! w- w% n" s" W6 r0 F, W6 q; T
  8. // 2 画一个矩形并填充背景颜色为白色
    + ^8 S% P+ B- p& X( _
  9. $color=imagecolorallocate($image, 255, 255, 255);
    " n! V1 U( C+ d3 ]. m
  10. imagefill($image, 20, 20, $color);% n/ V: m, e! r& a1 Q6 [
  11. // for($i=0;$i<4;$i++){
    ! E/ n8 w) m& u8 Y
  12.     // $font=6;1 ?9 B$ k1 O9 H* J' O# H8 W8 z
  13.     // $x=rand(5,10)+$i*100/4;+ |, E7 X+ y* [) H4 B4 Y0 z# h
  14.     // $y=rand(8, 15);
    ' p4 D1 u, T1 D  @+ t
  15.     // $string=rand(0, 9);. Z7 m) g/ z% q5 y7 Q0 p
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));' [; K$ h1 F! K; ?
  17.     // imagestring($image, $font, $x, $y, $string, $color);1 j  l: n3 \* C
  18. // }1 ~9 m: j6 ~4 R7 d5 W
  19. $code='';
    6 N* f' M, [  C8 F1 e  L
  20. for($i=0;$i<4;$i++){
    # \* k% E& d1 M9 k0 I
  21.     $fontSize=8;
    ) H5 e! r3 `% t
  22.     $x=rand(5,10)+$i*100/4;5 h! J6 N; D2 z
  23.     $y=rand(5, 15);) K8 f9 Z$ }8 C' D7 |
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    0 _3 d( G3 `: P
  25.     $string=substr($data,rand(0, strlen($data)),1);
    & J# h; R1 {2 N/ [! p" ~* x
  26.     $code.=$string;+ v( n$ E) Y3 ?) k& t- s
  27.         // 3.2 加载自定义字体
    3 H% C' A' }" t( J5 A3 q& N0 T
  28.         $font_file = '1.ttf';
    8 k  N8 _* i. C# |
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));6 ]9 z" }8 }8 u5 m3 y
  30.         // 把文字填充到画布
    " o6 s9 j  W5 Z8 R
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);% L6 l3 Z9 u2 Q, t
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    ! x5 O' w( o; X6 |
  33. }
    $ L0 b# S2 j, r$ X. R0 L6 ~) Q
  34. $_SESSION['code']=$code;//存储在session里5 y4 L4 q3 Z& u9 t0 r% Y
  35. for($i=0;$i<200;$i++){8 Y0 H1 z3 s% R3 C2 ~6 i
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    4 ?9 e+ ]0 I' d- K. F7 z, l
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    & R9 [5 x6 J7 q: k3 X
  38. }* Z$ a8 r' c: O9 X# d
  39. for($i=0;$i<2;$i++){% w$ {7 F- N8 ]) w- E
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));2 H- K  u0 Y; n9 O( `* Q/ V9 U
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);5 M, P3 q5 @, |( Q( x! u
  42. }/ R4 j( y7 i7 k: w
  43. imagepng($image);' C. B9 W8 e) u8 ^+ ^8 k$ h! M
  44. imagedestroy($image);
    8 I3 y/ M" B) p; d8 s( r/ X; _+ N
  45. ) N* b$ G. b) ?3 Q4 s& S8 |) @* ]
  46. ?>
复制代码
6 c0 A7 x  F' E1 r7 _5 E+ n" d
form.php  d- d) A; W# Z) M( G. y
  1. <?php
    % p3 F  |- ?2 s2 u& y" C
  2.     if(isset($_REQUEST['code'])){- c8 a4 a; v$ O9 K3 R$ X( g
  3.          session_start();; f0 _& f# z# ]( Q
  4.         if($_REQUEST['code']==$_SESSION['code']){; ^/ K- k, l% I9 }( ^( H
  5.             echo "<font color='red'>输入正确</font>";, `0 P6 R) e( z0 d
  6.         }else{/ N9 D; A. j4 D( I" L8 Q# U
  7.             echo "<font color='red'>输入错误</font>";/ F; B- t  J# z2 \5 t% h, v
  8.         }
    ) d- H( U8 E6 t& r& k5 x$ F* n. B
  9.     }
    8 C6 l; ]5 a0 B, O' L8 l2 r! Z1 T
  10. ?>
    1 S9 n/ N- _- |- F; m
  11. <!DOCTYPE html>
    0 X+ b$ O; N. g8 V
  12. <html>
    1 c2 |+ z0 ^" X2 G+ \
  13.     <head>
    : C- @% C7 Y+ O' [6 y3 k
  14.         <meta chartset="UTF-8" />
    + ^; l3 S' N1 a/ C! x/ H! |
  15.         <style type="text/css" rel="stylesheet">
    , ^& L  L' O7 U1 y8 i- q* K
  16.             a{0 `5 N3 |' p, ]7 ?  A; U
  17.                 text-decoration:none;
    ! W' R0 v8 w- C1 @
  18.                 font-size:30px;
    0 N2 T$ t" }. Z1 f3 Z7 O
  19.                 color:blue;/ t. H6 e6 ]$ g; ?4 u$ G* }. H' I
  20.             }
    * A% d8 l% ~9 a/ B2 z0 R
  21.             a:hover{. M' u" c$ b) Q! E" d9 u9 h1 v
  22.                 text-decoration:underline;
    + y4 d' Z7 w. C4 D! J
  23.             }' o" [: L& P+ f! [# u+ n6 O
  24.         </style>. `& M7 G7 `& t3 W( N
  25.     </head>$ u1 Z1 Q! g( n' o8 A7 U
  26.     <body>" h3 _( x7 _8 S' `* P( I; H% w! S
  27.         <form action="form.php" method="get"># E; v9 _9 m$ _& A$ B, ]# R. u
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>0 _& r4 l/ q1 V5 S; O6 U" j
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    8 x6 j: n4 m) F; W: Q
  30.             请输入验证码: <input name="code" /><br />+ e8 h& A0 f. b- [8 T( x
  31.             <input type="submit" value="提交" />: J5 p$ z$ F) d* S
  32.         </form>- p. s" G# N1 F7 ^; Y
  33.     </body>$ r8 W; z0 r' U
  34.     <script type="text/javascript">
    2 f, U  V% Y* v: c7 j4 p3 V. O
  35.         
    # |! W! f5 P% u( v# C% u$ w
  36.         function changeCode(){% Q( P3 d4 h1 u' n
  37.             var img=document.getElementById('img');6 q  Q1 H1 {2 U) l% e( t6 p/ D
  38.             //img.src='index.php?r='+Math.random();) r5 m" o+ @+ j1 j9 }: g" s
  39.             img.setAttribute('src','index.php?r='+Math.random());
    ' f. t+ g( G# {2 c4 h0 t) X* p
  40.         }
    9 w9 k# \3 }( k
  41.     </script>8 q& b$ f, g9 O9 J& k; b
  42. </html>
复制代码

! f8 }8 o! _. D2 C8 c
. V1 ^! H/ o" k8 B* @
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 13:55 , Processed in 0.070285 second(s), 19 queries .

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