ホームページ  >  記事  >  バックエンド開発  >  PHP 検証コード生成コード

PHP 検証コード生成コード

WBOY
WBOYオリジナル
2016-07-25 08:43:23982ブラウズ
  1. class ValidationCode
  2. {
  3. //プロパティ
  4. private $width;
  5. private $height;
  6. private $codeNum;
  7. private $image;
  8. private $disturbColorNum; //干扰元素数目
  9. private $checkCode;
  10. function __construct($width=80,$height=20,$codeNum=4)
  11. {
  12. $this->width=$width;
  13. $this->height= $height;
  14. $this->codeNum=$codeNum;
  15. $number=floor($width*$height/15);
  16. if($number>240-$codeNum)
  17. {
  18. $this->disturbColorNum= 240-$codeNum;
  19. }else
  20. {
  21. $this->disturbColorNum=$number;
  22. }
  23. $this->checkCode=$this->createCheckcode();
  24. }
  25. function getCheckCode()
  26. {
  27. return $this->checkCode;
  28. }
  29. プライベート関数 createImage(){
  30. $this->gt;image=imagecreatetruecolor($this->width,$this->height);
  31. $backcolor=imagecolorallocate($this ->image,rand(225,255),rand(225,255),rand(255,255));
  32. imagefill($this->image,0,0,$backcolor);
  33. $border=imagecolorallocate($this-> image,0,0,0);
  34. imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
  35. }
  36. プライベート関数setDisturbColor(){
  37. for($i=0;$i<$this->disturbColorNum;$i++){
  38. $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand (0,255));
  39. imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
  40. }
  41. for($i=0;$i {
  42. $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
  43. imagearc($ this->image,rand(-10,$this->width),rand(-10,$this->height),rand(30,300),rand(20,300),55,44,$color);
  44. }
  45. }
  46. プライベート関数outputText($fontFace=""){
  47. for($i=0;$i<$this->codeNum;$i++)
  48. {
  49. $fontcolor=imagecolorallocate($this-> image,rand(0,128),rand(0,128),rand(0,128));
  50. if($fontFace=="")
  51. {
  52. $fontsize=rand(3,5);
  53. $x=floor($this- >width/$this->codeNum)*$i+5;
  54. $y=rand(0,$this->height-15);
  55. imagechar($this->image,$fontsize,$x ,$y,$this->checkCode{$i},$fontcolor);
  56. }
  57. else
  58. {
  59. $fontsize=rand(12,16);
  60. $x=floor(($this->width- 8)/$this->codeNum)*$i+8;
  61. $y=rand($fontsize,$this->height-8);
  62. imagettftext($this->image,$fontsize,rand( -45,45),$x,$y,$fontcolor,$fontFace,$this->checkCode{$i});
  63. }
  64. }
  65. }
  66. プライベート関数 createCheckCode(){
  67. $code="23456789abcdefghijkmnpqrstuvwrst ";
  68. $str="";
  69. for($i=0;$i<$this->codeNum;$i++)
  70. {
  71. $char=$code{rand(0,strlen($code)-1) )};
  72. $str.=$char;
  73. }
  74. return $str;
  75. }
  76. プライベート関数 OutputImage()
  77. {
  78. if(imagetypes()&IMG_GIF)
  79. {
  80. header("Content-Type:image/gif" );
  81. imagepng($this->image);
  82. }else if(imagetypes()&IMG_JPG)
  83. {
  84. header("Content-Type:image/jpeg");
  85. imagepng($this->image);
  86. }else if(imagetypes()&IMG_PNG)
  87. {
  88. header("Content-Type:image/png");
  89. imagepng($this->image);
  90. }else if(imagetypes()&IMG_WBMP){
  91. header ("Content-Type:image/vnd.wap.wbmp");
  92. imagepng($this->image);
  93. }else
  94. {
  95. die("PHP不支持图片验证码");
  96. }
  97. }
  98. //この方法を介して浏览器に画像を出力
  99. function showImage($fontFace="")
  100. {
  101. //画像背景
  102. $this->createImage();
  103. //干扰元素を配置します
  104. $this-> setDisturbColor();
  105. //方向画像中随机画出文本
  106. $this->outputText($fontFace);
  107. //方向画像
  108. $this->outputImage();
  109. }
  110. function __destruct()
  111. {
  112. imagedestroy($this->image);
  113. }
  114. }
  115. function checklogin(){
  116. if(empty($_POST['name']))
  117. die( 'ユーザー名はできませんempty');
  118. if(empty($_POST['password']))
  119. die("パスワードを空にすることはできません");
  120. if($_SESSION['code']!=$_POST['vertify'])
  121. die("認証コードの入力が間違っています".$_SESSION['code']);
  122. $username=$_POST['name'];
  123. $password=md5($_POST['password']);
  124. / /存在するかどうかを確認します
  125. conndb($username,$password);
  126. }
  127. function conndb($name="",$ps=""){
  128. $conn=mysql_connect('localhost','root','123456 ' );
  129. if(!$conn) die("データベース接続に失敗しました".mysql_error());
  130. mysql_select_db('5kan',$conn) または die('データベースの選択に失敗しました'.mysql_error());
  131. mysql_set_charset( ' utf8',$conn);
  132. $sql="ユーザー名='{$name}' およびパスワード='{$ps}' である k_user から ID を選択します";
  133. $result=mysql_query($sql) または die(" SQL ステートメント エラー".mysql_error());
  134. if(mysql_num_rows($result)>0) die("ログイン成功");
  135. else die("ユーザー名またはパスワードが間違っています");
  136. mysql_close($conn);
  137. }
  138. session_start();
  139. if(!isset($_POST['randnum']))
  140. {
  141. $code=new ValidationCode(120,20,4);
  142. $code->showImage("comicbd.ttf " ;
  143. コードをコピー
確認コード、php

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:ジョームラ WeChat次の記事:ジョームラ WeChat