Home > Article > Backend Development > Second-hand recycling website developed using PHP supports user authentication
Second-hand recycling website developed using PHP supports user authentication
With people's increasing attention to environmental protection, second-hand recycling has become a popular way of life. In order to meet people's needs, many second-hand recycling websites have emerged. This article will introduce how to use PHP to develop a second-hand recycling website that supports user authentication and provide code examples.
First, we need to create a login system with user authentication functionality. In this way, users can use the second-hand recycling website by registering and logging in. The following is a simple user login page sample code:
<?php // 用户登录页面 session_start(); // 检查用户是否已登录,如果已登录则跳转到主页 if(isset($_SESSION['user_id'])){ header("Location: home.php"); exit; } // 检查用户提交的登录表单 if($_SERVER['REQUEST_METHOD'] == 'POST'){ // 获取用户提交的用户名和密码 $username = $_POST['username']; $password = $_POST['password']; // 进行身份验证操作, 这里省略实际的验证步骤 // 如果身份验证通过则将用户信息保存到SESSION中,并跳转到主页 $_SESSION['user_id'] = $user_id; header("Location: home.php"); exit; } ?> <!DOCTYPE html> <html> <head> <title>用户登录</title> </head> <body> <h2>用户登录</h2> <form method="POST" action=""> <input type="text" name="username" placeholder="用户名" required /><br/> <input type="password" name="password" placeholder="密码" required /><br/> <input type="submit" value="登录" /> </form> <p>还没有账号?<a href="register.php">点击此处进行注册</a></p> </body> </html>
In the above code, we use $_SESSION
to save the user login status. Users log in by filling in their username and password and submitting the form. We have omitted the actual authentication step, and you can perform the authentication operation according to your actual needs.
Next, we need to create a user registration page. The following is a simple user registration page sample code:
<?php // 用户注册页面 session_start(); // 检查用户是否已登录,如果已登录则跳转到主页 if(isset($_SESSION['user_id'])){ header("Location: home.php"); exit; } // 检查用户提交的注册表单 if($_SERVER['REQUEST_METHOD'] == 'POST'){ // 获取用户提交的用户名和密码 $username = $_POST['username']; $password = $_POST['password']; // 进行注册操作, 这里省略实际的注册步骤 // 注册成功后跳转到登录页面 header("Location: login.php"); exit; } ?> <!DOCTYPE html> <html> <head> <title>用户注册</title> </head> <body> <h2>用户注册</h2> <form method="POST" action=""> <input type="text" name="username" placeholder="用户名" required /><br/> <input type="password" name="password" placeholder="密码" required /><br/> <input type="submit" value="注册" /> </form> <p>已经有账号?<a href="login.php">点击此处进行登录</a></p> </body> </html>
In the above code, the user registers by filling in the username and password and submitting the registration form. We have omitted the actual registration steps, you can perform registration operations according to actual needs.
With the user authentication function, we can control access on other pages of the second-hand recycling website. For example, only logged-in users can post second-hand item information. The following is a simple sample code for publishing an item page:
<?php // 发布物品页面 session_start(); // 检查用户是否已登录,如果未登录则跳转到登录页面 if(!isset($_SESSION['user_id'])){ header("Location: login.php"); exit; } // 处理用户提交的发布物品表单 if($_SERVER['REQUEST_METHOD'] == 'POST'){ // 处理发布物品信息,这里省略实际的处理步骤 } ?> <!DOCTYPE html> <html> <head> <title>发布物品</title> </head> <body> <h2>发布物品</h2> <form method="POST" action=""> <!-- 物品信息字段,省略 --> <input type="submit" value="发布" /> </form> </body> </html>
In the above code, we check whether the user is logged in, and if not, jump to the login page. This way users must authenticate before accessing the publish item page.
Through the above sample code, we have implemented a second-hand recycling website that supports user authentication. Users can use various services of the website through the registration and login functions, which also improves the security and user experience of the website. Of course, the above code is just an example, and you can adjust and expand it according to actual needs and business logic.
The above is the detailed content of Second-hand recycling website developed using PHP supports user authentication. For more information, please follow other related articles on the PHP Chinese website!