Home > Article > Backend Development > How to build a secure electronic payment system using PHP and Vue.js
How to build a secure electronic payment system using PHP and Vue.js
In recent years, with the rapid development of the Internet, electronic payment has become an indispensable part of people's lives. How to build a safe and reliable electronic payment system has become an important issue faced by developers. This article will introduce how to use PHP and Vue.js to build a secure electronic payment system, and attach relevant code examples.
CREATE TABLE `users` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(50) NOT NULL, `password` VARCHAR(50) NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `payments` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `user_id` INT(11) NOT NULL, `amount` FLOAT NOT NULL, `timestamp` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), FOREIGN KEY (`user_id`) REFERENCES users(`id`) );
// login.php <?php session_start(); $username = $_POST['username']; $password = $_POST['password']; // 验证用户名和密码是否正确 // ... // 验证成功,设置session $_SESSION['username'] = $username; // 返回登录成功信息给前端 echo json_encode(["status" => "success"]); ?> // register.php <?php $username = $_POST['username']; $password = $_POST['password']; // 验证用户名是否已存在 // ... // 插入用户信息到数据库 // ... // 返回注册成功信息给前端 echo json_encode(["status" => "success"]); ?>
// pay.php <?php session_start(); $amount = $_POST['amount']; // 向第三方支付接口发送请求 // ... // 支付成功,将支付信息插入数据库 // ... // 返回支付成功信息给前端 echo json_encode(["status" => "success"]); ?> // payment-history.php <?php session_start(); $username = $_SESSION['username']; // 根据用户名查询支付历史记录 // ... // 返回支付历史记录给前端 echo json_encode($paymentHistory); ?>
// login.php <?php session_start(); $username = $_POST['username']; $password = $_POST['password']; // 对密码进行加密 $hashedPassword = password_hash($password, PASSWORD_DEFAULT); // 防止SQL注入攻击,使用预处理语句 $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?"); $stmt->execute([$username]); $user = $stmt->fetch(); // 验证密码是否正确 if (password_verify($password, $user['password'])) { // 验证成功,设置session $_SESSION['username'] = $username; // 返回登录成功信息给前端 echo json_encode(["status" => "success"]); } else { // 验证失败,返回错误信息给前端 echo json_encode(["status" => "error", "message" => "Invalid username or password"]); } ?>
Through the above four steps, we can use PHP and Vue.js to build a safe and reliable Electronic payment system. Of course, this is just a simple example, and more security and performance optimizations are needed in actual projects. Hope this article helps you!
The above is the detailed content of How to build a secure electronic payment system using PHP and Vue.js. For more information, please follow other related articles on the PHP Chinese website!