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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
2 c9 Z- j9 r& Z1 C; m( h: [1 X& y
  1. <?php$ Y  g6 I( M% w8 J8 _. |# G
  2. session_start();9 c2 Q# C% O3 }% O/ e2 ?7 {4 S
  3. // 丢弃输出缓冲区的内容 **
    * O9 I6 z+ [/ t: C
  4. ob_clean();
    " ^' f2 ~8 `  |8 I4 ]% K8 n( }; C
  5. header ('Content-Type: image/png');
    8 g) @  `, ]# w0 f" m, D. D
  6. // 1 创建画布
    : H! E, i7 ?6 d: j9 B" ^2 f3 f
  7. $image=imagecreatetruecolor(100, 30);' X5 |, O; B& B2 n" _$ t+ g
  8. // 2 画一个矩形并填充背景颜色为白色
    " H, O6 _7 x9 f! C- Q. I
  9. $color=imagecolorallocate($image, 255, 255, 255);
    ' Z5 f& y6 \$ L1 P
  10. imagefill($image, 20, 20, $color);6 d' a8 C% B% [8 E  B' G
  11. // for($i=0;$i<4;$i++){
    3 {' G8 k7 P8 v. q1 `$ B) A
  12.     // $font=6;: L1 s& o0 W7 n+ h* o4 s5 n
  13.     // $x=rand(5,10)+$i*100/4;; v/ v' ?7 m( E
  14.     // $y=rand(8, 15);0 [0 y% A& S5 F0 u/ g& D
  15.     // $string=rand(0, 9);
    % y5 l- y3 Q  T7 |8 h. h
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));% M9 g6 P: m- W, y
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    5 n( r6 Q3 R4 {% g- Z) l
  18. // }, S7 V  Q0 H" S# ~  l3 _! `, O
  19. $code='';  g7 `9 I8 Y; @! D+ M) d
  20. for($i=0;$i<4;$i++){  U5 }% G- F  E! @$ Z# |; T
  21.     $fontSize=8;
    2 p# w" J& \5 J* h. M
  22.     $x=rand(5,10)+$i*100/4;& v& c, t& H* V
  23.     $y=rand(5, 15);
    & n+ S4 Q* f: Q: Q
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';1 X* ^! ~; n* W( `1 i# ?
  25.     $string=substr($data,rand(0, strlen($data)),1);; X# G5 V) k8 Y5 o4 n# N$ _/ h& }: T
  26.     $code.=$string;
    4 [( T( G+ P# D
  27.         // 3.2 加载自定义字体
    1 h6 B$ X+ B6 ?0 o
  28.         $font_file = '1.ttf';2 Z+ r0 y% ^/ j* a" M
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));# }8 y; [' Y& P& `5 a5 p8 }
  30.         // 把文字填充到画布- u% P) I: P, Y% k9 u, K8 s- A
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    ( {' N$ D  q9 D" L3 p" ?9 e; j& ]
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    , I2 }& p2 u! y( w# t: O
  33. }
    2 R; n. A  I2 @
  34. $_SESSION['code']=$code;//存储在session里
    # A9 o" }1 k9 Q$ b5 H/ ?& R+ s+ l$ I
  35. for($i=0;$i<200;$i++){  A3 w8 x  V8 M3 f3 ?0 v7 r5 ^
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    8 M5 g8 q8 q3 O$ B* E
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);8 A- D3 q1 k) H! t6 D. a
  38. }3 B$ q# @& z- H3 \" n0 I9 W
  39. for($i=0;$i<2;$i++){
    5 c1 v% X8 w7 y6 B1 A
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));+ J* B6 p* o/ T4 {4 G& B! e
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    % H) p" Z: A% [2 s- J; q7 S
  42. }
    . n$ U8 k3 Q+ L" F: k+ E; c
  43. imagepng($image);* T7 j. w, A/ ^% [. q) L2 H
  44. imagedestroy($image);- Q# q2 z3 Z, i8 L) Z& ]

  45. 2 }% w- _1 K- l( h6 Z1 R
  46. ?>
复制代码
4 ]- i. w& _' |. ?- X
form.php! ?: G2 V8 s, k2 j$ x
  1. <?php
    # [2 F7 |7 @  B0 M! S
  2.     if(isset($_REQUEST['code'])){. J5 I% m+ E; ?3 B
  3.          session_start();$ `. J$ m# C, g
  4.         if($_REQUEST['code']==$_SESSION['code']){. b, F6 ?  U) `) o5 J9 w
  5.             echo "<font color='red'>输入正确</font>";0 O$ f  Z5 _) D8 L
  6.         }else{! U; B/ z2 f' d* S
  7.             echo "<font color='red'>输入错误</font>";+ h" @! F$ G* b7 ~3 a: a5 j
  8.         }" L. p( o8 I' u. m5 M
  9.     }
    6 g2 c9 L1 v4 G& [2 `  r' c. e+ X
  10. ?>
    " Z' x; k0 C' U- R" ?
  11. <!DOCTYPE html>
    ! O9 Z7 G% G3 v0 r
  12. <html>
    0 i7 k' ?2 P  a8 c4 e- _% |
  13.     <head>
    2 g( v% v' b9 t2 P  N* ]+ A1 ~" V
  14.         <meta chartset="UTF-8" />
    " z9 c3 X1 ~" T! L# O8 ~2 h
  15.         <style type="text/css" rel="stylesheet">/ M/ Q, F+ k. F6 c5 M; H% o1 @
  16.             a{5 q! C! [0 J2 h
  17.                 text-decoration:none;
    5 L) y" z6 l$ @9 Y8 i) w
  18.                 font-size:30px;: z3 f& @6 i! P) t  H& Y
  19.                 color:blue;: k, k( A9 u/ |' ]" L- F- m
  20.             }
    6 j* h- ~' n5 \' D
  21.             a:hover{/ I' H3 [9 j5 {
  22.                 text-decoration:underline;4 X; O& R( d& U4 y
  23.             }
    6 o  U% ~( O2 N# }$ ~
  24.         </style>& w: A# i3 t( k; R* i8 y" T
  25.     </head>  d8 u+ ?' J4 J. H8 ^$ w- e; c
  26.     <body>
    / h( n7 v! l6 @% d
  27.         <form action="form.php" method="get">
    $ \+ z7 o8 g1 R2 |
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>5 j0 ?6 i, n2 y
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    . m. W. a5 L) V* y1 p
  30.             请输入验证码: <input name="code" /><br />
    8 h2 w  N5 y4 g0 v4 x
  31.             <input type="submit" value="提交" />
    2 \! X6 c' _. y; T( c  k) Q
  32.         </form>
    / n" }$ @. O3 |! G
  33.     </body>
    5 s6 _# W# ?7 q& Q. k8 f
  34.     <script type="text/javascript">' `, w. f! L. H' I
  35.         4 Y" O! q$ |: \2 x9 `! X/ G, Y3 C
  36.         function changeCode(){
    ( b; S8 J1 c) Q* ~
  37.             var img=document.getElementById('img');& ]( F0 q2 e+ f# ?6 s2 v8 d$ k1 D
  38.             //img.src='index.php?r='+Math.random();
    $ V8 Y( S8 @" F- d
  39.             img.setAttribute('src','index.php?r='+Math.random());3 G7 z, z( a" a) O+ l& Y/ J. z, X* i: ^! G
  40.         }3 Q( [5 T5 m5 C" s6 k$ ~
  41.     </script>* K( G( r/ z% T: Y; I# ]
  42. </html>
复制代码
1 D7 X& F+ K, ^0 C* T
: ~* [" O! G4 C) l7 o# B
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-4-30 19:57 , Processed in 0.079116 second(s), 19 queries .

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