数据库表的设计:
DROP TABLE IF EXISTS `user_s`;
CREATE TABLE `user_s` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`pwd` varchar(255) NOT NULL,
`email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
注册页面如图,依次添加控件如图所示。
<html>
<form name="myform" method="post">
<table align="center">
<tr>
<td><a href="index.php?">返回首页</a></td>
<td><a href="login.php?">登录</a></td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="Array[username]"></td>
</tr>
<tr>
<td align="right">密   码:</td>
<td><input type="password" name="Array[pwd]"></td>
</tr>
<tr>
<td align="right">E-mail:</td>
<td><input type="email" name="Array[email]"></td>
</tr>
<tr>
<td ><input type='submit' value='提交'></td>
</tr>
</form>
</table>
<html>
注册成功后将信息添加到数据库表user_s中:
<?php
session_start();
if ($_POST) {
$s = $_POST['Array'];
include_once ("conn.php");
if (empty ($s['username']) || empty ($s['pwd']) || empty ($s['email'])) {
echo "<script>alert('字段内容不能为空!');</script>";
} else {
$name = $s['username'];
$query = "select * from user_s where username = '$name'";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) != 0) {
echo "<script>alert('用户名已存在,请重新输入!');</script>";
} else {
$_SESSION['user'] = $name;
$pwd = $s['pwd'];
$email = $s['email'];
$query = "insert into user_s (username,pwd,email) values ('$name','$pwd','$email')";
$result = mysqli_query($link, $query);
if (!$result) {
exit ('写入数据库失败!' . mysqli_error($link));
} else {
echo "<script>alert('注册成功!');parent.location.href='index.php';</script>";
}
}
}
}
?>
登录页面如图:
<html>
<form name="myform" method="post">
<table align="center">
<tr>
<td><a href="index.php?">返回首页</a></td>
<td><a href="register.php?">注册</a></td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="Array[username]"></td>
</tr>
<tr>
<td align="right">密   码:</td>
<td><input type="password" name="Array[pwd]"></td>
</tr>
<tr>
<td ><input type='submit' value='登录'></td>
</tr>
</form>
</table>
<html>
登录查询数据库,信息比对成功后利用session存储用户名:
<?php
session_start();
if ($_POST) {
include_once ("conn.php");
$s = $_POST['Array'];
if (empty ($s['username']) || empty ($s['pwd'])) {
echo "<script>alert('字段内容不能为空!');</script>";
} else {
$name = $s['username'];
$query = "select * from user_s where username = '$name'";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) == 0) {
echo "<script>alert('用户不存在,请先进行注册!');</script>";
} else {
$pwd=$s['pwd'];
$res=mysqli_fetch_row($result);
$pass=$res[2];
if (strcmp($pwd,$pass)==0) {
$_SESSION['user'] = $name;
echo "<script>alert('登录成功!');parent.location.href='index.php';</script>";
} else {
echo "<script>alert('密码输入错误!');</script>";
}
}
}
}
?>
index.php页面用来取出session的用户名内容输出:
<?php
session_start();
if (isset($_SESSION['user'])) {
echo $_SESSION['user'] . ',欢迎来到首页!';
echo '<a href="logout.php">退出登录<a>';
} else {
echo "<script> alert('您需要先登录才能访问首页');parent.location.href='logout.php'; </script>";
exit;
}
?>
数据库连接:
<?php
$link=@mysqli_connect('localhost','root','123','user')or die("连接数据库失败");
mysqli_set_charset($link,'gb2312');
?>
退出登录,清理session中的数据,返回初始登录界面:
<?php
session_start();
$_SESSION['user'] = ('');
session_destroy();
header("Location:login.php");
?>
|