• 创建 PHP 文本验证码


    PHP Text Captcha 是由免费的验证码脚本生成的文本图像。它用于保护您的登录表单、注册表单或任何表单免受垃圾邮件、滥用和黑客攻击。

    在本文中,我们将学习如何在没有 google captcha API 的情况下创建 PHP Text Captacha。PHP Text Captcha 是用于创建复杂图像的开源脚本。

    PHP文本验证码的特点:

    • 代码结构很小。
    • phpTextClass 用于创建验证码图像
    • 代码是完全可定制的
    • 带有随机线条和噪音的安全功能
    • 防止垃圾邮件发送者和滥用脚本。
    • 消除从随机黑客脚本中破解信息的可能性。

    phpcaptchaClass.php

    在这个文件中,我们创建了一个 phpcaptcha 类、hexTo RGB 和 ImageTTFCenter 函数来创建文本验证码的 imgae。

    1. /* phpcaptcha class, version 1.0
    2. created by www.tutorialswebsite.com (Pradeep Maurya)
    3. october 5, 2017
    4. */
    5. class phpcaptchaClass {
    6. public function phptext($text, $textColor, $backgroundColor = '', $fontSize, $imgWidth, $imgHeight, $dir, $fileName) {
    7. /* settings */
    8. $font = realpath(".") . '/monofont.ttf'; /* define font */
    9. $textColor = $this->hexToRGB($textColor);
    10. $im = imagecreatetruecolor($imgWidth, $imgHeight);
    11. $textColor = imagecolorallocate($im, $textColor['r'], $textColor['g'], $textColor['b']);
    12. if ($backgroundColor == '') {/* select random color */
    13. $colorCode = array('#56aad8', '#61c4a8', '#d3ab92');
    14. $backgroundColor = $this->hexToRGB($colorCode[rand(0, count($colorCode) - 1)]);
    15. $backgroundColor = imagecolorallocate($im, $backgroundColor['r'], $backgroundColor['g'], $backgroundColor['b']);
    16. } else {/* select background color as provided */
    17. $backgroundColor = $this->hexToRGB($backgroundColor);
    18. $backgroundColor = imagecolorallocate($im, $backgroundColor['r'], $backgroundColor['g'], $backgroundColor['b']);
    19. }
    20. imagefill($im, 0, 0, $backgroundColor);
    21. list($x, $y) = $this->ImageTTFCenter($im, $text, $font, $fontSize);
    22. imagettftext($im, $fontSize, 0, $x, $y, $textColor, $font, $text);
    23. if (imagejpeg($im, $dir . $fileName, 90)) {/* save image as JPG */
    24. return json_encode(array('status' => TRUE, 'image' => $dir . $fileName));
    25. imagedestroy($im);
    26. }
    27. }
    28. public function phpcaptcha($textColor, $backgroundColor, $imgWidth, $imgHeight, $noiceLines = 0, $noiceDots = 0, $noiceColor = '#162453') {
    29. /* Settings */
    30. $text = $this->random();
    31. $font = realpath(".") . '/monofont.ttf'; /* define font */
    32. $textColor = $this->hexToRGB($textColor);
    33. $fontSize = $imgHeight * 0.75;
    34. $im = imagecreatetruecolor($imgWidth, $imgHeight);
    35. $textColor = imagecolorallocate($im, $textColor['r'], $textColor['g'], $textColor['b']);
    36. $backgroundColor = $this->hexToRGB($backgroundColor);
    37. $backgroundColor = imagecolorallocate($im, $backgroundColor['r'], $backgroundColor['g'], $backgroundColor['b']);
    38. /* generating lines randomly in background of image */
    39. if ($noiceLines > 0) {
    40. $noiceColor = $this->hexToRGB($noiceColor);
    41. $noiceColor = imagecolorallocate($im, $noiceColor['r'], $noiceColor['g'], $noiceColor['b']);
    42. for ($i = 0; $i < $noiceLines; $i++) {
    43. imageline($im, mt_rand(0, $imgWidth), mt_rand(0, $imgHeight),
    44. mt_rand(0, $imgWidth), mt_rand(0, $imgHeight), $noiceColor);
    45. }
    46. }
    47. if ($noiceDots > 0) {/* generating the dots randomly in background */
    48. for ($i = 0; $i < $noiceDots; $i++) {
    49. imagefilledellipse($im, mt_rand(0, $imgWidth),
    50. mt_rand(0, $imgHeight), 3, 3, $textColor);
    51. }
    52. }
    53. imagefill($im, 0, 0, $backgroundColor);
    54. list($x, $y) = $this->ImageTTFCenter($im, $text, $font, $fontSize);
    55. imagettftext($im, $fontSize, 0, $x, $y, $textColor, $font, $text);
    56. imagejpeg($im, NULL, 90); /* Showing image */
    57. header('Content-Type: image/jpeg'); /* defining the image type to be shown in browser widow */
    58. imagedestroy($im); /* Destroying image instance */
    59. if (isset($_SESSION)) {
    60. $_SESSION['captcha_code'] = $text; /* set random text in session for captcha validation */
    61. }
    62. }
    63. /* for random string */
    64. protected function random($characters = 6, $letters = '23456789bcdfghjkmnpqrstvwxyz') {
    65. $str = '';
    66. for ($i = 0; $i < $characters; $i++) {
    67. $str .= substr($letters, mt_rand(0, strlen($letters) - 1), 1);
    68. }
    69. return $str;
    70. }
    71. /* function to convert hex value to rgb array */
    72. protected function hexToRGB($colour) {
    73. if ($colour[0] == '#') {
    74. $colour = substr($colour, 1);
    75. }
    76. if (strlen($colour) == 6) {
    77. list( $r, $g, $b ) = array($colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5]);
    78. } elseif (strlen($colour) == 3) {
    79. list( $r, $g, $b ) = array($colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2]);
    80. } else {
    81. return false;
    82. }
    83. $r = hexdec($r);
    84. $g = hexdec($g);
    85. $b = hexdec($b);
    86. return array('r' => $r, 'g' => $g, 'b' => $b);
    87. }
    88. /* function to get center position on image */
    89. protected function ImageTTFCenter($image, $text, $font, $size, $angle = 8) {
    90. $xi = imagesx($image);
    91. $yi = imagesy($image);
    92. $box = imagettfbbox($size, $angle, $font, $text);
    93. $xr = abs(max($box[2], $box[4]));
    94. $yr = abs(max($box[5], $box[7]));
    95. $x = intval(($xi - $xr) / 2);
    96. $y = intval(($yi + $yr) / 2);
    97. return array($x, $y);
    98. }
    99. }
    100. ?>

    index.php

    该文件用于创建表单数据并用于在提交表单后验证验证码图像。

    1. session_start();
    2. if (isset($_POST['Submit'])) {
    3. // code for check server side validation
    4. if (empty($_SESSION['captcha_code']) || strcasecmp($_SESSION['captcha_code'], $_POST['captcha_code']) != 0) {
    5. $msg = "The Validation code does not match!"; // Captcha verification is incorrect.
    6. } else {// Captcha verification is Correct. Final Code Execute here!
    7. $msg = "The Validation code has been matched.";
    8. }
    9. }
    10. ?>
    11. "utf-8">
    12. PHP Secure text Captcha.
    13. "./css/style.css" rel="stylesheet">
    14. "frame0">
    15. PHP Secure Text Captcha Demo.


  • "" method="post" name="form1" id="form1" >
  • "400" border="0" align="center" cellpadding="5" cellspacing="1"class="table">
  • php if (isset($msg)) { ?>
  • } ?>
  • "2" align="center" valign="top"> echo $msg; ?>
    "right" valign="top"> Validation code:"captcha.php?rand=" id='captchaimg'>

  • "captcha_code" name="captcha_code" type="text">

  • Can't read the image? click here to refresh.
  •  
  • captcha.php

    该文件用于创建验证码图像。在这个文件中,我们包含“phpcaptchaClass.php”文件并创建一个phpcaptchaClass对象。
    phpcaptcha():-此函数根据参数创建带有文本和线条或噪声的图像。

    1. session_start();
    2. include("./phpcaptchaClass.php");
    3. /* create class object */
    4. $phptextObj = new phpcaptchaClass();
    5. /* phptext function to genrate image with text */
    6. $phptextObj->phpcaptcha('#000', '#fff', 120, 40, 10, 25);
    7. ?>

    结论

    在示例脚本中,静态图像显示在 3D 图像库中。

  • 相关阅读:
    java-php-python-ssm网上书城系统计算机毕业设计
    BIM时代要来了?有了这份职业规划who怕who
    Spark SQL 结构化数据文件处理
    C语言学习记录(九)之结构体
    maven
    2023年电赛---运动目标控制与自动追踪系统(E题)OpenART mini的代码移植到OpenMV
    【Python 爬虫基本入门教程】讲解
    C语言力扣第32题之最长有效括号。用栈实现
    Python中if __name__ == ‘__main__‘
    [Prob] (Coupon collector)
  • 原文地址:https://blog.csdn.net/allway2/article/details/126685339