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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php" t0 Y" c+ k) D. k' E* J
  1. <?php
    ! N1 h0 |6 |5 F) L% u- i
  2. session_start();# `+ S3 R9 H. [, a, O
  3. // 丢弃输出缓冲区的内容 **% z# J3 o( {5 R% q6 r8 ?9 n! H5 `5 {
  4. ob_clean();* v1 H8 X, A9 Q/ ?" s
  5. header ('Content-Type: image/png');6 |3 \8 G- |5 W" A
  6. // 1 创建画布0 Q+ |) s, e) O9 a  X% h) g, D
  7. $image=imagecreatetruecolor(100, 30);, @: [) U4 {! f2 s
  8. // 2 画一个矩形并填充背景颜色为白色
    % v# p: ~+ x4 Q% M1 w5 h
  9. $color=imagecolorallocate($image, 255, 255, 255);
    ; B" q3 L+ u2 ]: A4 K2 f
  10. imagefill($image, 20, 20, $color);
    . q9 `. G- V4 D2 \
  11. // for($i=0;$i<4;$i++){
    3 G5 {% f9 }. Y% l$ v3 j- |9 E, E
  12.     // $font=6;
    8 ?, P! L6 ~9 j/ F4 T: X, T7 O
  13.     // $x=rand(5,10)+$i*100/4;
    , [- v, k8 b4 p) f  V; Y
  14.     // $y=rand(8, 15);
    9 s5 E, t9 f1 ~- j& Y; }
  15.     // $string=rand(0, 9);
    ( h! b$ `4 W6 Q+ h( S) X
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));. n7 u+ W; {- M( ?7 _
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    9 i" ^2 Q9 o8 h) {$ K& c6 P
  18. // }. W8 P3 i' t1 I1 C! U& d
  19. $code='';3 A4 X( d& o. A% J4 z
  20. for($i=0;$i<4;$i++){0 P! H' P1 m- V+ u! l3 N5 W
  21.     $fontSize=8;
    ' A: {( `3 A! w: Y' I/ ^
  22.     $x=rand(5,10)+$i*100/4;
    % n! I# t% R, Q9 B5 B
  23.     $y=rand(5, 15);
    5 f1 ^1 P0 c& ]$ \# S0 \7 @
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    ( q3 ]" K/ S# d  j" c
  25.     $string=substr($data,rand(0, strlen($data)),1);6 G7 ]! S8 r3 \6 L- K! Y
  26.     $code.=$string;- I% n7 L. o( x* Z
  27.         // 3.2 加载自定义字体9 N& ?& p1 l4 N$ i. c, b
  28.         $font_file = '1.ttf';
    % ]/ f1 [3 g  W" o, `# A+ `0 ?6 m
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));$ t  T6 |7 ~  ^
  30.         // 把文字填充到画布8 A+ N5 s; W; ]% o
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);9 i( U1 z$ C& y
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    , c) B' J  ~! b- o7 i
  33. }# K7 h/ v' M/ |
  34. $_SESSION['code']=$code;//存储在session里5 L7 u1 H: W! T3 j+ C/ t9 Z+ m1 [. l1 l
  35. for($i=0;$i<200;$i++){
    9 O# t; M( s+ ?$ t% z4 U
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));9 T% e: R6 m/ g! m0 M! I
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);& v5 X$ T+ P; a8 \: [
  38. }' K$ t! [; K8 `. a8 y4 x  H& O
  39. for($i=0;$i<2;$i++){
    ( Z  o( f7 u7 C. N1 X5 S
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));0 \: K7 ~  S" Z( B' I: b
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    . Z4 E6 e3 E3 o0 w* `5 t/ a
  42. }3 O, y8 z' K1 |; y  i( ]* R
  43. imagepng($image);
    - L8 j$ O3 n' }' R1 a
  44. imagedestroy($image);
    0 ]( }8 U/ I7 q! |0 W0 O- }" X
  45. ! j  \5 y+ c% z+ ?1 t, z. \
  46. ?>
复制代码

. a# d3 q) h, z1 Wform.php# G( \6 v1 |  ]! v- a) y! C" y, @
  1. <?php
    3 T3 f1 r* _% n* O% `- u, y9 H$ h+ n
  2.     if(isset($_REQUEST['code'])){
    # A5 F$ c# R0 y% U: L
  3.          session_start();
    " A6 {3 U4 P% I8 V( u( h
  4.         if($_REQUEST['code']==$_SESSION['code']){- ~  a  Q4 e2 B! s2 R+ T, n
  5.             echo "<font color='red'>输入正确</font>";
    : P. `. Z! w$ i8 d) b6 x3 t) g
  6.         }else{- T  s/ r6 L+ m) {$ n
  7.             echo "<font color='red'>输入错误</font>";- X# O1 ]6 n$ {
  8.         }
    ) X3 @/ r; l6 ]1 a, c" L
  9.     }
    6 Y/ l0 p2 E4 a0 H4 [
  10. ?>3 R; @' V1 D, O+ I" M( p, y
  11. <!DOCTYPE html>* m6 Z; X& G9 }. H$ i8 d
  12. <html>
    " {+ z1 M2 C9 {8 S3 |- U' y8 F
  13.     <head># J# y: ]  A. |" W. R! E2 i- ~
  14.         <meta chartset="UTF-8" />
    0 e1 n1 s) l# Q. l
  15.         <style type="text/css" rel="stylesheet">" Z7 H$ F9 s. D6 n8 r  l' t
  16.             a{! G! ?4 X9 B: z) ]' P
  17.                 text-decoration:none;6 p& A5 V% v( n+ q, V4 e0 \
  18.                 font-size:30px;
    ; g* V0 }4 X: W! y
  19.                 color:blue;4 Y6 e( S+ t/ Q; V
  20.             }
    , G: [( p2 x' `: P
  21.             a:hover{
    - S4 `' s. M& j$ U* x# f7 g
  22.                 text-decoration:underline;" K5 f7 Q* o+ [
  23.             }% J- g% }4 o7 d' q9 {" |* A
  24.         </style>/ V! n- y) D4 l9 J
  25.     </head>9 M  |( E- s* h" F# S
  26.     <body>
      d" h% t. j. }
  27.         <form action="form.php" method="get">
    - |& D+ `, S6 }0 L% E* B/ X
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>. ~+ E1 [4 D: N% u, p! A
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />& o0 i- c) D2 s) p
  30.             请输入验证码: <input name="code" /><br />) `, q+ [: H# U& I
  31.             <input type="submit" value="提交" />
    1 ~/ k3 ^/ V6 g
  32.         </form>
    8 e: r+ g2 q9 i) i+ w! K3 K1 n
  33.     </body>
    & k1 p" `7 V! \+ \3 Z1 e; T4 `, z
  34.     <script type="text/javascript">
    / k& Z! d) N/ e: p% [) G0 B
  35.         
    3 H; d/ R& n- h6 S# n
  36.         function changeCode(){
    3 V5 G. w6 F% e1 |: ^
  37.             var img=document.getElementById('img');
    ' J/ [5 C3 d2 G1 O
  38.             //img.src='index.php?r='+Math.random();
    & M! A% B* I$ I* n" K
  39.             img.setAttribute('src','index.php?r='+Math.random());; J8 E/ I  U/ A; ], v
  40.         }
    # A" J7 C' @# _# e  E3 Q  E. N
  41.     </script># W# p& Z' M+ K/ b2 Y/ b* f
  42. </html>
复制代码
# u# x: Y$ k+ C  `7 L

: S7 U1 E% C7 c- q0 S" _
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-6-19 22:43 , Processed in 0.056688 second(s), 20 queries .

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