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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php. }7 J6 N7 t, q& M
  1. <?php
      t1 T, B/ y- V- U
  2. session_start();3 E9 a# H% {# \3 N
  3. // 丢弃输出缓冲区的内容 **
    6 A; |# k; R0 B. F
  4. ob_clean();- X6 W8 n: c/ i3 z, h
  5. header ('Content-Type: image/png');6 ^+ i) J2 B" B' q0 ?$ f
  6. // 1 创建画布) ?& r2 T/ S% H3 q" w
  7. $image=imagecreatetruecolor(100, 30);" V! |; F' A8 f+ _. N8 d$ O4 [% o
  8. // 2 画一个矩形并填充背景颜色为白色
    $ S/ r  p+ ~. @1 ?; u  Q
  9. $color=imagecolorallocate($image, 255, 255, 255);
    3 A" Y# q9 \. E) N# Y5 i: P* T
  10. imagefill($image, 20, 20, $color);
    ; i  Y0 i  c0 _" V2 u4 D6 t
  11. // for($i=0;$i<4;$i++){
    # I+ v6 w, A+ E5 d: Q8 E" t$ [
  12.     // $font=6;
    / X* Z- `* T3 h8 x! S
  13.     // $x=rand(5,10)+$i*100/4;
    # j# S$ U* z" k1 P
  14.     // $y=rand(8, 15);: y- {2 k4 O3 Q) w% m' _/ i6 l
  15.     // $string=rand(0, 9);
    5 x  E) K: r- u$ I
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    ' d- Q% m0 b/ x2 @' H* [
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    9 j* ]; Z3 v8 @) M
  18. // }
    4 Z5 B+ M4 K$ N% l# n
  19. $code='';
    / N# a* Y1 R: G* \( S
  20. for($i=0;$i<4;$i++){9 G* l: s) O6 x/ b/ Q8 d/ A  B
  21.     $fontSize=8;
    3 ~: ]  k' n+ A3 d) W9 p- a  ~
  22.     $x=rand(5,10)+$i*100/4;
    8 \7 G8 H" _- R: L* ^  }
  23.     $y=rand(5, 15);
    ; D5 y! q: ^+ S3 {( e
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    4 |) ~) X9 B7 P$ g
  25.     $string=substr($data,rand(0, strlen($data)),1);
    6 H! S" j9 h2 j% M$ Z2 Z$ `/ E& b7 B7 I
  26.     $code.=$string;( `* K! G$ g9 X. v
  27.         // 3.2 加载自定义字体8 J& K, A" D+ [! A8 R  K" ?
  28.         $font_file = '1.ttf';! l* Q1 W+ m6 j3 G/ r8 w" T
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));1 @$ h' o$ I  }2 z# F1 l
  30.         // 把文字填充到画布+ W8 L" C( \" M" Y8 A0 p: G5 @
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);5 s* |& P! o2 t' V/ _0 d2 D% X6 ^
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    , D# z4 x' m3 c) ]
  33. }7 E5 G5 Y* h; R" y
  34. $_SESSION['code']=$code;//存储在session里
    7 C0 ]8 ]. F- d/ y
  35. for($i=0;$i<200;$i++){  V! F1 ]2 ^. l0 i' F; x
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    " F- k# v) @2 I" p) w
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);) s  |' ~2 D4 H4 b. a" l
  38. }! Z- _1 Z. H1 C8 D9 B0 T
  39. for($i=0;$i<2;$i++){) g( u, r$ K; _$ [3 @% j
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    7 C. C' \9 h; E6 H& z
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    ; P$ W% n3 v" I% t- U
  42. }
    ; p  o' H0 ?8 z
  43. imagepng($image);! n4 S- v- B8 s- X) ~
  44. imagedestroy($image);
    ; u9 c& z. ]" `9 A
  45. 2 z1 F, b( r# G6 `+ i8 p; S) Z1 G
  46. ?>
复制代码

9 [( Z- ]5 R& O+ m& h3 _. hform.php% }3 ~2 v5 R0 X- w& v
  1. <?php0 \0 L8 n6 o7 H" v
  2.     if(isset($_REQUEST['code'])){
    , a/ h" U6 u# n
  3.          session_start();0 w) q0 }" T# f
  4.         if($_REQUEST['code']==$_SESSION['code']){# y, v. T, C( f7 G! X' m0 b
  5.             echo "<font color='red'>输入正确</font>";
    2 h; z) \0 j) N
  6.         }else{* v/ u( T% G, m1 z5 G, E3 m5 m
  7.             echo "<font color='red'>输入错误</font>";8 `* y  x4 w8 i6 o; L
  8.         }: G# I2 t; n, R  I& n
  9.     }
    * l8 e4 ^* N" r% b
  10. ?>
    ( C7 X# l2 v( A* W2 M
  11. <!DOCTYPE html>
    : F+ P: h4 u) q7 b# O) P8 h
  12. <html>' ~: y0 `2 E, }" x3 X& R# T
  13.     <head>/ |' ^% d' R9 q5 ?8 b0 z; X
  14.         <meta chartset="UTF-8" />
    7 {( s, q) N" T, h9 V) e* K1 `* b
  15.         <style type="text/css" rel="stylesheet">
    / ^8 a1 e7 H$ P
  16.             a{
    & [! t6 k" C; L4 c7 d' D* c
  17.                 text-decoration:none;) T1 v, W, k$ [9 E# ]
  18.                 font-size:30px;" K6 J6 L# n/ {) ]4 s
  19.                 color:blue;1 w) a0 J1 o/ q" k. p
  20.             }
    7 U( y  @, j3 ^: A
  21.             a:hover{
    + X% @- ~/ c* V
  22.                 text-decoration:underline;' I$ O* K8 ]5 E. D* {/ P0 D
  23.             }$ a  O% N! {& L# Y; @
  24.         </style>0 t$ N% P3 w, h6 g6 J
  25.     </head>0 r) X. m# b% ]+ |( M
  26.     <body>
    ! A  ?: Q4 X% ~3 |$ q% u' I& l
  27.         <form action="form.php" method="get">7 N* e) c- Q) H* u' E
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    , V( n' `0 W! f: |
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />$ @; `- P( g% b+ I
  30.             请输入验证码: <input name="code" /><br />9 t% Y/ D8 }2 }! `3 g& O/ o
  31.             <input type="submit" value="提交" />
    ) w8 \, @' o$ b. T
  32.         </form>
    - H6 `8 O8 [. G
  33.     </body>
    # W  q- p" J- m5 o& h
  34.     <script type="text/javascript">  N9 R7 t" ?- p: V6 p
  35.         ! g3 P- }2 L+ H% y" G7 ~1 t2 i
  36.         function changeCode(){& n% \: x5 e& [: s1 ^) V; r
  37.             var img=document.getElementById('img');3 }4 p3 F0 L' c# B5 \& K, A
  38.             //img.src='index.php?r='+Math.random();
    " D' e4 S. f$ Y9 y' l* s
  39.             img.setAttribute('src','index.php?r='+Math.random());- X+ f7 x8 X6 ?( Z% s1 x; K
  40.         }
    4 u/ n: N- I8 z1 [! u
  41.     </script>+ [  f5 }6 S+ k* z8 h
  42. </html>
复制代码

1 s, e, {/ L" h' C
) C2 H, F: ?/ s% I0 t% p  Q" s3 m
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 14:31 , Processed in 0.077323 second(s), 19 queries .

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