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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php) S) {. j# [/ m# L
  1. <?php9 H5 J$ \7 w8 S# u3 U) a+ J
  2. session_start();
    3 h( ~5 O4 b  R3 A, X4 |! Z0 y
  3. // 丢弃输出缓冲区的内容 **
    , M1 W' ~6 \5 d1 Y- J, _5 q
  4. ob_clean();
    % l: O$ u& ]2 n1 f' X
  5. header ('Content-Type: image/png');; [! N0 n/ U9 W! {& a6 C- @
  6. // 1 创建画布
    2 y. @0 v2 l1 T
  7. $image=imagecreatetruecolor(100, 30);
    + @$ o5 E/ {, _* C
  8. // 2 画一个矩形并填充背景颜色为白色
    , J. r' g2 U* T- @% g, @
  9. $color=imagecolorallocate($image, 255, 255, 255);# @. v7 o3 O- P+ s+ u
  10. imagefill($image, 20, 20, $color);
    0 \: M) x: C1 b" Z# w' g
  11. // for($i=0;$i<4;$i++){
    5 j0 U7 \0 f: w6 _: ?- W
  12.     // $font=6;
    ; F. R1 Q! ^/ C( J, v+ Y
  13.     // $x=rand(5,10)+$i*100/4;: D4 h* }3 T( s# B4 Q6 ?7 `
  14.     // $y=rand(8, 15);
    3 v' @* a+ P3 K+ G& m' R- L: q) l
  15.     // $string=rand(0, 9);
    8 x" ?& U8 J5 _4 q7 o
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));9 J# V) ~9 Z& s1 B& s5 |' G8 x
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    , i9 W  b% b) t
  18. // }; J1 d# Z! G, a; t
  19. $code='';! I  a" |: v  T4 E% n7 ?' S
  20. for($i=0;$i<4;$i++){  a# v8 j( g. f! k
  21.     $fontSize=8;# j0 F% f' T2 V; j5 [: ]3 E% v! S
  22.     $x=rand(5,10)+$i*100/4;
    6 K: w4 s* E4 Y2 N/ d- z
  23.     $y=rand(5, 15);
    7 J& \" E5 y4 L0 m: s$ ~/ \4 u0 y
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    / P: f8 Y, {0 o# i% O7 d
  25.     $string=substr($data,rand(0, strlen($data)),1);- p/ ~* @, k# R+ `2 t
  26.     $code.=$string;
    / i: f* e# u6 y" E
  27.         // 3.2 加载自定义字体' l- _1 V! ~8 \0 y6 R6 q7 R# ^& Y0 Y, i
  28.         $font_file = '1.ttf';7 i8 u. \$ S) V
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    , i: {9 w% s) Z+ @
  30.         // 把文字填充到画布) a. X+ ]" v' A1 R
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);4 E5 C4 J- L6 P6 C- a8 ^
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);1 `! f5 w, U% z$ R7 B
  33. }4 m) D* O! Y7 i" ]6 ~1 s
  34. $_SESSION['code']=$code;//存储在session里: m8 o& v' B! r) A$ n
  35. for($i=0;$i<200;$i++){# w2 U; l& i8 l# {1 Z  a
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));" Y, B8 D, x5 D
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    & {  e/ l/ K. n
  38. }* L' e8 X% L2 i- H4 x' q
  39. for($i=0;$i<2;$i++){8 h3 S0 Z7 m& k* l2 M
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    + ]. i, k' [7 T% f$ t
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);! z7 e  e' R& H/ d: ]( M
  42. }& `! {& S5 @, Z+ j9 y
  43. imagepng($image);
    0 g+ P, q9 f  N) _, q3 D
  44. imagedestroy($image);2 i. q6 \+ z5 Z/ D

  45. , l* @* ^$ V- n* ?
  46. ?>
复制代码

* B% Y$ U" ?3 Mform.php
2 Q& H4 V9 U  G. f. ~0 E4 C9 k
  1. <?php# o5 b* r- T+ j3 z
  2.     if(isset($_REQUEST['code'])){. ^8 y4 S0 |6 P3 M) L; d
  3.          session_start();
    ) {9 ^+ Y9 }& y& N( x3 N8 ]
  4.         if($_REQUEST['code']==$_SESSION['code']){
    6 u: P; h* X+ V! E: j* @  n5 Q2 {
  5.             echo "<font color='red'>输入正确</font>";
    1 b' n& m+ J% b! e% i
  6.         }else{
    ! Q9 v$ D: U1 Q' t' F( t) w
  7.             echo "<font color='red'>输入错误</font>";% g0 B) b9 Q8 u6 Q9 D7 R
  8.         }7 M, I( ^% t1 E+ K$ m2 o
  9.     }, g1 v) s9 |, |" ^& o4 j" N
  10. ?>3 A- f+ P* I8 Z
  11. <!DOCTYPE html>. A5 K: c: `! p# ~" ?5 ^
  12. <html>
    / ]4 r6 ?! G8 H9 z6 L
  13.     <head>
    . Z; h# y9 N5 h( a5 V. b' N' v
  14.         <meta chartset="UTF-8" />
    " t' j# w6 u9 {" W. a! o9 _- v; t
  15.         <style type="text/css" rel="stylesheet">3 r' s9 a+ T% F
  16.             a{' F5 n* H" P; X# R9 A" i
  17.                 text-decoration:none;+ h, [. O3 J) t% S  i) Y
  18.                 font-size:30px;( ~; k! J  }) U& o/ C
  19.                 color:blue;
    4 o! P: T) s. e# N8 O1 ]& Z
  20.             }3 j6 `' @* }6 o% O
  21.             a:hover{
    " g8 I) i  G* s" }( i0 \" c# m
  22.                 text-decoration:underline;( e$ G/ [, F* M' p; l) {" ?7 P
  23.             }0 L" L( P. Q1 G- W$ A3 t
  24.         </style>9 ]; Y% D# z: t$ f
  25.     </head>/ w% X1 G8 e7 ^7 v# A
  26.     <body>! W8 O% |5 I4 ^" f1 f
  27.         <form action="form.php" method="get">5 ?: k' D! ?* r5 Z7 h
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>% h6 Q  F1 N8 c6 V; T* n
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    ! B6 p% O: l6 [
  30.             请输入验证码: <input name="code" /><br />
    , y1 T. l5 f5 _6 y/ s7 H
  31.             <input type="submit" value="提交" />7 H7 Z, L" X* F5 ?
  32.         </form>
    2 t; G9 p3 U" ~9 `
  33.     </body>
    5 e: U6 M4 I/ I! I3 y1 t8 K
  34.     <script type="text/javascript">
      P6 o, g; I) y7 O; b
  35.         
    . ^  ]3 Q2 i" G6 x& V
  36.         function changeCode(){
    & d6 ^4 l& C+ Z# i7 D6 ^
  37.             var img=document.getElementById('img');
    5 {9 f3 F) L" p6 t0 f
  38.             //img.src='index.php?r='+Math.random();/ V: g& m9 U& B3 m- K$ ^  {
  39.             img.setAttribute('src','index.php?r='+Math.random());! o0 G$ i& G/ w
  40.         }
    . I4 \; U  f5 ^5 f
  41.     </script>  e$ j! s% a4 d7 t5 u0 w, ~$ p
  42. </html>
复制代码

1 m3 {3 J* A; N
2 ]8 m; ?: H3 r4 j7 @
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-5-2 18:24 , Processed in 0.057137 second(s), 22 queries .

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