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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
! ?8 y; R& @- [& F# o
  1. <?php0 K9 r6 r3 u. y0 B
  2. session_start();: Q8 h* ~' [  S3 M5 M
  3. // 丢弃输出缓冲区的内容 **
    . K9 d5 [$ R$ D6 m& I2 X0 _0 q
  4. ob_clean();
    2 A3 X5 [5 i9 s' f1 A
  5. header ('Content-Type: image/png');! J: ]  D8 w0 ?; d: e- b1 P+ {5 y
  6. // 1 创建画布
    ( p. T6 x4 e7 F6 D1 y
  7. $image=imagecreatetruecolor(100, 30);
      W1 K; ?" C& U1 v2 ]
  8. // 2 画一个矩形并填充背景颜色为白色7 k* ]; H* |0 k9 N
  9. $color=imagecolorallocate($image, 255, 255, 255);
    + Z5 C' f$ T6 t# q, {7 {- q
  10. imagefill($image, 20, 20, $color);
    0 G# o- d, c) g1 y
  11. // for($i=0;$i<4;$i++){8 f4 m2 j. f' F3 |
  12.     // $font=6;
    / R% U5 ?$ Z% _3 [7 q
  13.     // $x=rand(5,10)+$i*100/4;
    & [0 y* t% ]6 M0 `% T
  14.     // $y=rand(8, 15);# r1 y( Z" a8 ]: p$ v. f$ F9 x  k
  15.     // $string=rand(0, 9);
    6 d8 p: v5 `+ K! a2 q
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    ; Q4 k1 R& P2 d  [! r! u1 R
  17.     // imagestring($image, $font, $x, $y, $string, $color);& j: V) N9 e  A7 j# D( F- N
  18. // }) i1 Y% T8 ]2 H7 G
  19. $code='';
    ; ]/ W% W$ ~4 V2 K. o$ J
  20. for($i=0;$i<4;$i++){
    ' l' Z# H1 X  Z
  21.     $fontSize=8;) J: E$ [& A1 m
  22.     $x=rand(5,10)+$i*100/4;
    # S' \) r! C& w' c, t
  23.     $y=rand(5, 15);
    : T, B& d6 ]4 Y; N
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    * U3 M4 M) }* L2 R: d
  25.     $string=substr($data,rand(0, strlen($data)),1);  h1 n2 z# b3 c1 U4 i- u: M4 v7 |
  26.     $code.=$string;
    & v+ W2 [( T" D/ D: J
  27.         // 3.2 加载自定义字体
    3 {0 _3 A/ k: M6 P" q/ E
  28.         $font_file = '1.ttf';6 W& I5 @. j3 I+ d+ j9 {8 M
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));5 U, i& h! a. H) S. {0 Q! G
  30.         // 把文字填充到画布+ @6 k8 m3 L3 L8 h* B. ?4 Y3 Z! w
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    % L; f1 w" ]7 E- G: s" V
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    " l' g6 n) n6 h3 h% k6 w6 @
  33. }
    & F$ u0 ~( B9 g& e# R) Q2 R* c" B
  34. $_SESSION['code']=$code;//存储在session里
    $ z, H$ z7 H6 n$ Z; ~" B, I
  35. for($i=0;$i<200;$i++){
    + \% {# T* r( c$ q7 s) r; y/ G' f
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));, X& }0 s8 p8 W" d1 e3 m, V; p
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);# Z# ~; W0 B% ~
  38. }' C$ N" M( c8 x% b6 R/ M/ R: s+ a2 F4 C
  39. for($i=0;$i<2;$i++){
    9 W3 W$ L! ~0 \
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    - X* h* t7 p/ S( I% N
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);9 @8 k# t1 f* K, g3 t1 L+ A
  42. }
      X  b. T: N7 C& w! x. G& N  _& J
  43. imagepng($image);1 c% E" n5 p; L$ g: O* d( {# Z% g5 d
  44. imagedestroy($image);
    . }, t- d3 a% |2 L9 E: G; ?

  45.   \' j9 v3 y3 {) e* N3 g
  46. ?>
复制代码

( O. \; F) X' h1 ?  Wform.php
& e7 s: U) w6 |/ Q: }/ w; k8 j* g
  1. <?php
    0 `  }( ?+ A8 Z5 `1 }
  2.     if(isset($_REQUEST['code'])){
    9 h9 S' U8 z' N6 i8 ~' _
  3.          session_start();/ {0 }: S; g2 Y, `9 q, H
  4.         if($_REQUEST['code']==$_SESSION['code']){( Z' S2 a& a" B/ E1 n
  5.             echo "<font color='red'>输入正确</font>";
      q' _' S- ?2 P. _( i9 q
  6.         }else{
    ) M2 H/ J7 m5 f  V
  7.             echo "<font color='red'>输入错误</font>";: }# V7 }* T$ Q0 ~
  8.         }
    ; i. i2 E0 c$ l4 B$ [0 R: O! ?
  9.     }9 R5 c, i: R: x: E( Y+ x& k
  10. ?>& @" k: I+ {' T+ _9 X
  11. <!DOCTYPE html>/ m! ~4 E% ~& N' s
  12. <html>
      N% ]  W' z' O3 J. i
  13.     <head>5 ]  b) n. z8 A8 m5 {
  14.         <meta chartset="UTF-8" />6 j# W/ ^5 L" i3 c$ c' z
  15.         <style type="text/css" rel="stylesheet">& Q' c+ ^' \/ ?: D8 d4 ^% w
  16.             a{
    2 C- l4 D+ c$ D6 j; F; w# A$ N
  17.                 text-decoration:none;  G. X7 B$ {; d6 P+ ^
  18.                 font-size:30px;
      Z" H, r( Q+ l( X0 ]! s
  19.                 color:blue;# E; \* {. G) _) v- b
  20.             }8 x& l  N0 L! j0 y( z  N
  21.             a:hover{9 \& I3 @5 j) i; X  S' P
  22.                 text-decoration:underline;
    * C! I' `* G: O# T6 d6 C+ V; m
  23.             }
    ( ]: R* x0 l, m3 K, I6 L
  24.         </style>
    2 }& D* I! Q6 x( W9 C! v( v8 ]; b
  25.     </head>
    0 t( L1 o9 f$ |: e
  26.     <body>
    & V" R! d8 H9 m+ e0 j+ K$ x
  27.         <form action="form.php" method="get">  j9 Q" Q! M" b; {
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>* b1 E5 z$ E  ^$ r4 S7 X3 o0 Q
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    ; h  D. y9 ]' b8 i4 N6 I, z6 E
  30.             请输入验证码: <input name="code" /><br />
      x  o" U7 W+ C" `3 B. L
  31.             <input type="submit" value="提交" />
    - Y  ?. l% |$ l3 A
  32.         </form>& L! q: A% v9 _5 J8 I7 I
  33.     </body>, ]5 k" g, ~* k5 S5 O: s9 W. u
  34.     <script type="text/javascript">, y6 F/ f- R0 X5 M
  35.         
    5 Q/ o# b6 B% o* q% I+ p) m
  36.         function changeCode(){
    4 z! n! L6 S4 x- H3 j$ g/ S& A8 @( t  ?
  37.             var img=document.getElementById('img');0 s$ p- z4 f* g! l2 ^
  38.             //img.src='index.php?r='+Math.random();% k5 r6 T/ v2 E3 j! Q
  39.             img.setAttribute('src','index.php?r='+Math.random());
    5 d0 y) O+ `' s! e: S
  40.         }( d, U/ N7 h; g" b
  41.     </script>& @% J9 V6 u, J- {
  42. </html>
复制代码

/ K- O0 v3 ?4 ?( K8 e, d* H1 H& D) y! ?2 y( i
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-6-20 04:14 , Processed in 0.060316 second(s), 19 queries .

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