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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php# F" f/ Y, X/ T
  1. <?php
    + c$ u- Z* q0 s' o$ z+ Z; c: t
  2. session_start();
    . h* ]0 s6 y# d# Y+ W
  3. // 丢弃输出缓冲区的内容 **
    ( e' R( }& O) `( f- e- c1 C, C" N
  4. ob_clean();
    ) C* M  y# [9 S$ _( G0 y
  5. header ('Content-Type: image/png');) E. V1 H5 A& p  n& M; `
  6. // 1 创建画布
    ( Y# \" g( f% }% s" e. I
  7. $image=imagecreatetruecolor(100, 30);
    ( O' N2 j2 e" C2 Q# z
  8. // 2 画一个矩形并填充背景颜色为白色
    # `+ K. e! P& c# {
  9. $color=imagecolorallocate($image, 255, 255, 255);
    8 P2 u- }8 V9 e# Z; e5 U/ B- ~- j: ~
  10. imagefill($image, 20, 20, $color);
    , `+ P+ v! }0 C( m1 A* O
  11. // for($i=0;$i<4;$i++){
    ) M' a9 ]$ Q% c% z6 _. L8 w6 d
  12.     // $font=6;& ^& O; i$ f8 I7 F2 l- ~4 q& D
  13.     // $x=rand(5,10)+$i*100/4;
    3 t2 h, x; A/ S  x4 ?: A: T; B, b
  14.     // $y=rand(8, 15);" V/ ~0 u/ i! c1 s
  15.     // $string=rand(0, 9);% o; H  {) {9 n
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    - E; A. L8 M1 `, T) t+ D
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    5 ^" ?9 b- x8 n- _. b8 c
  18. // }
    ) X( q: Z; v' j
  19. $code='';
    2 e5 m; M" ~$ n5 X8 l
  20. for($i=0;$i<4;$i++){6 B: @+ Q6 v% C7 M+ D0 O# H# h
  21.     $fontSize=8;
    ) m* O8 p( Z( O* m5 R. ]' f
  22.     $x=rand(5,10)+$i*100/4;
    ) A9 Q2 ~: M  f2 f
  23.     $y=rand(5, 15);
    $ f- l% V2 E; O2 Y- Q' J+ q3 q
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    2 m8 R, ?( l; h/ e4 R) n3 q
  25.     $string=substr($data,rand(0, strlen($data)),1);; u) p, e$ g1 h. l! ]* P" ]
  26.     $code.=$string;
    6 L% m1 G2 E% t, [$ h" ^2 v8 y* Y
  27.         // 3.2 加载自定义字体" x7 ^% j8 b2 g' j3 L* R& k
  28.         $font_file = '1.ttf';
    1 S# p1 m# n8 r
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));# ]5 r& o( Q2 b6 h
  30.         // 把文字填充到画布' ~  g' V' H: y$ _" F# g4 Z2 R" Y
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    ; z3 t8 q+ S( b! ^2 y6 g5 ]
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);9 k, A; J# u6 M/ f% Q
  33. }+ f: j6 Z5 ^$ X8 p3 F
  34. $_SESSION['code']=$code;//存储在session里
      |* k) s6 @- E
  35. for($i=0;$i<200;$i++){
    1 ?' O' ~5 b; n5 _, p2 U" |: |
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    , w9 H9 z9 [3 w- u& H- E- z( w0 Z
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);/ C0 ?1 C  U8 R& w/ C  k2 f, M
  38. }
    + \) K4 S0 }  I9 o& o
  39. for($i=0;$i<2;$i++){
    1 Y6 d; R( c. `8 X# U
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));3 x- m& U; k3 o5 z6 E9 R. z
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);( I/ t1 F, U- J  t+ z& @
  42. }! i  N5 J* d( H& B
  43. imagepng($image);% [" K% i9 i; |; M) o( K8 ~; Q) {
  44. imagedestroy($image);
    7 f/ W( ~/ ?5 E- K
  45. % v5 o9 Y4 _' E3 d# t9 j5 U% m
  46. ?>
复制代码

# m; t7 o) {. R, ?% Hform.php0 T4 i/ O% ^* ]
  1. <?php+ ?. l# T: Z7 P; |7 b* S
  2.     if(isset($_REQUEST['code'])){0 \. f5 b( T! `' N9 ^
  3.          session_start();
    8 W6 \2 x* g7 D  d
  4.         if($_REQUEST['code']==$_SESSION['code']){
    3 q3 X5 N& C0 H& s& j
  5.             echo "<font color='red'>输入正确</font>";
    , f! l" z' _' B$ I5 K9 s2 j- A
  6.         }else{% d4 M2 ?2 U* Z& g3 o
  7.             echo "<font color='red'>输入错误</font>";* L5 m; u1 x0 i% b5 k
  8.         }
    ) M( N' {1 \, u5 B6 g
  9.     }, h$ i) @0 q! u/ i
  10. ?>  j: w( y! Z- `5 w8 l3 O
  11. <!DOCTYPE html>
    * K% b4 u8 C, f& J$ K  \
  12. <html>
    ! x: y2 _  d9 `' r3 u" r" H2 \
  13.     <head>0 U% q- X' w7 C4 Q; e7 a
  14.         <meta chartset="UTF-8" />
    0 D, g% x2 n6 L
  15.         <style type="text/css" rel="stylesheet">: u* q9 h/ k1 s4 z( J" e
  16.             a{  K8 p6 L- E! D4 L
  17.                 text-decoration:none;% m9 g) ~7 ^* v* J& C$ v! O+ K
  18.                 font-size:30px;
    ; S3 ]9 T# g" M- O' Z: Q9 l. l
  19.                 color:blue;9 h6 p- D9 N0 L
  20.             }- l% K" a! y) @, b
  21.             a:hover{# g; N4 k2 v2 R, g; A! |
  22.                 text-decoration:underline;
    % D( t+ ^1 B6 V
  23.             }
    0 b9 D, \: \' ~+ s  B
  24.         </style>
    * h, d. {4 @* D3 M6 p
  25.     </head>
    ) L1 K* ~- I+ K; X' e0 F
  26.     <body>
    $ d$ T% B  z  q5 u% t* G
  27.         <form action="form.php" method="get">
    & m2 o1 L+ c* t* S' E9 ?6 a
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    9 L: ~! w" e) ^
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br /># H  K, F  l4 F' b: t7 j
  30.             请输入验证码: <input name="code" /><br />
      u% }) x6 \% ]' N; w) r* ~
  31.             <input type="submit" value="提交" />/ l: S  B" U+ f2 V0 \
  32.         </form>( t4 K3 n0 L1 s3 O
  33.     </body>2 S( i, Q0 ^) |* o: u! D( ?. a3 P: B1 l
  34.     <script type="text/javascript">) ?/ O$ ?# y5 v8 `
  35.         0 X( A1 _/ a( B" G
  36.         function changeCode(){
    * ^3 r+ D% W4 a
  37.             var img=document.getElementById('img');- i$ g, X# X, t# r, ^0 k) q; E
  38.             //img.src='index.php?r='+Math.random();, y. N5 X1 e2 u5 |8 G
  39.             img.setAttribute('src','index.php?r='+Math.random());
    $ z8 V/ y" T5 Q# o8 ^" l( v  {2 K; K- [
  40.         }
    " S% v% W1 H) ]4 a7 R8 w8 T
  41.     </script>
      Z0 [8 a8 b7 i8 Z
  42. </html>
复制代码

) S! c/ t0 A* w  e" m1 t# ~+ X4 H5 q- F0 H- ?( t5 Q: O& R9 Y! k# b
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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