How to change the position of verification code in PHP
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.
- 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 = 'abcdefghkmnprstuvwxyABCDEFGHKMNPRSTUVWXY3456789'; $code = ''; for ($i = 0; $i < $code_len; $i++) { $code .= $charset[mt_rand(0, strlen($charset) - 1)]; } $_SESSION['verify_code'] = 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);
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); // 控制竖直方向位置
After running, the verification code will appear in the center of the canvas.
- 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"><div id="verify">
<img src="verify.php" alt="验证码">
</div></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"><?php
session_start();
header("Content-type: image/png");
$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 < $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 < $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)
- 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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.

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.

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.

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

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

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

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