Home Backend Development PHP Problem How to change the position of verification code in PHP

How to change the position of verification code in PHP

Apr 04, 2023 pm 05:28 PM

With the development of websites, verification codes have become a necessary security measure for registration or login on many websites. However, sometimes the location of the verification code does not meet our design requirements, so today we will talk about how to change the location of the verification code in PHP.

First of all, we need to understand the principle of generating verification code. Usually, the verification code is generated in a PHP script using the GD library. The GD library provides some functions for generating verification codes, such as imagecreate(), imagecolorallocate(), imagestring(), etc.

Next, we will explain these functions. If you are using another verification code generation method, you can modify it according to its generation principle.

  1. Place the verification code at the specified location

The verification code can be output to the canvas through the imagestring() function, and we can use it to control the location of the verification code.

The following is a basic verification code generation script:

<?php
session_start();
header("Content-type: image/png");

$code_len = 4;
$width = 80;
$height = 25;

$charset = &#39;abcdefghkmnprstuvwxyABCDEFGHKMNPRSTUVWXY3456789&#39;;
$code = &#39;&#39;;
for ($i = 0; $i < $code_len; $i++) {
    $code .= $charset[mt_rand(0, strlen($charset) - 1)];
}
$_SESSION[&#39;verify_code&#39;] = strtoupper($code);

$img = imagecreate($width, $height);
imagecolorallocate($img, 255, 255, 255);  //设置背景颜色

$font_color = imagecolorallocate($img, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
for ($i = 0; $i < $code_len; $i++) {
    $font_size = mt_rand(12, 16);
    $angle = mt_rand(-10, 10);
    $f_x = ($width / $code_len) * $i + mt_rand(0, 5);
    $f_y = $height / 2 + mt_rand(-5, 6);

    imagestring($img, $font_size, $f_x, $f_y, $code[$i], $font_color);  //输出验证码
}

imagepng($img);
imagedestroy($img);
Copy after login

With the above code, the verification code will be output to a random position on the canvas. If we want to fix the verification code at a specified location, we only need to modify the values ​​of $f_x and $f_y according to the design requirements.

For example, if we fix the verification code at the center of the canvas, we only need to modify the values ​​of $f_x and $f_y as follows:

$f_x = ($width / 2 - (($font_size + 3) * $code_len) / 2) + ($font_size + 3) * $i;  // 控制水平方向位置
$f_y = $height / 2 + mt_rand(-5, 6);  // 控制竖直方向位置
Copy after login

After running, the verification code will appear in the center of the canvas.

  1. Place the verification code within the specified HTML element

If we want to place the verification code within the specified HTML element, we can control the position of the canvas through CSS and place Canvases are embedded into HTML elements.

For example, if we want to place the verification code inside the <div id="verify"> element, we can add the following code in CSS: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">#verify {     position: relative;     background-color: #fff;  /* 图片背景色 */     width: 100px;     height: 32px;     text-align: center; } #verify img {     position: absolute;  /* 设置验证码图片位置 */     top: 0;     left: 0; }</pre><div class="contentsignin">Copy after login</div></div><p>Then Add the following code in HTML: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;div id=&quot;verify&quot;&gt;     &lt;img src=&quot;verify.php&quot; alt=&quot;验证码&quot;&gt; &lt;/div&gt;</pre><div class="contentsignin">Copy after login</div></div><p>Through the above code, we can embed the generated verification code into the specified element, and its position will be controlled by CSS. </p><ol start="3"><li>Place the verification code on the background</li></ol><p>In some cases, we may need to place the verification code on the background. For example, the background is a large picture, we The verification code needs to be placed in the corner of the screen. </p><p>To implement this function, we can first generate a background image without a verification code, and then draw the verification code onto the background. </p><p>The following is a basic implementation: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;?php session_start(); header(&quot;Content-type: image/png&quot;); $width = 300; $height = 200; $img = imagecreate($width, $height); imagecolorallocate($img, 255, 255, 255);  //设置背景颜色 // 生成不带验证码的背景图 $bg_img = imagecreatefromjpeg('bg.jpg'); imagecopy($img, $bg_img, 0, 0, 0, 0, $width, $height); $code_len = 4; $charset = 'abcdefghkmnprstuvwxyABCDEFGHKMNPRSTUVWXY3456789'; $code = ''; for ($i = 0; $i &lt; $code_len; $i++) {     $code .= $charset[mt_rand(0, strlen($charset) - 1)]; } $_SESSION['verify_code'] = strtoupper($code); // 将验证码写入背景图中 $font_color = imagecolorallocate($img, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); for ($i = 0; $i &lt; $code_len; $i++) {     $font_size = mt_rand(12, 16);     $angle = mt_rand(-10, 10);     $f_x = mt_rand(0, $width - ($font_size + 3));     $f_y = mt_rand(0, $height - $font_size);     imagestring($img, $font_size, $f_x, $f_y, $code[$i], $font_color);  //输出验证码 } imagepng($img); imagedestroy($img);</pre><div class="contentsignin">Copy after login</div></div><p>In the above code, we first load the original background image <code>bg.jpg, and then copy it to the new canvas , then generate the verification code and insert it into the canvas. (Please note that the verification code generated in this example is in a random position. In order to achieve our purpose, some modifications need to be made)

  1. Summary

Introduction to this article There are three methods to change the position of the verification code. The first is based on the verification code generated by PHP and is implemented by modifying the output position. The second is to embed the verification code canvas into the HTML element and control the position through CSS. The third is to embed the verification code canvas into the HTML element and control the position through CSS. The first is to add the verification code to the background so that the position of the verification code is consistent with the background. Different implementation methods are suitable for different scenarios, and you can choose the best solution according to your actual needs.

The above is the detailed content of How to change the position of verification code in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. Mar 25, 2025 am 10:37 AM

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

PHP Secure File Uploads: Preventing file-related vulnerabilities. PHP Secure File Uploads: Preventing file-related vulnerabilities. Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP Encryption: Symmetric vs. asymmetric encryption. PHP Encryption: Symmetric vs. asymmetric encryption. Mar 25, 2025 pm 03:12 PM

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

What is the purpose of prepared statements in PHP? What is the purpose of prepared statements in PHP? Mar 20, 2025 pm 04:47 PM

Prepared statements in PHP enhance database security and efficiency by preventing SQL injection and improving query performance through compilation and reuse.Character count: 159

How do you retrieve data from a database using PHP? How do you retrieve data from a database using PHP? Mar 20, 2025 pm 04:57 PM

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159

PHP API Rate Limiting: Implementation strategies. PHP API Rate Limiting: Implementation strategies. Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Authentication & Authorization: Secure implementation. PHP Authentication & Authorization: Secure implementation. Mar 25, 2025 pm 03:06 PM

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

See all articles