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: <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>
<p>Then Add the following code in HTML: </p>
<pre class="brush:php;toolbar:false"><div>
<img src="/static/imghwm/default1.png" data-src="verify.php" class="lazy" alt="How to change the position of verification code in PHP" >
</div></pre>
<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>
<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(&#39;bg.jpg&#39;);
imagecopy($img, $bg_img, 0, 0, 0, 0, $width, $height);
$code_len = 4;
$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);
// 将验证码写入背景图中
$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><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!

This article explores efficient PHP array deduplication. It compares built-in functions like array_unique() with custom hashmap approaches, highlighting performance trade-offs based on array size and data type. The optimal method depends on profili

This article analyzes PHP array deduplication, highlighting performance bottlenecks of naive approaches (O(n²)). It explores efficient alternatives using array_unique() with custom functions, SplObjectStorage, and HashSet implementations, achieving

This article explores PHP array deduplication using key uniqueness. While not a direct duplicate removal method, leveraging key uniqueness allows for creating a new array with unique values by mapping values to keys, overwriting duplicates. This ap

This article details implementing message queues in PHP using RabbitMQ and Redis. It compares their architectures (AMQP vs. in-memory), features, and reliability mechanisms (confirmations, transactions, persistence). Best practices for design, error

This article examines current PHP coding standards and best practices, focusing on PSR recommendations (PSR-1, PSR-2, PSR-4, PSR-12). It emphasizes improving code readability and maintainability through consistent styling, meaningful naming, and eff

This article explores optimizing PHP array deduplication for large datasets. It examines techniques like array_unique(), array_flip(), SplObjectStorage, and pre-sorting, comparing their efficiency. For massive datasets, it suggests chunking, datab

This article details installing and troubleshooting PHP extensions, focusing on PECL. It covers installation steps (finding, downloading/compiling, enabling, restarting the server), troubleshooting techniques (checking logs, verifying installation,

This article explains PHP's Reflection API, enabling runtime inspection and manipulation of classes, methods, and properties. It details common use cases (documentation generation, ORMs, dependency injection) and cautions against performance overhea


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
