我需要制作PHP论坛的源代码
时间: 2023-06-11 14:05:55 浏览: 50
制作PHP论坛的源代码需要具备PHP编程语言的基本知识和Web开发技能。以下是一个简单的PHP论坛的源代码:
index.php
```php
<?php
session_start();
include('config.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP论坛</title>
</head>
<body>
<h1>PHP论坛</h1>
<?php if(isset($_SESSION['username'])) { ?>
<p>欢迎回来,<?php echo $_SESSION['username']; ?>!</p>
<a href="logout.php">退出登录</a>
<?php } else { ?>
<a href="login.php">登录</a>
<a href="register.php">注册</a>
<?php } ?>
<hr>
<h2>帖子列表</h2>
<?php
$query = "SELECT * FROM posts ORDER BY created_at DESC";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<h3>" . $row['title'] . "</h3>";
echo "<p>" . $row['content'] . "</p>";
echo "<p>作者:" . $row['author'] . "</p>";
echo "<p>发布时间:" . $row['created_at'] . "</p>";
echo "<hr>";
}
} else {
echo "暂无帖子!";
}
?>
<hr>
<h2>发表帖子</h2>
<?php if(isset($_SESSION['username'])) { ?>
<form method="post" action="add_post.php">
<label>标题:</label>
<input type="text" name="title">
<br>
<label>内容:</label>
<textarea name="content"></textarea>
<br>
<input type="submit" name="submit" value="发布帖子">
</form>
<?php } else { ?>
<p>请先登录才能发表帖子!</p>
<?php } ?>
</body>
</html>
```
config.php
```php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "forum";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn) {
die("连接失败:" . mysqli_connect_error());
}
?>
```
add_post.php
```php
<?php
session_start();
include('config.php');
if(isset($_POST['submit'])) {
$title = $_POST['title'];
$content = $_POST['content'];
$author = $_SESSION['username'];
$query = "INSERT INTO posts (title, content, author) VALUES ('$title', '$content', '$author')";
if(mysqli_query($conn, $query)) {
header("Location: index.php");
} else {
echo "发布帖子失败:" . mysqli_error($conn);
}
}
?>
```
login.php
```php
<?php
session_start();
include('config.php');
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) == 1) {
$_SESSION['username'] = $username;
header("Location: index.php");
} else {
echo "登录失败!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>用户名:</label>
<input type="text" name="username">
<br>
<label>密码:</label>
<input type="password" name="password">
<br>
<input type="submit" name="submit" value="登录">
</form>
</body>
</html>
```
register.php
```php
<?php
session_start();
include('config.php');
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$query = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if(mysqli_query($conn, $query)) {
$_SESSION['username'] = $username;
header("Location: index.php");
} else {
echo "注册失败!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>注册</title>
</head>
<body>
<h1>注册</h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>用户名:</label>
<input type="text" name="username">
<br>
<label>密码:</label>
<input type="password" name="password">
<br>
<input type="submit" name="submit" value="注册">
</form>
</body>
</html>
```
上述代码实现了一个简单的PHP论坛,包括登录、注册、发帖、查看帖子等基本功能。需要注意的是,这只是一个简单的示例,实际的论坛需要更完整的功能和更严密的安全措施。