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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php9 R+ a% ]8 {( d( Q
  1. <?php
    : E# q. L8 [7 m
  2. session_start();
    & n1 E+ j) Z! D6 e
  3. // 丢弃输出缓冲区的内容 **4 Q5 C6 o2 j5 Z/ _1 l& @# j
  4. ob_clean();% g( Y3 j* a) @' T- n
  5. header ('Content-Type: image/png');& W8 V7 g* p  k2 ~
  6. // 1 创建画布
    ! F8 M4 ^) ]" t2 U( X6 ?- U3 t
  7. $image=imagecreatetruecolor(100, 30);
    7 V: g5 g$ @2 ]7 \# F
  8. // 2 画一个矩形并填充背景颜色为白色8 P' r. i0 v; h# P/ W5 H8 r6 m/ N
  9. $color=imagecolorallocate($image, 255, 255, 255);9 J4 m' S% k2 M# q
  10. imagefill($image, 20, 20, $color);9 u) E- k% |7 [0 d% J; F/ p6 [
  11. // for($i=0;$i<4;$i++){
    , {, k4 M; h( z, d5 ]
  12.     // $font=6;
    1 ]% ~% ]% Y/ Q6 z8 H4 f
  13.     // $x=rand(5,10)+$i*100/4;# m4 H! n- e  y
  14.     // $y=rand(8, 15);9 [4 Y' l/ }$ o3 N$ o3 k
  15.     // $string=rand(0, 9);* H. D! B9 d* u( d1 m, B4 ^) U1 \1 k3 M
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    4 A+ @' i9 [' C5 r3 j  T: Z
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    " `) A5 [$ f* q
  18. // }
    / z  E* ~5 X, [& f0 ?
  19. $code='';: k, y' h0 Z1 u" O7 m; Y' }: f6 @
  20. for($i=0;$i<4;$i++){
      V. G) Y+ x  b3 i+ @
  21.     $fontSize=8;
    . [4 }7 x/ U9 o
  22.     $x=rand(5,10)+$i*100/4;
    & K: n; H7 X/ v! S0 P
  23.     $y=rand(5, 15);8 c6 P0 N3 Z6 y3 x. M
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    " _5 {! |' y# h$ M4 {+ O/ c" ?
  25.     $string=substr($data,rand(0, strlen($data)),1);
    % |! f% D1 p0 J4 k
  26.     $code.=$string;
    & z( ?7 f0 F& L; R, d' R7 V1 O
  27.         // 3.2 加载自定义字体, @& P: e+ i, D1 j1 W
  28.         $font_file = '1.ttf';. f6 t; _) H. j! K( R. W4 h
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
      }/ ]% J  z4 D, x1 x
  30.         // 把文字填充到画布" W  }5 ^0 U& V! [' n" A0 v/ Y* c
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);) O7 @) r, l: R+ h, w: X8 N. |2 r8 i
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    5 i$ ^: B1 E/ T# q, N; {
  33. }7 S! P  B5 c; a
  34. $_SESSION['code']=$code;//存储在session里, b( [/ U  q7 M, L, p# ~
  35. for($i=0;$i<200;$i++){: l; h3 ~# A$ w. Q( H8 B
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    ! Z) G$ D) @* r1 [' M. d
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);. j* i7 k8 J) ?! X
  38. }
    3 Z& h& Y6 E/ b0 d  a5 W
  39. for($i=0;$i<2;$i++){
    * j) e$ w, w6 M, D4 x" x$ U
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));3 I; _9 z# y- `; Y, u
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);) c2 ?% m- @: T$ T
  42. }9 `0 G6 k) d$ q, Q4 N; p
  43. imagepng($image);# S9 s/ K2 K& a: J
  44. imagedestroy($image);
    3 h$ N" d; e2 j) L

  45. ! N' z+ q: v. v6 e
  46. ?>
复制代码

/ M- U! @- j! _3 }9 S+ @/ v' d% wform.php
4 ]( S/ b3 d. g* A, S- [! t/ W; g
  1. <?php3 C& {8 @* s+ }4 }+ g# E! j7 Z
  2.     if(isset($_REQUEST['code'])){- W+ B2 [0 w  |/ h
  3.          session_start();3 @8 O7 w6 w  H7 @% |% a
  4.         if($_REQUEST['code']==$_SESSION['code']){
    9 [  M1 c* i& m1 v0 T
  5.             echo "<font color='red'>输入正确</font>";% u: b, Y& d9 ]" L  I0 s- ]
  6.         }else{
    ! G  e! {; q! X3 J2 ]
  7.             echo "<font color='red'>输入错误</font>";
    % A- \) F# K) W( V5 I
  8.         }: R0 S! [1 v8 Y6 T8 _
  9.     }
    6 P" p0 e8 @# \- O4 z, m
  10. ?>
    $ N. g( d$ k' ?: X
  11. <!DOCTYPE html>+ v% V, R% r/ k& C3 o3 L
  12. <html>
    5 V' \6 I; U# F
  13.     <head>
    2 q9 P  z/ J% {( A
  14.         <meta chartset="UTF-8" />+ {3 R7 O- ?4 G2 L* D0 l
  15.         <style type="text/css" rel="stylesheet">2 e! L/ v! K9 ^" O+ f1 g7 F0 ?
  16.             a{
    4 q; _7 s, C: x! e1 i2 b* S' A/ D
  17.                 text-decoration:none;
    9 z5 E; K, W! |
  18.                 font-size:30px;3 n! ^2 a5 c& S9 |: g6 @3 p
  19.                 color:blue;
    5 E. U4 P. i* B5 ^5 ~7 O/ ^! U! H
  20.             }
    ( L# C8 T6 B4 u# ~. d6 s0 B5 w
  21.             a:hover{1 s% r. P5 S: j# e$ \
  22.                 text-decoration:underline;- u3 m0 T" c9 \, E4 }8 _! S7 |
  23.             }- k, y4 s# S* X/ W6 Q' y* X0 s
  24.         </style>
    & g% t9 F4 k, ^8 r
  25.     </head>
    & O$ s  X+ V) b" A* b+ m
  26.     <body>( d- O- j  i9 H# e- n+ ?/ F& u5 S
  27.         <form action="form.php" method="get">
    / p) q9 i/ n( |2 g4 \* N
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>, K) F2 `" V# E& i
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    0 |5 d. {8 d/ }( q( y
  30.             请输入验证码: <input name="code" /><br />/ ]8 k& n' P% o. c2 v
  31.             <input type="submit" value="提交" />
    $ ?1 \* ^# H+ z" c: `. s" u
  32.         </form>( e/ l- S( Q% ~8 v& K
  33.     </body>
    , Z3 G6 t1 \2 w/ j/ n6 x9 w0 h
  34.     <script type="text/javascript">& ]0 a! a0 E$ g; A  {
  35.         
    % X' T6 {. Z! w4 \2 V5 g3 v' V1 C7 Q
  36.         function changeCode(){
    1 {/ v+ g& c4 I2 M
  37.             var img=document.getElementById('img');
    . m# }/ ?0 p3 B; J) K9 O
  38.             //img.src='index.php?r='+Math.random();* y/ ]1 ?; B  S0 P& ~* z+ w
  39.             img.setAttribute('src','index.php?r='+Math.random());
    0 r+ t5 L; F8 y
  40.         }
    0 {! A9 G9 X+ I! S5 d. D7 W
  41.     </script>6 [3 J  v- i( h8 b
  42. </html>
复制代码
  D& ?  {* T2 ?0 @! M" J7 J, m

" E9 }' c1 n/ Z3 S4 p! _
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-4-30 20:49 , Processed in 0.058938 second(s), 21 queries .

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