1、要完成留言板的登陆和注册页面就需要对HT5(html5),php,sql简单的应用。
- 在这个留言板小项目中我使用了session和cookie两个函数
首先我们要用html写出前段的登陆和注册界面,然后再用php进行传参,简单的过滤和mysql的交互。
-
这是我的登陆界面
- 下面是代码
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登陆</title>
</head>
<body background="3.jpg" >
<center>
<h1>登陆</h1>
<?php
include "my.php";
if (isset($_POST["submit"])){
if (empty($_POST["user"]) or empty($_POST["password"])) {
echo "<font color='red'> 用户名或者密码不能为空,请重新输入!</font>";
}else{
$user = $_POST["user"];
$password = $_POST["password"];
$sql = "select * from user where username = '$user' and password = '$password' ";
//echo $sql;
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0) {
//echo "<font color='green'>登陆成功!</font>";
$preson = mysqli_fetch_array($result);
$_SESSION['uid'] = $preson["uid"];
$_SESSION["username"] = $preson["username"];
#echo $_SESSION['uid'];
#echo $_SESSION['username'];
echo "<script>alert('登陆成功')</script>";
echo "<script>window.open('ly.php')</script>";
}else{
echo "<font color='red'>用户名或者密码不存在!</font>";
}
}
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" >
<table border="0" >
<tr >
<td size= "6" >用户名</td>
<td ><input type="text" name="user" style="width:250px;height:30px" ></td>
</tr>
<br />
<tr>
<td size="6">密码</td>
<td>
<input type="password" name="password" style="width:250px;height:30px"></td>
</tr>
<br/>
<tr>
<td colspan="2" align="center"><input type="submit" value="登陆" name="submit" style="width:100px;height:30px">
<input type="submit" name="zhuce" value="注册" onClick="window.open('test.php')" style="width:100px;height:30px">
<!--<input type="button" value="注册" onclick="document.location.href='http://127.0.0.1/5.php'" >-->
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
-
这里我用php进行了一个简单的过滤(用户名和密码不能为空,不然不能登陆)
-
没有填密码就报错
- 这里 my.php文件是链接数据库的文件
<meta charset="utf-8">
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$db_name = "shiyan";
#创建连接
$conn = new mysqli($servername,$username,$password,$db_name);
#检测连接
if (!$conn){
die("连接失败!".mysqli_error());
}else{
#echo "连接成功";
}
?>
- $db_name="shiyan";是我的数据库名
-
下面截图是我留言板用到的数据库和表
- 这是我创建user表的字段等;
CREATE table IF NOT EXISTS `user`(
`uid` INT AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(20) NOT NULL,
`email` VARCHAR(50) NOT NULL,
`sex` VARCHAR(10) NOT NULL,
`password` VARCHAR(32) NOT NULL,
`picture` VARCHAR(50) default '/image/default.jpg',
`time` DATE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
- 这是我创建massage表用到的字段
CREATE table IF NOT EXISTS `message`(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`content` VARCHAR(100) NOT NULL,
`uid` INT NOT NULL,
`ip` VARCHAR(20) NOT NULL,
`time` DATETIME,
FOREIGN KEY (uid) REFERENCES user(uid) on delete cascade on update no action
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
登陆页面要用的几条sql语句
判断user表是否存在你输入的user和password。存在说明可以登录不存在就需要注册了
select * from user where username = '$user' and password = '$password'