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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php% ?+ i9 G9 O: n
  1. <?php' n& I9 I4 o4 z6 D* G1 \2 v6 t
  2. session_start();6 Y& m8 Z, m+ A7 c3 g
  3. // 丢弃输出缓冲区的内容 **
    ) w3 q/ U  |# q3 s
  4. ob_clean();
    # u/ ?- C# f9 K  v: q9 W
  5. header ('Content-Type: image/png');) x3 B( D& Q( r3 [
  6. // 1 创建画布# b( G. _' \8 D
  7. $image=imagecreatetruecolor(100, 30);
    6 o" @! D& j( w0 S$ @
  8. // 2 画一个矩形并填充背景颜色为白色; M# n) D+ {! D0 n
  9. $color=imagecolorallocate($image, 255, 255, 255);
    * C$ ~1 u, b) f. J. ^5 j
  10. imagefill($image, 20, 20, $color);  k# B# T6 v3 M3 r; M8 F
  11. // for($i=0;$i<4;$i++){
    ) z. T8 \( `3 X% H( B
  12.     // $font=6;% W% I) U  e' `, q! h) o1 c
  13.     // $x=rand(5,10)+$i*100/4;
    " J! I: w5 w. w
  14.     // $y=rand(8, 15);
    + g+ I* }" b* y/ U
  15.     // $string=rand(0, 9);
      [: `, \5 K0 D2 O8 d* p' _/ M
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));+ W& }; Z# Y) g/ C
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    4 J$ r. G# M4 G/ \* a+ o4 L/ t1 r9 j
  18. // }
    - p9 R3 Z& A3 a: h( S. L5 _
  19. $code='';+ t) m2 [$ a' M  A! s
  20. for($i=0;$i<4;$i++){
    8 E, `  X  Y# ]" d. b+ k
  21.     $fontSize=8;
    5 C% I" M/ I8 j2 w% `
  22.     $x=rand(5,10)+$i*100/4;7 k. q  x. ]6 C7 R4 T  k- [/ a
  23.     $y=rand(5, 15);
    1 Q1 m) g  d' r4 G8 y
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    ; @6 X: @% u4 e; p# ?7 N
  25.     $string=substr($data,rand(0, strlen($data)),1);5 b6 h. v  v# s4 n
  26.     $code.=$string;
    1 a% D! d# r  w) c
  27.         // 3.2 加载自定义字体
    " a2 x0 u' B0 u; X8 L! U
  28.         $font_file = '1.ttf';
    % q/ x( B4 v! K% B
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    : J" c2 E5 l4 o; J
  30.         // 把文字填充到画布2 K3 _2 i% A6 C: p
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);( O- J! D( X! P
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);. e* `( ~, ~3 H" U5 _/ O3 K
  33. }
    1 h5 B& k) @4 `& h2 i
  34. $_SESSION['code']=$code;//存储在session里& ]8 F- ]/ W. U; M4 i
  35. for($i=0;$i<200;$i++){
    ; a' ^! Q% N. L& n4 p
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));3 J+ e- t$ N, U9 k3 W. A
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);; `9 H3 S4 X0 p' X
  38. }
    ' C) [# ]( T- u  |
  39. for($i=0;$i<2;$i++){2 R! s: B& e( F2 j* n# e9 H
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    ( T  @# R5 q: y8 ]: E' [
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    8 G; y8 k; s9 @& h% y6 o/ s
  42. }
    4 w4 G. W. i* k# c$ _# c" t) Q
  43. imagepng($image);2 U6 B4 f8 x9 [/ }, d' h7 Y2 i
  44. imagedestroy($image);
    : N( }, I$ n% y  S, k
  45. * d" M4 x$ e+ q1 c: u
  46. ?>
复制代码
# V  W: j, ?$ ]) b& i& Q# u
form.php0 w7 p9 n+ q+ c* W4 U3 ]! J5 z
  1. <?php
    8 z9 j& n" d2 Z4 k: F  p
  2.     if(isset($_REQUEST['code'])){
    ' X" o5 o- M2 N& S& K' H8 X
  3.          session_start();. @, d* U4 P* G* i  |0 D, e  M  z
  4.         if($_REQUEST['code']==$_SESSION['code']){
    2 v. f" H( t9 M* u9 E
  5.             echo "<font color='red'>输入正确</font>";/ Y0 J) [. }* T# R- p% [: a, \
  6.         }else{7 E* S1 c; s5 J: L- \0 [
  7.             echo "<font color='red'>输入错误</font>";* V4 n. e# G9 s# c
  8.         }. Q0 ]4 D. ]9 G; S& Y5 F. |) N
  9.     }# N* E  D! Q  e- c( Y- [5 _
  10. ?>2 V/ ^* z- d/ g# |
  11. <!DOCTYPE html>2 k0 _% A8 }- d1 E; Z: Z0 h0 f9 [4 c
  12. <html>
    ! c% l* X( j7 t6 e) A% g
  13.     <head>1 B8 s" F' I! s! x3 W
  14.         <meta chartset="UTF-8" />
    ' V$ ?4 x- F4 s# o* w8 g$ |
  15.         <style type="text/css" rel="stylesheet">' w+ d! m7 e, N3 M2 x" [
  16.             a{) h+ ?4 y# }* z1 A  r
  17.                 text-decoration:none;
    8 Q! ~: ]$ q7 D
  18.                 font-size:30px;/ O7 Z7 z% D( `( L
  19.                 color:blue;# R! z! G4 S" }: _; B( M1 |, C
  20.             }
    6 q/ k0 I% U5 Y0 J) f+ O' }
  21.             a:hover{
    1 z6 s4 @; v; a* \- j
  22.                 text-decoration:underline;6 W* m; j  q% }7 v
  23.             }. K& ]8 J7 ]; t8 U' w% \( k) D
  24.         </style>
    * ]  C& O8 T5 {
  25.     </head>
    , l, h8 t+ t/ l' j( }; m: Q
  26.     <body>
    / S1 b; w8 k2 f0 x3 a, ~
  27.         <form action="form.php" method="get">
      q! p' ~' z( k8 W2 g8 a
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>: A) {. ]  x, |' F3 v
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    3 E# _4 E* _  P4 D$ @* V, R& z
  30.             请输入验证码: <input name="code" /><br />
    , y% o" P# Y) e5 E. z1 F  K$ J
  31.             <input type="submit" value="提交" />; b* I) J3 z9 H5 j
  32.         </form>
    9 r3 t3 \$ F4 H* [/ }
  33.     </body>
    5 n& m/ d; ?/ J9 c
  34.     <script type="text/javascript">
    " D# ?9 N" y+ \
  35.         $ `  t6 k5 i9 P5 q
  36.         function changeCode(){
    ; a0 w& p+ }+ k9 }9 x0 ^0 q' ^
  37.             var img=document.getElementById('img');# Q2 q3 C5 z# v& J
  38.             //img.src='index.php?r='+Math.random();
    & A" f- Z6 {- r6 j8 s, D5 S
  39.             img.setAttribute('src','index.php?r='+Math.random());
    % K! B- H! a: p* W" w
  40.         }
    # n+ H1 ^+ o# u$ q$ x3 [
  41.     </script>  u: ~0 T8 |* R3 P
  42. </html>
复制代码
' _8 x& B! l% d5 {

3 U/ j5 S; t; q! q; X
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 10:06 , Processed in 0.052554 second(s), 19 queries .

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