PHP検証コードの実装コード

WBOY
リリース: 2016-07-25 08:46:16
オリジナル
903 人が閲覧しました
PHP検証コード実装原則

乱数や文字を生成してセッションに保存し(認証コードを検証するときに使用します)、生成した数字や文字を描画して目の前に提示します

検証コードを更新する: js を使用して検証コード画像のパラメーターを変更し、ブラウザーがキャッシュされた画像を読み取らないようにします。これにより、検証コードを更新する効果が得られます。

コード例
    $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
  1. $image=imagecreate(50,25);
  2. imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand( 0,125));
  3. $ color = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  4. for($i=1;$i<=4;$i++) {
  5. $date=$str[mt_rand( 0,strlen($str)-1)];
  6. $code.=$date;
  7. }
  8. session_start();
  9. $_SESSION['code'] = $code;
  10. imagestring($image,4,8,4 ,$code,$color);
  11. for($i=1;$i<=30;$i++) {
  12. imagesetpixel($image,mt_rand(0,50),mt_rand(0,25),mt_rand(125,200) );
  13. }
  14. for($i=1;$i<=mt_rand(1,5);$i++) { imageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0, 50),mt_rand(0,25),mt_rand(100,150)); } header("content-type:image/png"); imagepng($image); ?>
  15. 数字 + 文字確認コード (各文字には別の色 ):
  16. $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
  17. $image=imagecreate(50,25);
  18. imagecolorallocate($image,mt_rand(0,125), mt_rand( 0,125),mt_rand(0,125));
  19. $color[0] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  20. $color[1] = imagecolorallocate($image ,mt_rand (200,255),mt_rand(200,255),mt_rand(200,255));
  21. $color[2] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  22. $color[3] ] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  23. for($i=0;$i$date=$str[mt_rand(0) ,strlen ($str)-1)];
  24. $code.=$date;
  25. imagestring($image,5,6+$i*10,4,$code[$i],$color[$i]) ;
  26. }
  27. session_start();
  28. $_SESSION['code'] = $code;
  29. for($i=1;$iimagesetpixel($image,mt_rand(0,50) ,mt_rand (0,25),mt_rand(125,200));
  30. }
  31. for($i=1;$iimageline($image,mt_rand(0,50) ), mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),mt_rand(100,150));
  32. }
  33. header("content-type:image/png");
  34. imagepng($image );
コードをコピー
出典: PHP 検証コードの実装原則
確認コード、php

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!