由于回答长度限制,我将提供一个简化版的PHP印刷网站源码示例,这个示例将包括一个简单的用户注册和登录系统,以及一个用于显示印刷产品列表的页面,这只是一个基本的示例,实际的网站可能需要更多的功能和安全性考虑。
<?php // config.php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "printing_db"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn>connect_error) { die("Connection failed: " . $conn>connect_error); } // register.php if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = $_POST["username"]; $password = $_POST["password"]; $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')"; if ($conn>query($sql) === TRUE) { echo "Registration successful!"; } else { echo "Error: " . $sql . "<br>" . $conn>error; } } // login.php if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = $_POST["username"]; $password = $_POST["password"]; $sql = "SELECT id FROM users WHERE username='$username' AND password='$password'"; $result = $conn>query($sql); if ($result>num_rows > 0) { session_start(); $_SESSION["loggedin"] = true; $_SESSION["username"] = $username; header("location: products.php"); } else { echo "Invalid username or password."; } } // products.php if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) { header("location: login.php"); exit; } $sql = "SELECT * FROM products"; $result = $conn>query($sql); ?> <!DOCTYPE html> <html> <head> <title>Printing Products</title> </head> <body> <h1>Welcome to our Printing Website!</h1> <table> <tr> <th>Product Name</th> <th>Description</th> <th>Price</th> </tr> <?php while($row = $result>fetch_assoc()): ?> <tr> <td><?php echo $row["product_name"]; ?></td> <td><?php echo $row["description"]; ?></td> <td><?php echo $row["price"]; ?></td> </tr> <?php endwhile; ?> </table> </body> </html>
FAQs:
Q1: 如何保护用户密码?
A1: 在实际应用中,您应该使用密码哈希函数(如password_hash()
)来存储用户密码,而不是直接存储明文密码,当验证用户登录时,您可以使用password_verify()
函数来检查提交的密码是否与存储的哈希值匹配,这样可以增加安全性,防止数据库泄露导致密码暴露。
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/743446.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复