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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
- P4 Y# l) r4 g- h1 z
  1. <?php* V" H% \- P9 \/ F& }- h5 F
  2. session_start();$ }% z6 ~  m, |/ U. W
  3. // 丢弃输出缓冲区的内容 **
    ' N" i& R( C% [1 B3 n7 L  [% u
  4. ob_clean();5 D: Q5 C5 M6 P, e6 Z4 ?
  5. header ('Content-Type: image/png');
    4 |+ I0 }0 V  N
  6. // 1 创建画布7 q6 j  ?7 `  u
  7. $image=imagecreatetruecolor(100, 30);; Y0 h$ c/ w: t- ~& b
  8. // 2 画一个矩形并填充背景颜色为白色  K& v% S" ~& [! j
  9. $color=imagecolorallocate($image, 255, 255, 255);7 ^2 n4 O3 K3 M' Q# w3 a' k
  10. imagefill($image, 20, 20, $color);  k/ e9 M& O5 G$ J
  11. // for($i=0;$i<4;$i++){5 U, u' V! H  R$ X! W
  12.     // $font=6;
    1 s# P0 m+ R) d2 q* K5 {, O" Z/ J
  13.     // $x=rand(5,10)+$i*100/4;$ z: d+ o2 {  I& M- B
  14.     // $y=rand(8, 15);5 t1 X  E) a: U$ ?) h
  15.     // $string=rand(0, 9);$ C5 t$ J: @; N6 J
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    1 a; o2 a0 T& C: O4 B( C6 }: J' r4 L
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    ! \5 Z$ y3 ?2 p- \1 a
  18. // }) o+ J# q& [4 d- Q* j2 g
  19. $code='';
    0 j/ ^7 d" d1 c% B6 e& v9 [" I
  20. for($i=0;$i<4;$i++){
    0 D9 B; ~/ g1 o: [
  21.     $fontSize=8;* d' u' K  p3 ~2 {- `6 K
  22.     $x=rand(5,10)+$i*100/4;
    & H* ^7 I4 O# ~: |( E5 h5 l
  23.     $y=rand(5, 15);
    * n% |% ]4 W: X/ S, x- z. X6 w
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    # s( w: l& T, r% }4 W
  25.     $string=substr($data,rand(0, strlen($data)),1);
    1 W" s* c+ [' L3 l
  26.     $code.=$string;/ u% p6 q! _8 O5 C, c: ~
  27.         // 3.2 加载自定义字体
    & m: E% {% Y9 m2 S8 N  v
  28.         $font_file = '1.ttf';( o; ]( v1 U* ^' y. p* w3 O
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));7 r& {2 A) R* i, ?7 i
  30.         // 把文字填充到画布% H1 W/ G% E3 c5 Y, g& F
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);3 G0 p8 f# |3 t( |
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);  L) y$ U! E7 c& G; E, @8 M* a* V
  33. }
    2 R% \, o3 a3 g: q. ^1 z' p# o
  34. $_SESSION['code']=$code;//存储在session里+ V" T. W7 d5 T6 E7 r
  35. for($i=0;$i<200;$i++){! N% G$ Z3 N5 {" o- ^! V
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));! N0 L3 @  a, j/ e! O
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    / n% Q$ H% @3 T' f7 Z
  38. }8 K/ q# d/ p4 l- i* }8 L/ R
  39. for($i=0;$i<2;$i++){
    9 D: x# s  j0 i% [8 n1 g
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    7 ]3 k) U! ]! b- e5 g
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);# e$ R, t  g- m+ e- c
  42. }9 F: l4 S  P6 |8 Y; ^& z
  43. imagepng($image);
    3 q* V! b" d& `  n9 u0 `
  44. imagedestroy($image);, L* \6 q8 U/ K8 z. q9 J& K0 G

  45. - N1 J5 [' [. b3 g. y/ N& h
  46. ?>
复制代码
$ k$ N5 T9 v9 E* G9 P+ W' T$ X- {
form.php
7 m/ A: @- N: w7 b' }
  1. <?php6 ?9 m7 b" p- M$ Q2 O
  2.     if(isset($_REQUEST['code'])){
    " J0 }; y. \. [' h* p) o% {& ^
  3.          session_start();6 F- |& D% U/ S+ Z/ v
  4.         if($_REQUEST['code']==$_SESSION['code']){# N7 P; X' [5 t5 Y) r0 V) p
  5.             echo "<font color='red'>输入正确</font>";
    - N( \# W3 {/ G6 b  r: o
  6.         }else{
    : M& C0 l7 d  Q. C6 h. w0 E
  7.             echo "<font color='red'>输入错误</font>";6 d9 [7 Y3 v/ F  F
  8.         }
    9 [2 L) I! K! E( k' ~. C/ {
  9.     }
    * u) j6 s$ I( Y  |6 d* s# B
  10. ?>* ?7 C) Z2 I8 w% A% v# t# @2 L
  11. <!DOCTYPE html>
    ' f5 p1 r$ g) H; U& I0 t
  12. <html>
    - i- ^) w% X1 {5 d! a) g. J  H6 P
  13.     <head>: e/ _# Q/ E) d' A
  14.         <meta chartset="UTF-8" />* ?( i* T6 G8 l& b) w7 o
  15.         <style type="text/css" rel="stylesheet">
    ' N) q7 Y5 s6 F2 n$ N( T7 G7 i
  16.             a{4 f  P$ {: \, M( R5 [( G
  17.                 text-decoration:none;# S- O0 @- v4 H) q5 a
  18.                 font-size:30px;
    $ n1 ], Y% Y4 {1 S0 r; W
  19.                 color:blue;4 y. u' X1 g9 h; T4 `3 u; v& {; u
  20.             }
    $ q( z: }6 ?5 o
  21.             a:hover{2 ?- d: [  c/ Q# l
  22.                 text-decoration:underline;
    ' t, p! h4 R: \
  23.             }
    ! ?  S# S- y1 |( ?
  24.         </style>( ^  [6 P& O  u5 p2 c1 ]6 l
  25.     </head>1 V" k0 D1 \; C) Y
  26.     <body>
    $ k! B  b* `" W1 {1 U
  27.         <form action="form.php" method="get">
    , y' z) _6 x- U# T; o7 d
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>$ b. C6 @6 F" u- w( L& J
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    & ]5 j8 O, \5 D2 R& i5 d: x2 N# F; x
  30.             请输入验证码: <input name="code" /><br />' c/ Q6 Y! m( A; N0 e
  31.             <input type="submit" value="提交" />/ p  u. u2 L& e3 i3 ^
  32.         </form>1 j0 R+ o6 i3 j) D* p
  33.     </body>  V! H, R3 H$ v/ f; u
  34.     <script type="text/javascript">* X0 L* X! {# j& g9 `
  35.         . y$ I+ z6 O% G; _3 E! g9 E
  36.         function changeCode(){
    - z( H! m1 ^8 c
  37.             var img=document.getElementById('img');4 `; G5 Q4 t9 H+ G% R- l( |
  38.             //img.src='index.php?r='+Math.random();* D9 q5 T' J) C% L5 c4 R- t
  39.             img.setAttribute('src','index.php?r='+Math.random());) i) e# @+ _+ n  F) Z; ?; M1 }9 E
  40.         }
    5 s0 [" Q4 g0 I% j& o) g
  41.     </script>
    ! _  Q. s& V6 b: I6 U% l$ d
  42. </html>
复制代码

$ O: e, e- b. |, a$ [0 P+ \: P' X/ ^
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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