ホームページ  >  記事  >  バックエンド開発  >  PHP 検証コードの慎重な作成

PHP 検証コードの慎重な作成

WBOY
WBOYオリジナル
2016-06-23 13:23:02842ブラウズ

たとえば、PHP を使用して Web サイトのメンバーログインページを開発する場合、当然ながら検証コードを実装する必要があります。検証コードの画像は静的または動的にすることができ、テキストは中国語、英語にすることができます。 、フランス語、日本語など、はい、お好みで置き換えてください。検証コードの画像など:

:

もちろん変更可能ですが、PHPのソースコードをインポートすることで実現できます

コードは次のとおりです:

01.

02. クラス Imagecode{

03 . private $width ;

05. private $counts;

07. private $fonturl;

08 . プライベート $session;

09. function __construct($width = 120,$height = 30,$counts = 5,$distrubcode="1235467890qwertyipkjhgfdaszxcvbnm",$fonturl="C:WindowsFontsTektonPro-BoldCond.otf"){

10. $this->width=$width;

11. $this->height=$height;

12. $this->counts=$counts;

13. $this->gt;distrubcode =$distrubcode;

14. $this ->fonturl=$fonturl;

15. $this->session=$this->sessioncode();

17. $_SESSION['code']=$this ->session;

18. }

19.

20. function imageout(){

21. $im=$this->createimagesource();

22. $this->setbackgroundcolor($im);

23. $this->set_code($im);

24. $this->setdistrubecode($im);

25. $im);

26. ImageDestroy( $im);

27. }

28.

29. private function createimagesource(){

30. return imagecreate($this->width,$this- >高さ);

31. }

32. private function setbackgroundcolor($im){

33. $bgcolor = ImageColorAllocate($im, rand(200,255),rand(200,255),rand(200,255)); //±³¾°ÑÕÉ«

34 . imagefill($im,0,0,$bgcolor);

35. }

36. private function setdistrubecode($im){

37. $count_h=$this ->高さ;

38. $ cou=floor($count_h*2);

39. for($i=0;$i<$cou;$i++){

40. $x=rand( 0,$this->幅);

41. $y=rand(0,$this->高さ);

42. $jiaodu=rand(0,360)

43. $fontsize=rand( 8,15);

44. $ fonturl=$this->fonturl;

45. $originalcode = $this->distrubcode;

46. $countdistrub = strlen($originalcode); $dscode = $originalcode[rand(0, $countdistrub-1)];

48. $color = ImageColorAllocate($im, rand(40,140),rand(40,140),rand(40,140));

49. imagettftext ($im,$fontsize,$jiaodu, $x,$y,$color,$fonturl,$dscode);

50.

51. }

52. }

53. プライベート関数 set_code($im ){

54. $width=$ this->width;

55. $counts=$this->counts;

56. $height=$this->height;

57. $scode =$this->セッション;

58. $y=floor($height/2)+floor($height/4);

59. $fontsize=rand(30,35);

; fonturl="C:WindowsFontsAdobeGothicStd-Bold.otf ";//$this->fonturl;

61.

62. $counts=$this->counts;

63. for($i=0; $i 64. $char=$scode[$i];

65. $x=floor($width/$counts)*$i+8;

66. $ jiaodu=rand(-20,30);

67. $color = ImageColorAllocate($im,rand(0,50),rand(50,100),rand(100,140));

68. imagettftext($im,$) fontsize,$jiaodu,$x,$y ,$color,$fonturl,$char);

69. }

70.

71.

72.

73. }

74. プライベート関数 sessioncode(){

75. $originalcode = $this->distrubcode;

76. $countdistrub = strlen($originalcode);

77. $_dscode = ""; . $counts=$this->counts;

79. for($j=0;$j

80. $dscode = $originalcode[rand(0,$countdistrub-1) ) ];

81. $_dscode.=$dscode;

82. }

83. $_dscode を返す 84.

85. }

86. }

87. ヘッダー("コンテンツ- type : image/GIF");

88. $imagecode=new Imagecode(160,50);

89. $imagecode->imageout();


書いたらDreamweaverエディタで開きますアップロードOK。

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