How to implement login registration with verification code in PHP

PHPz
Release: 2023-03-29 13:55:05
Original
938 people have browsed it

PHP is a programming language widely used in website development. It is very convenient, efficient and flexible. In PHP development, user login and registration functions are often involved, and in order to achieve security, a verification code verification mechanism needs to be added. This article will introduce how to use PHP to implement the login registration function with verification code.

1. Login function implementation

1.1 Create a login page

We first need to create a login page for users, providing them with a framework for entering their account number and password, as well as a verification code Input box. You can add the following code to the code:

<html>
<head>
    <title>登陆页面</title>
    <meta charset="utf-8">
</head>
<body>
<form action="login.php" method="POST">
    <label>账号: <input type="text" name="username"></label><br>
    <label>密码: <input type="password" name="password"></label><br>
    <label>验证码: <input type="text" name="captcha"></label>
    <img src="captcha.php" alt="验证码"><br>
    <input type="submit" name="submit" value="登陆">
</form>
</body>
</html>
Copy after login

1.2 Create verification code

In order to increase the security of login, we need to use the verification code to confirm the user's identity, so we need to create a verification code picture. You can add the following code to the code:

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

//定义画布的宽和高
$width = 60;
$height = 25;

//生成验证码并保存到Session
$captcha_num = &#39;&#39;;
for ($i = 0; $i < 4; ++$i) {
    $captcha_num .= rand(0, 9);
}
$_SESSION[&#39;captcha&#39;] = $captcha_num;

//创建画布
$img = imagecreate($width, $height);

//设置画布颜色
$bg_color = imagecolorallocate($img, 255, 255, 255);

//生成干扰线
for ($i = 0; $i < 5; ++$i) {
    $line_color = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
    imageline($img, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $line_color);
}

//生成干扰点
for ($i = 0; $i < 100; ++$i) {
    $pixel_color = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($img, rand(0, $width), rand(0, $height), $pixel_color);
}

//设置验证码颜色和大小
$captcha_color = imagecolorallocate($img, 0, 0, 0);
$captcha_size = 20;

//将验证码画到画布上
imagestring($img, $captcha_size, 10, 5, $captcha_num, $captcha_color);

//输出画布
imagepng($img);

//释放资源
imagedestroy($img);
?>
Copy after login

The captcha.php file in this code is a separate file, which will generate a random verification code image and save the verification code to the Session.

1.3 Login verification

After the user enters the user information and verification code, we need to verify the input content. You can add the following code to the code:

<?php
session_start();
if (isset($_POST[&#39;submit&#39;])) {    //判断是否提交表单
    if ($_POST[&#39;captcha&#39;] == $_SESSION[&#39;captcha&#39;]) {    //判断验证码是否正确
        $username = $_POST[&#39;username&#39;];
        $password = $_POST[&#39;password&#39;];

        //查询数据库验证账号密码是否正确
        $mysql = new mysqli(&#39;localhost&#39;, &#39;root&#39;, &#39;password&#39;, &#39;database&#39;); //替换成自己的用户名、密码、数据库
        $result = $mysql->query("SELECT * FROM users WHERE username='$username' AND password='$password'");
        if ($result->num_rows == 1) {  //账号密码验证成功
            echo "登录成功!";
        } else {    //账号或密码错误
            echo "账号或密码错误!";
        }
        $mysql->close();
    } else {    //验证码错误
        echo "验证码错误!";
    }
}
?>
Copy after login

The login.php file in this code is a separate file used to process the form data submitted by the user, and then perform the corresponding operations after passing the verification.

2. Registration function implementation

2.1 Create a registration page

We need to create a registration page for users to provide them with a framework for entering their account number and password, as well as a verification code input frame. You can add the following code to the code:

<html>
<head>
    <title>注册页面</title>
    <meta charset="utf-8">
</head>
<body>
<form action="register.php" method="POST">
    <label>账号: <input type="text" name="username"></label><br>
    <label>密码: <input type="password" name="password"></label><br>
    <label>验证码: <input type="text" name="captcha"></label>
    <img src="captcha.php" alt="验证码"><br>
    <input type="submit" name="submit" value="注册">
</form>
</body>
</html>
Copy after login

2.2 Registration verification

After the user enters the user information and verification code, we need to verify the input content. You can add the following code to the code:

<?php
session_start();
if (isset($_POST[&#39;submit&#39;])) {    //判断是否提交表单
    if ($_POST[&#39;captcha&#39;] == $_SESSION[&#39;captcha&#39;]) {    //判断验证码是否正确
        $username = $_POST[&#39;username&#39;];
        $password = $_POST[&#39;password&#39;];

        //插入数据到数据库中
        $mysql = new mysqli(&#39;localhost&#39;, &#39;root&#39;, &#39;password&#39;, &#39;database&#39;); //替换成自己的用户名、密码、数据库
        $result = $mysql->query("INSERT INTO users(username, password) VALUES ('$username', '$password')");
        if ($result) {  //插入数据成功
            echo "注册成功!";
        } else {    //插入数据失败
            echo "注册失败!";
        }
        $mysql->close();
    } else {    //验证码错误
        echo "验证码错误!";
    }
}
?>
Copy after login

The register.php file in this code is a separate file used to process the form data submitted by the user, and then insert the data into the database after passing verification .

3. Summary

The above is all the code to use PHP to implement the login and registration function with verification code. Through this article, we can learn how to create verification codes and verify user login and registration information in PHP. The login and registration function with verification code can effectively improve the security of the website, prevent malicious attacks by robots, and protect users' private information.

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

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template