re" />

飞道的博客

[zkaq靶场]PHP简易论坛开发

208人阅读  评论(0)

PHP–简易论坛功能开发

1.论坛首页:index.php

提供注册和登录功能。

<!DOCTYPE html>
<html>
<head>
	<title>cat forum</title>
</head>
<body>
<h1>welcome to cat forum</h1>

welcome, please sign in.<br>

<a href="./register.php">register</a> <br>
<a href="./login.php">login</a> <br>

</body>
</html>

2.登录功能:login.php


输入账号密码即可登录,提交至当前页面

<meta charset="utf-8">

<?php 
	include"./db.php";
	$db = new mydb();
	$conn = $db->connect();

	if (isset($_POST["submit"])) {
		# code...
		$user_name = $_POST["user_name"];
		$user_password = $_POST["user_password"];
		$user_sql = "select user_name,user_password from users where user_name='{$user_name}' and user_password='{$user_password}'";
		$result = $conn->query($user_sql);
		if ($result->num_rows > 0) {
			echo "success! </br>";
			setcookie("user_name",$_POST["user_name"],time()+3600);
			echo "go home page <a href='./home.php'>go</a>";
		}
		else{
			echo "failed,please retry.<br>";
			echo "<a href='./login.php'>retry</a>";
		}

	}else{
		$html = <<<HTML
<form action="" method="post" >
	<h1>login page:</h1>

	user_name:<input type="text" name="user_name"><br>
	user_password:<input type="password" name="user_password"><br>
	<input type="submit" name="submit" value="submit">
</form>

HTML;
echo $html;
	}

  ?>

2.1 登录成功:导航至家页面:home.php

2.2 登录失败:导航至login.php重新登录

3.成功登录:家页面


home.php

<!DOCTYPE html>
<html>
<head>
	<title>home page</title>
	<meta charset="utf-8">
</head>
<body>
<h1>
	welcom to cat forum!
</h1>
<?php
include "./db.php"; 
$db = new mydb();
$conn = $db->connect();

if (isset($_COOKIE["user_name"])) {
	# code...
	echo "welcome , " .$_COOKIE["user_name"];
	echo "<br>";
	echo "<a href='./logout.php'>logout</a>";
	echo "|";
	echo "<a href='./submit_message.php'>我要留言</a>";

	$message_sql = "select * from message ";

	
	$results = $conn->query($message_sql);
	
	if ($results->num_rows > 0 ) {
		# code...
		echo "<table border =2 >";
		echo "<tr>
			<th>ID</th><th>AUTHOR</th><th>CONTENT</th><th>TIME</th>
			</tr>";
		while ($row =  $results->fetch_assoc()) {
			
			echo "<tr><td>{$row['id']}</td><td>{$row['author']}</td><td>{$row['content']}</td><td>{$row['time']}</td></tr>";
		}
		echo "</table>";
	}else{
		echo "no message.";
	}



}else{
	header("Location:./index.php");
}


 ?>

</body>
</html>

当前页面连接数据库,提取数据库中的留言,显示在当前页面。

3.1 采用Cookie机制显示当前登录用户名。

welcom, tom

3.2 提供登出与留言功能。

3.2.1 登出 logout.php

注销cookie,重新导航至首页:index.php

<!DOCTYPE html>
<html>
<head>
	<title>logout page</title>
</head>
<body>

<h1>cat forum</h1>
<?php 
if (setcookie("user_name",$_COOKIE["user_name"],time()-3601)) {
	# code...
	echo "logout!";
	// sleep(3);
	header("Location:./index.php");
}

 ?>
</body>
</html>

3.2.2 留言 subimit_message.php


首先判断当前是否登录,如果未登录,导航至登录页面。
然后将文本框里的内容提交至数据库。提交页面为当前页面。
留言成功则导航至家页面。
家页面则可以看到留言。

<meta charset="utf-8">
<h1>welcome to cat forum</h1>
<h2>欢迎留言</h2>
<?php 
if (isset($_COOKIE["user_name"])) {
	# code...
	echo "<h3><a href='./home.php'>{$_COOKIE["user_name"]}</a><h3>";
}else{
	echo "<a href='./index.php'>please login</a>";
}
 ?>

<?php
ini_set('date.timezone','Asia/Shanghai');

include "./db.php";

$db = new mydb();
$conn = $db->connect();


if (isset($_POST["message_submit"])) {
	
	if (isset($_POST["message_content"])) {
		# code...
		$message_content = $_POST["message_content"];
		$author = $_COOKIE["user_name"];
		$create_time = date('Y-m-d H:i');
		$submit_sql = "insert into message (author,content,time)values ('{$author}','{$message_content}','{$create_time}')";
		// echo $submit_sql;
		if ($conn->query($submit_sql)) {
			echo "留言成功。<br>";
			echo " <a href='./home.php'>返回首页</a>";
		}else{
			header("Location:./submit_message.php");
		}

	}
}else{
$htm = <<<HTML
<form action="" method="post">
	请留言:
	<br>
	<textarea name="message_content">
	</textarea>

	<input type="submit" name="message_submit" value="submit">
</form>

HTML;
echo $htm;
}

 ?>

4.注册功能

4.1 注册页面:register.html

<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
	<title>register page</title>

<script >
	function confirm_password(){
		var ps1 = document.getElementById('user_password1');
		var ps2 = document.getElementById('user_password2');
		if (ps1.value != ps2.value) {
				//document.getElementById('pass_alert').innerHTML = "两次密码不一致";
				alert("两次密码不一致,请重新输入。");
				ps1.value = "";
				ps2.value = "";
		}
	}
</script>

</head>

<body>
<h1>cat forum register</h1>
<form action="add_user.php" method="post">
	user_name:<input id="user_name" type="text" name="user_name"><br>
	password:<input id="user_password1" type="password" name="user_password1"><br>
	confirm password<input id="user_password2" type="password" name="user_password2">
	<p id="pass_alert"></p>
	<br>
	<input type="submit" name="submit" value="register" onmouseover="confirm_password()">
</form>

</body>
</html>

当两次输入的密码不一致时就会弹框提示。

注册成功之后会将数据提交至add_user.php。

4.2添加用户add_user.php

<meta charset="utf-8">
<?php 
include "./db.php";

$db = new mydb();
$conn = $db->connect();

if (isset($_POST["submit"])) {
	
	//var_dump($_POST);
	$user_name = $_POST["user_name"];
	$user_password1 = $_POST["user_password1"];
	$user_password2 = $_POST["user_password2"];
	
	if (isset($user_name)
        && isset($user_password1)
        && isset($user_password2)
        && $user_password1===$user_password2) {
		$sql = "insert into users (user_name, user_password) values ('{$user_name}', '{$user_password1}')";
		// echo $sql;
		// var_dump($conn);
		if ($conn->query($sql)) {
			echo "register success!<br><a href='./home.php'>return home page</a>";
			setcookie("user_name", $_POST["user_name"],time()+3600);
		}else{
			echo "register failed. <a href='./register.php'> return register.</a>";
		}

	}else{
		echo "返回重新注册。<a href='./register.php'>return register</a>";
	}
}else{
	header("Location:./register.php");
}
 ?>

注册成功则直接导航至家页面。注册失败需要返回注册页面重新注册。

5.数据库设计

5.1数据库连接:db.php

<?php 
/**
 * 
 */
class mydb 
{
	
	function __construct()
	{
		$this->server = "localhost";
		$this->dbuser = "root";
		$this->dbpassword = "root";
		$this->dbname = "forum";
	}
	function connect(){
		$conn = new mysqli($this->server, $this->dbuser, $this->dbpassword, $this->dbname);
		
		return $conn;
	}
}
 ?>

5.2数据库结构

总共有两张表:message,users
message表存储用户留言信息,users表存储用户名与密码


转载:https://blog.csdn.net/weixin_42633229/article/details/116544351
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场