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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
* }# [3 y' _& E0 }- H6 s
  1. <?php9 S' n6 x& \) o8 ^8 I
  2. session_start();/ Z4 i& X: z3 \0 \
  3. // 丢弃输出缓冲区的内容 **
    8 \3 ~* h$ @$ `" J6 H% W
  4. ob_clean();2 M" \3 F  K7 g4 {2 I. I$ v
  5. header ('Content-Type: image/png');
    4 F# V' D' E$ A% [' k
  6. // 1 创建画布) ?4 d8 C/ {9 r2 A1 V7 Z  N
  7. $image=imagecreatetruecolor(100, 30);
    / g& L5 ]$ X0 z8 Y, X. c& v
  8. // 2 画一个矩形并填充背景颜色为白色1 \1 U, p4 _& Z! e' \& S
  9. $color=imagecolorallocate($image, 255, 255, 255);, C1 m) C& R8 Z
  10. imagefill($image, 20, 20, $color);& k5 X5 C& s) e3 J3 c3 y; n
  11. // for($i=0;$i<4;$i++){! r0 @7 Z1 K/ U" ^- n
  12.     // $font=6;" F7 j* B2 K$ F1 E
  13.     // $x=rand(5,10)+$i*100/4;8 q; S, ^  x6 Q
  14.     // $y=rand(8, 15);
    9 ^; q+ h4 w; [2 ~5 A
  15.     // $string=rand(0, 9);
    ) t6 E1 E. b  |8 ?
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));+ M3 V1 c& P+ i6 h- P) u
  17.     // imagestring($image, $font, $x, $y, $string, $color);" W! A, T/ s: J9 g0 v7 G. l
  18. // }- W/ h$ g, M2 `" n8 V
  19. $code='';2 [, F+ W; ]# k, C; {9 t
  20. for($i=0;$i<4;$i++){# k9 x, v; j( [3 E& ~9 m  c0 a
  21.     $fontSize=8;
    / S/ u) L" y. m* a% E* f
  22.     $x=rand(5,10)+$i*100/4;' V( F3 w+ B  h- A1 h/ d
  23.     $y=rand(5, 15);* G1 ^' l: W  G1 `
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    & I1 ]4 c* e$ @4 l8 v0 ?" m! T
  25.     $string=substr($data,rand(0, strlen($data)),1);/ h, g; b( F/ t+ l0 |; K
  26.     $code.=$string;9 }9 Y/ }2 Q" O  U2 p
  27.         // 3.2 加载自定义字体
    2 M4 u  W3 [7 E& C9 _0 }/ M
  28.         $font_file = '1.ttf';
    , b9 O, O" L$ m
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));( u" p' M% S: |4 E1 }) P7 N) ^
  30.         // 把文字填充到画布! ]) H0 J& h; {: x8 q/ G. ^
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);- w7 k! B4 K5 v; v: a; ]3 z) E
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);3 o, G* S4 x/ H
  33. }
    1 r0 `8 u, |) ?# @7 p
  34. $_SESSION['code']=$code;//存储在session里: Y8 H+ K* n5 D8 P0 S* h
  35. for($i=0;$i<200;$i++){
    - I: ?+ {7 @" x9 p
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));6 R# d7 g7 {5 C  q# j* o. X$ o
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);9 ]* I; J. x7 m# I# O  x$ D5 _# Z
  38. }4 l6 e9 t) {. S5 q% N4 K7 Z
  39. for($i=0;$i<2;$i++){) w7 s1 l; P3 W7 o4 W6 \
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));+ s- m1 Q1 C  W! A% U+ e
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);  S( J- l! V1 S, B+ }# p/ V
  42. }
    3 V8 z. Y0 R7 g
  43. imagepng($image);
    & ~0 p3 s5 b) i  z; d" T
  44. imagedestroy($image);! i* [) @" k$ W
  45. ! _7 B, ?  [4 o7 p! U
  46. ?>
复制代码
- d% Y0 U( P, k0 v
form.php
+ n6 X5 C& e- R' \
  1. <?php6 `! w: S) b; q, r" m4 b6 J  L
  2.     if(isset($_REQUEST['code'])){1 F) M- l. E& O/ B
  3.          session_start();2 ^9 S8 o% D/ [1 |+ T  ^' K
  4.         if($_REQUEST['code']==$_SESSION['code']){6 t2 S# N& y6 X! s
  5.             echo "<font color='red'>输入正确</font>";% w# z+ q( I& f7 ^
  6.         }else{
    * E2 ^) |) z9 |7 u0 }
  7.             echo "<font color='red'>输入错误</font>";0 O. W. G  h9 u! e, ^  _
  8.         }
    $ k6 Y6 o- n- J0 n
  9.     }9 L+ W$ O7 b- @- N. \+ u- z4 h5 {
  10. ?>
    ( k. O+ m6 D' g6 T% x+ d! [
  11. <!DOCTYPE html>
    ' h) W! p0 J" B/ D" Z& Z5 `7 r1 E6 q
  12. <html>
      s7 ^: ]- t, H1 t) j* C
  13.     <head>. B4 L1 G1 ~4 n
  14.         <meta chartset="UTF-8" />
    6 {( R3 K8 k: V7 W4 W
  15.         <style type="text/css" rel="stylesheet">* d! b0 j/ O1 w/ W
  16.             a{
    2 Z1 x% x6 o" P- P
  17.                 text-decoration:none;
    % v1 O/ w# D  R
  18.                 font-size:30px;' |7 d. _; {9 W& Q( W5 [& y  X
  19.                 color:blue;
    * m. \' l9 @9 y8 G8 Y1 F; D  \
  20.             }# j1 M+ x) |' U7 l( f
  21.             a:hover{
    8 O9 M+ G- A& D6 Q- H1 T: l5 v' z
  22.                 text-decoration:underline;
    2 F" S! P/ @! C6 {! _4 A& i! u
  23.             }
      ]0 Z6 r3 S$ L) ^5 y. _
  24.         </style>
    ! m) m' q6 g0 U: h( o
  25.     </head>
    ( Y# A2 K1 }' o2 o8 d1 |
  26.     <body>  p6 u1 l0 d6 L) S5 ?
  27.         <form action="form.php" method="get">- e9 ]" S2 g8 c1 ^4 E1 s* ]
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>* [* U4 l/ _1 ]) v% k/ F
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />1 N/ M! o! O  |" l/ L
  30.             请输入验证码: <input name="code" /><br />  d6 l5 Y3 o' C4 o7 P) S
  31.             <input type="submit" value="提交" />
    % f: \& x$ @& u7 S
  32.         </form>
    ( E8 X# {5 ]4 z# R  z: E
  33.     </body>8 n" D) u: N  c( M( n( ^' h
  34.     <script type="text/javascript">
    - q- G% c& I& Q1 R" k! p# w
  35.         
    $ Z8 X- b! ]( F; ~; g6 A; K
  36.         function changeCode(){
    5 k$ U. W% D! Y' U, Z3 y2 b7 U2 B# ^
  37.             var img=document.getElementById('img');
    " F% X1 v: i' A. C
  38.             //img.src='index.php?r='+Math.random();0 S" F' `  @8 B0 z
  39.             img.setAttribute('src','index.php?r='+Math.random());) R% I+ m1 B* C5 q' Y
  40.         }
    4 M/ U5 G/ P4 f$ H/ B" `0 _
  41.     </script>' @1 Y, }" ]# v8 ^/ V7 o
  42. </html>
复制代码

5 |. {# n4 U2 R. N( p3 \
1 k% W% E$ I. C( i5 y* p3 ^8 m
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 17:24 , Processed in 0.059666 second(s), 20 queries .

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