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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php' a: s5 m: Z+ e1 e  I
  1. <?php
    * ]7 k- r* z8 j7 v3 S$ j
  2. session_start();
      E9 b9 S4 P" V- H" u
  3. // 丢弃输出缓冲区的内容 **7 B, N; |; `+ c& N1 c3 M, s' D
  4. ob_clean();
    5 {% ^' k# T1 C1 E
  5. header ('Content-Type: image/png');4 X+ d: v2 u9 I5 c6 T
  6. // 1 创建画布
    + c6 l  ?  C8 L% ?& F
  7. $image=imagecreatetruecolor(100, 30);9 y6 ^# K" s: ?  W+ M# j* y
  8. // 2 画一个矩形并填充背景颜色为白色
    5 a+ Y9 o" F+ y7 p2 h3 ?
  9. $color=imagecolorallocate($image, 255, 255, 255);
    # }4 w' q6 N  {; d
  10. imagefill($image, 20, 20, $color);
    ) X9 T7 P! [1 ?/ z: R4 y* k/ p
  11. // for($i=0;$i<4;$i++){: v9 a' @2 j8 q& b/ x: H- u6 ]
  12.     // $font=6;
    . ?% S6 G, ^5 B& A
  13.     // $x=rand(5,10)+$i*100/4;: {. F' k+ ^- b) V; @
  14.     // $y=rand(8, 15);- m" s. j6 ]- e9 O3 i
  15.     // $string=rand(0, 9);
    7 F1 p4 Y" j. I, A7 f8 w' L
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    # F$ g: ?: m+ L( O  e
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    & \) s9 {; V! B6 {7 ]
  18. // }
    . f7 G, Y. t, T6 H
  19. $code='';
    0 n: ^, p; h4 e# |# s  s# {3 p  E
  20. for($i=0;$i<4;$i++){
    9 _5 x! B) _: Z$ Y
  21.     $fontSize=8;3 [) o' V2 J# L  L9 ]/ G
  22.     $x=rand(5,10)+$i*100/4;# i2 d! f! X! ?% y8 l
  23.     $y=rand(5, 15);+ J9 J- {' q* [6 ^# K
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    ( ?. n. |# P5 k7 c. g0 f5 ?2 g
  25.     $string=substr($data,rand(0, strlen($data)),1);' o2 v2 p. k; t
  26.     $code.=$string;
    " P2 s% M1 l! i# l6 W
  27.         // 3.2 加载自定义字体- ?" f1 P' ^6 N' q8 q
  28.         $font_file = '1.ttf';
    : k. h; [  O3 P7 _" u  p
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));- Q6 j3 W- @, ~8 E; ~
  30.         // 把文字填充到画布2 [6 P& E! Y2 X6 B+ V  t5 J; ?9 x
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);+ z  X) S7 j" T/ J6 Q$ v. ?% J
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);6 e' R8 {  E. h, r! D$ ^& r
  33. }% u2 F/ g/ V7 ~) G
  34. $_SESSION['code']=$code;//存储在session里" q) @) p5 _- ^) @
  35. for($i=0;$i<200;$i++){
    * u# u9 L9 @  F3 a- h' P  U
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));4 U, ?0 }; ?& B  p8 A
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    " A0 f0 N% w! A0 Q) Z! J0 v% M
  38. }
    . {, h6 K& @9 ^
  39. for($i=0;$i<2;$i++){& Z* s1 e6 @- Z% g* w+ J( ~
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    1 Y4 {4 x- r' s
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    $ _6 I( x& ?+ v1 n, J; k, j( n
  42. }
    4 j: A; s+ g1 `
  43. imagepng($image);
    6 Y" Y7 D$ q$ b0 v, @7 K* i8 [& M9 ~( M
  44. imagedestroy($image);
    # J. w& a9 E. G8 n  c
  45. & A. f3 V; j# {- ?8 j) L0 h
  46. ?>
复制代码

# u+ w! q+ d! e* q- Xform.php' U2 t/ Y0 }* W$ H
  1. <?php9 B, M$ w) b1 v3 C; ]
  2.     if(isset($_REQUEST['code'])){
    8 J' e& N" s5 K/ V% h
  3.          session_start();7 I8 M7 h8 R8 p# w1 I& m
  4.         if($_REQUEST['code']==$_SESSION['code']){( s% `  z5 K) ~
  5.             echo "<font color='red'>输入正确</font>";
    + N7 M/ w0 \3 p" |1 E7 N9 n6 N/ b
  6.         }else{9 P: Y! q, e( K2 A8 A: |8 O
  7.             echo "<font color='red'>输入错误</font>";
    8 o& Y. @5 @4 K  D
  8.         }$ h/ E, w& M+ Q; U
  9.     }! z; k! @8 C( {5 }7 i7 q
  10. ?>( d! J* z2 s5 o' B+ i( N7 n
  11. <!DOCTYPE html>. l- m1 t# W8 `/ N4 C4 N
  12. <html>6 P' t& _( i; v
  13.     <head>
    & |# I8 Q$ T5 f
  14.         <meta chartset="UTF-8" />
    2 o; P! j2 P1 Y
  15.         <style type="text/css" rel="stylesheet">
    + K; w7 d0 f/ L# M: g2 ?8 M
  16.             a{
    : a. ~7 N% q6 t8 A
  17.                 text-decoration:none;
    0 l: H* H% S. z' f  L% p
  18.                 font-size:30px;9 g* N, Y( u5 e- \5 z1 R% L$ j
  19.                 color:blue;
    8 I/ d, b7 X! x! s
  20.             }
    . ?# j6 `: o6 p* ]0 l9 T- g& m
  21.             a:hover{
    ' m, V4 L6 `0 j$ \3 ]
  22.                 text-decoration:underline;
    , Z- u9 g$ g4 l
  23.             }6 l/ h1 B+ m& w) ^4 W
  24.         </style>
    ' A" o4 e/ P" f$ o: K8 o" `
  25.     </head>
    " ?. k. s! \- D6 b/ o. F; n: x
  26.     <body>
    ( h3 g7 U1 D3 q0 h5 G. p& S
  27.         <form action="form.php" method="get">: y3 B) C8 ]2 t: b/ V' f
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    : [) v" t* Q3 b' W% q- B
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    , T& b0 Y9 x9 y
  30.             请输入验证码: <input name="code" /><br />7 J: e4 [. d+ H' k7 q
  31.             <input type="submit" value="提交" />
    ! T) ]: z9 m4 [0 Q' T  J, a& _
  32.         </form>, G3 V1 [) ^5 W4 N, e# {5 S, X
  33.     </body>
    & K! o% T# _' l: T$ P' B4 L
  34.     <script type="text/javascript">' p& D/ l+ }0 u8 I+ n0 f0 [
  35.         
    8 ~7 S" i1 O* h$ q- I& X
  36.         function changeCode(){2 h# w6 i; J/ |5 Q1 A; A6 P
  37.             var img=document.getElementById('img');
    1 i9 S" J9 C( V. j% a9 I" q
  38.             //img.src='index.php?r='+Math.random();
    7 K$ K; a; u0 M! C1 G
  39.             img.setAttribute('src','index.php?r='+Math.random());
    3 R/ t" K  Z3 E% v! F3 _: N: p6 y
  40.         }
    ) G5 K( a# a. }5 r5 C* ^
  41.     </script>6 x+ m3 \. E% v: J3 c
  42. </html>
复制代码
3 m: t+ Q7 g4 `7 E( `0 g
+ T( C! I# z# J8 w) o3 B+ B$ T+ a# i
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-9-21 03:11 , Processed in 0.051598 second(s), 19 queries .

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