写一个网站,在首页能看用户的留言,同时能实现包括用户注册、登录、注销、上传头像、修改个性签名等。
<html>
<head>
<meta charset = "utf-8">
<title>留言论坛title>
head>
<body>
<h1>综合实验:留言论坛h1>
";
echo "个人中心 ";
echo "注销";
}else{
echo "注册 ";
echo "登录";
}
?>
<hr />
<p>!此处用作留言板!p>
body>
html>
$dbHost = "127.0.0.1";
$dbUser = "root";
$dbPass = "root";
$dbName = "my_test";
if (!$link = @mysqli_connect($dbHost,$dbUser,$dbPass,$dbName)){
die(mysqli_connect_error());
}
mysqli_set_charset($link,"utf-8");
?>
include "../inc/dblink.inc.php";
?>
<html>
<head>
<meta charset = "utf-8">
<title>留言论坛</title>
</head>
<body>
<h1>个人中心</h1>
<?php
if (isset($_COOKIE['name'])){
$username = $_COOKIE['name'];
$sql = "select * from users where name ='".$username."'";
if ($results = mysqli_query($link,$sql)){
if (mysqli_num_rows($results)){
$result = mysqli_fetch_assoc($results);
//var_dump($result);
echo "
";
echo "欢迎来到个人中心,".$_COOKIE['name']." 返回首页 注销";
echo "
";
echo "个人信息:
";
echo "账号名:".$result['name']."
";
//echo $result['photo'];
echo "头像:]."'> 更新头像
";
echo "个性签名:".$result['signature']." 更新签名
";
}else{
echo "用户不存在!请注册";
}
}else{
echo "数据获取失败!请重新登录
";
die (mysqli_error());
}
}else{
echo "注册 ";
echo ">登录</a>";
}
?>
<?php
mysqli_close($link);
?>
</body>
</html>
<html>
<head>
<meta charset = "utf-8">
<title>留言论坛title>
head>
<body>
<h1>综合实验:留言论坛h1>
<form action = "./addUser.php" method = "POST">
用户名:<input type = "text" name = "userName"><br>
密码:<input type = "password" name = "userPass1"><br>
确认密码:<input type = "password" name = "userPass2"><br>
<input type = "submit" name = "userSubmit" value = "注册">
form>
<hr />
body>
html>
<meta charset = "utf-8">
<?php
include "../inc/dblink.inc.php";
?>
<?php
//var_dump($_POST);
echo "
";
if (isset($_POST['userSubmit'])){
if (is_null($_POST['userName']) or is_null($_POST['userPass1']) or is_null($_POST['userPass2'])){
echo "账号密码不能为空,请重新注册";
}else if ($_POST['userPass1'] !== $_POST['userPass2']){
echo "两次密码不一致,请重新注册";
}else {
// 查询数据库中是否有此用户,若无则新增,若有则要求重新输入
$sql = "select * from users where name = '".$_POST['userName']."'";
if ($results = mysqli_query($link,$sql)){
if (!mysqli_num_rows($results)){//为空则新增
$sql = "insert into users (name,password) values ('".$_POST['userName']."','".md5($_POST['userPass1'])."')";
//echo $sql."
";
if (mysqli_query($link,$sql)){
echo "注册成功,请登录";
}else{
echo "注册失败!请再次注册";
}
}else{
echo "该用户已存在,请重新注册";
}
}else{
echo "数据获取失败!";
die (mysqli_error());
}
}
}else{
header ("Location:./register.php");
}
?>
<?php
mysqli_close($link);
?>
<meta charset = "utf-8">
<?php
include "../inc/dblink.inc.php";
?>
<?php
if (isset($_POST['userSubmit'])){
//var_dump($_POST);
$sql = "select * from users where name = '".$_POST['userName']."' and password ='".md5($_POST['userPass'])."'";
//echo $sql;
if ($results = mysqli_query($link,$sql)){
if (mysqli_num_rows($results) > 0){
setcookie ('name',$_POST['userName'],time()+3600,"/");
echo "登录成功,请返回首页或个人中心";
}else{
echo "账号或密码错误,请重新输入";
}
}else{
die(mysqli_error($link));
}
}else{
echo "请输入账号密码";
// $html=<<
//
// 用户名:
// 密码:
//
//
// HTML;
// echo $html;
}
?>
<html>
<form method = "post" target = "_blank">
用户名:<input type = "text" name = "userName"><br>
密码:<input type = "password" name = "userPass"><br>
<input type="submit" name="userSubmit" value="登录">
</form>
</html>
<?php
mysqli_close($link);
?>
if (setcookie('name',$_COOKIE['name'],time()-3600,"/")){
echo "logout!";
header ("Location:../index.php");
}else{
die ("Error!");
}
?>
<meta charset = "utf-8">
<?php
include "../inc/dblink.inc.php";
?>
<?php
if (isset($_POST['userSubmit'])){
@$userName = $_COOKIE['name'];
//var_dump ($_FILES);
$tmp_path = $_FILES['up']["tmp_name"];
//echo $tmp_path;
$path = "./images/".$_FILES['up']["name"];
if (move_uploaded_file($tmp_path,$path)){
$path = mysqli_real_escape_string($link,$path);
$sql = "update users set photo = '".$path."' where name = '".$userName."'";
echo $sql;
if (mysqli_query($link,$sql)){
echo "头像上传成功,请返回个人中心";
}else{
die (mysqli_error($link));
}
}else{
echo "头像上传失败";
}
}else{
echo "请选择上传头像";
}
?>
<html>
<form method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "up"><br>
<input type = "submit" name = "userSubmit" value = "提交">
</form>
</html>
<?php
mysqli_close($link);
?>
<meta charset = "utf-8">
<?php
include "../inc/dblink.inc.php";
?>
<?php
if (isset($_POST['userSubmit'])){
@$userName = $_COOKIE['name'];
$sql = "update users set signature = '".$_POST['userSignature']."' where name = '".$userName."'";
if (mysqli_query($link,$sql)){
echo "签名更新成功,请返回个人中心";
}else{
die (mysqli_error($link));
}
}else{
echo "请更新签名";
}
?>
<html>
<form method = "POST" >
输入个性签名:<input type = "text" name = "userSignature"><br>
<input type = "submit" name = "userSubmit" value = "提交">
</form>
</html>
<?php
mysqli_close($link);
?>
include "./inc/dblink.inc.php";
?>
<html>
<head>
<meta charset = "utf-8">
<title>留言论坛</title>
</head>
<body>
<h1>综合实验:留言论坛</h1>
<?php
if (isset($_COOKIE['name'])){
echo "欢迎来到论坛,".$_COOKIE['name']."
";
echo "个人中心 ";
echo "注销 " ;
echo "我要留言" ;
}else{
echo "注册 ";
echo "登录";
}
echo "
";
$sql = "select * from messages";
if ($results = mysqli_query($link,$sql)){
if (mysqli_num_rows($results)>0){
echo "";
echo "ID TITLE AUTHOR ";
while($result = mysqli_fetch_assoc($results)){
echo $result['id'];
echo "
".$result['id']."
]}' target='_blank'>".$result['title']."
".$result['uname']."
";
}
echo "
";
}else{
echo "暂无留言";
}
}else{
mysqli_error($link);
}
?>
</body>
</html>
<?php
mysqli_close($link);
?>
include "../inc/dblink.inc.php";
?>
<html>
<head>
<meta charset = "utf-8">
<title>留言论坛</title>
</head>
<body>
<h1>留言内容</h1><a href = '../index.php'>返回首页</a><hr />
<?php
if(isset($_GET['id'])){
$id = $_GET['id'];
//echo $id;
$sql = "select * from messages where id =".$id;
//echo $sql;
if ($results = mysqli_query($link,$sql)){
$result = mysqli_fetch_assoc($results);
echo "作者:".$result['uname']."
标题:".$result['title']."
";
echo $result['content'];
}else{
mysqli_error($link);
}
}else{
echo "ID error!";
}
?>
</body>
</html>
<?php
mysqli_close($link);
?>
<meta charset = "utf-8">
<?php
include "../inc/dblink.inc.php";
?>
<?php
if (isset($_COOKIE['name'])){
//echo "已登录";
if (isset($_POST['userSubmit']) && isset($_POST['userTitle'])){
$userName = $_COOKIE['name'];
$title = mysqli_real_escape_string($link,$_POST['userTitle']);
$cont = mysqli_real_escape_string($link,$_POST['userCont']);
$sql = "insert into messages (uname,title,content) values ('".$userName."','".$title."','".$cont."')";
//echo $sql;
if(mysqli_query($link,$sql)){
echo"留言成功,返回首页";
}else{
echo "留言失败";
}
}else{
echo "标题不能为空,请点击提交按钮进行提交。";
}
}else{
echo "您还未登录,请登录后留言。";
}
?>
<html>
<form method = "POST">
标题:<input type = "text" name = "userTitle"><br>
内容:<br>
<textarea name = "userCont"></textarea>
<input type = "submit" name = "userSubmit" value = "提交">
</form>
</html>
<?php
mysqli_close($link);
?>