我可以为你提供一个详细的PHP社交网站源码示例,这个示例将包括用户注册、登录、发布动态和评论功能,为了简洁起见,我将代码分成几个部分进行解释。
数据库设置
我们需要创建一个MySQL数据库来存储用户信息和动态内容。
CREATE DATABASE social_network; USE social_network; CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, email VARCHAR(100) NOT NULL UNIQUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE posts ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, content TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ); CREATE TABLE comments ( id INT AUTO_INCREMENT PRIMARY KEY, post_id INT NOT NULL, user_id INT NOT NULL, content TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (post_id) REFERENCES posts(id), FOREIGN KEY (user_id) REFERENCES users(id) );
配置文件 `config.php`
创建一个配置文件来存储数据库连接信息。
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "social_network"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接 if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
3. 用户注册register.php
创建一个用户注册表单并处理注册请求。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Register</title> </head> <body> <h3>Register</h3> <form action="register.php" method="post"> Username: <input type="text" name="username" required><br> Email: <input type="email" name="email" required><br> Password: <input type="password" name="password" required><br> <button type="submit">Register</button> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { include 'config.php'; $username = $_POST['username']; $email = $_POST['email']; $password = password_hash($_POST['password'], PASSWORD_BCRYPT); $stmt = $conn->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)"); $stmt->bind_param("sss", $username, $email, $password); if ($stmt->execute()) { echo "Registration successful!"; } else { echo "Error: " . $stmt->error; } $stmt->close(); $conn->close(); } ?> </body> </html>
用户登录 `login.php`
创建一个用户登录表单并处理登录请求。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <h3>Login</h3> <form action="login.php" method="post"> Username: <input type="text" name="username" required><br> Password: <input type="password" name="password" required><br> <button type="submit">Login</button> </form> <?php session_start(); if ($_SERVER["REQUEST_METHOD"] == "POST") { include 'config.php'; $username = $_POST['username']; $password = $_POST['password']; $stmt = $conn->prepare("SELECT id, password FROM users WHERE username = ?"); $stmt->bind_param("s", $username); $stmt->execute(); $stmt->store_result(); if ($stmt->num_rows > 0) { $stmt->bind_result($id, $hashed_password); $stmt->fetch(); if (password_verify($password, $hashed_password)) { $_SESSION['user_id'] = $id; header("Location: home.php"); exit(); } else { echo "Invalid password."; } } else { echo "No user found with that username."; } $stmt->close(); $conn->close(); } ?> </body> </html>
主页 `home.php`
显示用户的动态和评论。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h3>Welcome to the Social Network</h3> <?php session_start(); if (!isset($_SESSION['user_id'])) { header("Location: login.php"); exit(); } include 'config.php'; $user_id = $_SESSION['user_id']; $stmt = $conn->prepare("SELECT * FROM posts ORDER BY created_at DESC"); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { echo "<div>"; echo "<p><strong>User ID:</strong> " . $row['user_id'] . "</p>"; echo "<p><strong>Content:</strong> " . $row['content'] . "</p>"; echo "<p><strong>Posted on:</strong> " . $row['created_at'] . "</p>"; echo "</div><hr>"; } $stmt->close(); $conn->close(); ?> <a href="logout.php">Logout</a> </body> </html>
注销 `logout.php`
处理用户注销请求。
<?php session_start(); session_destroy(); header("Location: login.php"); exit(); ?>
发布动态 `post.php`
允许用户发布动态。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Post</title> </head> <body> <h3>Create a Post</h3> <form action="post.php" method="post"> Content: <textarea name="content" required></textarea><br> <button type="submit">Post</button> </form> <?php session_start(); if (!isset($_SESSION['user_id'])) { header("Location: login.php"); exit(); } if ($_SERVER["REQUEST_METHOD"] == "POST") { include 'config.php'; $user_id = $_SESSION['user_id']; $content = $_POST['content']; $stmt = $conn->prepare("INSERT INTO posts (user_id, content) VALUES (?, ?)"); $stmt->bind_param("is", $user_id, $content); if ($stmt->execute()) { header("Location: home.php"); exit(); } else { echo "Error: " . $stmt->error; } $stmt->close(); $conn->close(); } ?> </body> </html>
8. 评论功能comment.php
(可选)
允许用户对动态发表评论,这里只提供一个简单的实现思路,你可以根据需要扩展。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Comment</title> </head> <body> <h3>Leave a Comment</h3> <form action="comment.php" method="post"> Post ID: <input type="number" name="post_id" required><br> Content: <textarea name="content" required></textarea><br> <button type="submit">Comment</button> </form> <?php session_start(); if (!isset($_SESSION['user_id'])) { header("Location: login.php"); exit(); } if ($_SERVER["REQUEST_METHOD"] == "POST") { include 'config.php'; $user_id = $_SESSION['user_id']; $post_id = $_POST['post_id']; $content = $_POST['content']; $stmt = $conn->prepare("INSERT INTO comments (post_id, user_id, content) VALUES (?, ?, ?)"); $stmt->bind_param("iis", $post_id, $user_id, $content); if ($stmt->execute()) { header("Location: home.php"); exit(); } else { echo "Error: " . $stmt->error; } $stmt->close(); $conn->close(); } ?> </body> </html>
是一个基本的社交网站源码示例,涵盖了用户注册、登录、发布动态和评论等功能,你可以根据实际需求进一步扩展和完善这些功能。
小伙伴们,上文介绍php社交网站源码_PHP的内容,你了解清楚吗?希望对你有所帮助,任何问题可以给我留言,让我们下期再见吧。
本文来源于互联网,如若侵权,请联系管理员删除,本文链接:https://www.9969.net/89710.html