Detailed Explanation of PHP Simple Verification Code Function Mechanism Example

  • 2021-12-04 18:12:36
  • OfStack

This paper describes the functional mechanism of PHP simple verification code with examples. Share it for your reference, as follows:

The security of websites is a problem that developers can't ignore, At present, the most used method to improve the security of websites is to use the verification code function mechanism. Some only use a verification code with several digits and letters confused, some send short messages to verify by mobile phone, and some send emails to verify by email. But how is this verification code function mechanism realized? The following is a detailed explanation of the realization ideas and simple implementation methods of the verification code function mechanism.

1. The realization idea of verification code function mechanism

① Conventional verification code implementation:

a, generating a picture of png
b, setting the background color for the picture
c, setting font color and style
d, which generates a 4-digit random verification code
e, which adjusts the rotation angle and position of each generated character to be drawn on the png picture
f, adding noise and interference lines to prevent the registration machine from analyzing the original picture to register maliciously
g, output picture
h, free the memory occupied by the picture
i, save the verification code to session or database
j, which will be compared with the input verification code

② SMS (email) verification code mechanism:

a, which generates random verification codes of 4-6 digits
b, save each character generated to session or database
c, send the verification code to the user's mobile phone (mailbox)
d, the user inputs within the specified time
e, take out the verification code from session or database
f, which will be compared with the input verification code for verification

2. Simple mechanism to realize verification code function

① Create a new captcha. php and write the following code


<?php
/**
 * =======================================
 * Created by WeiBang Technology.
 * User: Wei ZhiHua
 * Date: 2016/10/12 0020
 * Time:  Afternoon  4:14
 * Power:  Realize the verification code function 
 * =======================================
 */
// Open session
session_start();
// Create 1 The size is  100*30  Verification code of 
$image = imagecreatetruecolor(100, 30);
$bgcolor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgcolor);
$captch_code = '';
for ($i = 0; $i < 4; $i++) {
  $fontsize = 6;
  $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
  $data = 'abcdefghijkmnpqrstuvwxy3456789';
  $fontcontent = substr($data, rand(0, strlen($data) - 1), 1);
  $captch_code .= $fontcontent;
  $x = ($i * 100 / 4) + rand(5, 10);
  $y = rand(5, 10);
  imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
// Save the generated verification code to session
$_SESSION['authcode'] = $captch_code;
// Add dot interference elements to the picture 
for ($i = 0; $i < 200; $i++) {
  $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
  imagesetpixel($image, rand(1, 99), rand(1, 29), $pointcolor);
}
// Add line interference elements to the picture 
for ($i = 0; $i < 3; $i++) {
  $linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220));
  imageline($image, rand(1, 99), rand(1, 29), rand(1, 99), rand(1, 29), $linecolor);
}
// Set head 
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
?>

② Create a new form. php and write the following code


<?php
/**
 * =======================================
 * Created by WeiBang Technology.
 * User: Wei ZhiHua
 * Date: 2016/10/12 0021
 * Time:  Afternoon  4:14
 * Power:  Realize the verification code function 
 * =======================================
 */
if (isset($_REQUEST['authcode'])) {
  session_start();
  if (strtolower($_REQUEST['authcode']) == $_SESSION['authcode']) {
    echo " Enter correctly! ";
  } else {
    echo " Input error! ";
  }
  exit();
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
  <title> Confirm verification code </title>
</head>
<body>
<form method="post" action="./form.php">
  <p> Verification code picture: 
    <img id="captcha_img" border="1" src="./captcha.php?r=<?php echo rand(); ?>" width=100 height=30>
    <a href="javascript:void(0)" rel="external nofollow" 
      onClick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()"> Change 1 A? </a>
  </p>
  <p> Please enter the contents of the picture: <input type="text" name="authcode" value=""/></p>
  <p><input type="submit" value=" Submit " style="padding:6px 20px;"></p>
</form>
</body>
</html>

The above is the production idea and implementation method of php verification code. From simple to complex, a perfect verification code function mechanism can be written according to these.

For more readers interested in PHP related contents, please check the special topics of this site: Summary of PHP Graphics and Pictures Operation Skills, Encyclopedia of PHP Array (Array) Operation Skills, Tutorial of PHP Data Structure and Algorithms, Summary of php Programming Algorithms, Summary of PHP Mathematical Operation Skills, Summary of php String (string) Usage and Summary of php Common Database Operation Skills

I hope this article is helpful to everyone's PHP programming.


Related articles: