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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
index.php
# T) X4 ]. E5 e: d
  1. <?php2 I$ c8 c4 b, a4 r' W- {2 [
  2. session_start();: r+ y& M7 h; v6 a* G# u
  3. // 丢弃输出缓冲区的内容 **
    1 L- @: u- {/ M6 @" d# E; M" H7 Q
  4. ob_clean();
    : G7 d2 M2 r9 X6 p; o1 @4 O
  5. header ('Content-Type: image/png');
    1 z. M* O9 \) f! O4 Q& B. G
  6. // 1 创建画布
    $ g+ p- g4 P8 _0 t
  7. $image=imagecreatetruecolor(100, 30);8 V' w2 ^% F8 f+ d$ e$ N4 l
  8. // 2 画一个矩形并填充背景颜色为白色
    , t( I+ ~  v9 L8 ]- C( U; f- x* q- t
  9. $color=imagecolorallocate($image, 255, 255, 255);
    & H2 y4 L' M# I8 \& o' k" Y
  10. imagefill($image, 20, 20, $color);3 W4 v0 y# o+ s0 H4 C: o
  11. // for($i=0;$i<4;$i++){4 e2 g9 c9 Z9 ^! v- _1 f4 F
  12.     // $font=6;
    / f" N; B$ c! c9 }; r8 i8 `5 S
  13.     // $x=rand(5,10)+$i*100/4;9 O/ ]5 p1 z0 \- a& n9 S& c
  14.     // $y=rand(8, 15);7 }' s0 F9 x0 n6 X6 r8 q- x
  15.     // $string=rand(0, 9);8 e- o7 S! T; p* @) A" E
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));1 S3 G. M) T, s4 ], X) k
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    5 C. ]7 A0 |  x; y6 N
  18. // }- k/ _' S7 v; _& J) o' F4 x* r  \
  19. $code='';
    , E. M3 `, ~. ^9 n
  20. for($i=0;$i<4;$i++){: o& V. j  Z( Y% N; l
  21.     $fontSize=8;
    " m% r, r9 d$ Y& l
  22.     $x=rand(5,10)+$i*100/4;
    " Y* Q8 c& n4 B7 p3 p9 ~
  23.     $y=rand(5, 15);/ X* ]/ J! w' \+ |( A7 B
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';, l4 C- v+ n! d6 r7 W7 N6 |0 N
  25.     $string=substr($data,rand(0, strlen($data)),1);
    & k8 |) U+ J9 F5 _
  26.     $code.=$string;
    4 @2 }$ j: a! m3 ^  e5 h: w
  27.         // 3.2 加载自定义字体
    ( n6 V0 a2 [. ?$ n3 r# T- p
  28.         $font_file = '1.ttf';
    2 x! T1 C+ ~; A  |5 k, M
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));1 M5 r' g- t1 N$ F- l8 ?5 o# e
  30.         // 把文字填充到画布6 U! T& \  _+ i' C' M3 d
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    # u6 q! Q- C6 Q6 h3 E2 f
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    ) C+ F, K, K( L
  33. }
    $ b: i2 T7 x  N7 C- N* @5 a9 _
  34. $_SESSION['code']=$code;//存储在session里
    * `& ~2 K' p/ }5 \
  35. for($i=0;$i<200;$i++){
    + B" Z! e5 e# _
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));8 e# j. U# L' x# a. Y0 p
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);9 I" |5 V5 p% b" \( z
  38. }# o9 z; t% t3 q) U
  39. for($i=0;$i<2;$i++){
    & k9 t/ E. E$ ?7 t+ _6 ~0 Y! @0 [
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));6 S! [* ?3 |& H) d
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);9 R3 Z+ [' B5 R
  42. }' c9 Z7 q3 u2 n  b' q( M6 G) a# ]
  43. imagepng($image);
    ) I$ s2 g* m/ s! S" d9 Q
  44. imagedestroy($image);( l+ P# l2 m4 o2 [# L
  45. 4 e- K$ S; ?7 a+ [
  46. ?>
复制代码
/ v/ h& \, D( x. a7 c& R# |
form.php
; y+ k8 x1 l! m; a
  1. <?php
    6 B1 B, b/ H4 T: N; B# s* }
  2.     if(isset($_REQUEST['code'])){
    3 p  w* D3 E* m+ }
  3.          session_start();3 d6 ^  |# D3 |* C- R
  4.         if($_REQUEST['code']==$_SESSION['code']){& f- S  C$ m) O- Z# d5 \# J
  5.             echo "<font color='red'>输入正确</font>";
      u" g# f" ?6 m, q; g
  6.         }else{
    / f& l) i: G* l. C+ i8 s
  7.             echo "<font color='red'>输入错误</font>";' v1 [( D! }3 k
  8.         }* b; E$ z8 b; Q1 a
  9.     }4 g; g- B& |8 S/ X% C
  10. ?>+ b$ Z- a" T& o( |( v6 [. H. c
  11. <!DOCTYPE html>
    6 Y/ A' b8 \+ }4 p
  12. <html>
    # J& g  G) k  R- ]
  13.     <head>
    ) l# [/ J( M6 L4 ?/ C
  14.         <meta chartset="UTF-8" />
    " ^( B& D1 s" T" A  Y) o4 Z4 x0 w
  15.         <style type="text/css" rel="stylesheet">/ ]2 P3 [0 _& R" E- D) H
  16.             a{5 |; [* E9 I( w6 Y1 Y) x8 o, u, U& g
  17.                 text-decoration:none;  e% P' H# e9 m. e  ]" s
  18.                 font-size:30px;/ t4 Z: l& i! n9 k0 X
  19.                 color:blue;
    $ q. ~* B" l0 j; W
  20.             }. O8 [9 g$ F& I" m
  21.             a:hover{
    4 [9 v* Z1 n, L) |1 a. U
  22.                 text-decoration:underline;
    " y! C5 v' C" S4 O* i9 o' ]- v
  23.             }- ~# ?: F% g; b; i/ X7 `
  24.         </style>7 ~5 T4 v! F5 e1 M5 \
  25.     </head>! R# s! j! F2 I; @8 S* {$ ~
  26.     <body>
    + M8 j, }# j- ?7 W
  27.         <form action="form.php" method="get">
    ' J0 e% t) l9 R% c( o
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    4 h4 W, `$ P" s/ P
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    / X9 b' T( \+ Y
  30.             请输入验证码: <input name="code" /><br />
    2 {; `; d9 |* D& a$ Z1 Q" U" X- l  b
  31.             <input type="submit" value="提交" />
      l" L% A, V* m1 y: q8 P
  32.         </form>' {$ I4 p$ W: C: e, G0 ]
  33.     </body>
    8 L$ w3 Q' ~& d& n4 h! I3 Q
  34.     <script type="text/javascript">
    ; q+ v$ I& o5 u$ O4 G
  35.         
    , L' P" K% Q& \% V) r# w
  36.         function changeCode(){
    1 ^6 ^$ W3 _2 c1 {
  37.             var img=document.getElementById('img');
    8 C% E3 H0 v' E: j5 Q
  38.             //img.src='index.php?r='+Math.random();
    3 E9 q3 R. M$ M1 _( m" I$ m. U
  39.             img.setAttribute('src','index.php?r='+Math.random());
    : D( @5 F1 ]6 m9 f. D6 y
  40.         }8 l  X, D' _/ s0 d% {
  41.     </script>, U6 u1 ]# i1 [' W0 N" ^, h8 v  L
  42. </html>
复制代码
0 [' o4 N. `! m' n; n5 d7 b

0 A' S" f. c8 p" d; h
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 18:51 , Processed in 0.053530 second(s), 20 queries .

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