登录注册系统上篇
1制作一个表单供用户登录注册
2合法用户登录成功可看到全部用户的全部信息,非法用户弹窗提示注册。
3到注册页面填写相关信息,更新数据库
一、前期准备
1、安装AppServ,建议安装低版本的,设置比较简单,没那么麻烦,适合新手
博主提供一个,64位32位都在里面 http://download.csdn.net/detail/shaynerain/9707047
2、简单的安装教程
1)选择安装位置
2)设置端口为80,其他随便,若端口设置为其他之后还需要配置,建议80
3)四个全选,下一步
4)设置root密码,一定要记住设置的什么,之后会经常用到,建议简单一些,我用的1
5)安装完成
6)测试是否安装成功,访问 http://127.0.0.1 弹出以下网页OK
二、添加数据库
1、打开数据,访问 http://127.0.0.1 然后输入刚才设置的密码
2、建立数据库添加数据,看图
1)选择库,然后新建表
2)插入相关信息
3)保存后插入
4)输入两个,供等下PHP调用
所有PHP文件放安装目录 D:\AppServ\www下,访问时直接访问http://127.0.0.1/xx xx是文件名,相关文件和库上传在http://download.csdn.net/detail/shaynerain/9707655
直接源码
1、 login源码,登录访问数据库,可以用上篇的两个登录
<?
session_start();
if($_POST["submit"])
{
$conn=mysql_connect("localhost","root","1"); //连接数据库服务器
mysql_query("set names 'gb2312'"); //设置字符集
mysql_select_db("login",$conn); //选择数据库
$name = $_POST['name'];
$word = $_POST['password'];
$resultSet = mysql_query("select * from mysql where Name='$name' and Password='$word'");
if(mysql_num_rows($resultSet)>0)
{
$_SESSION["user"] = $name;
if(isset($_SESSION['user']))
echo "<script>location.href='user.php';</script>";
}
else
{
echo "<script>alert('用户名或密码错误!如果没有账号请注册!')</script>";
}
mysql_close($conn) or die(mysql_error());
}
?>
<html xmlns = "http://www.w3.org/1999/xhtml" >
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<head>
<title>用户登录</title>
</head>
<STYLE type = "text/css" >
#form1{
position:relative;
left:30%;
top:20%;
}
</STYLE>
<body>
<form id = "form1" name="form1" method="post" action="" >
<table border = "2" >
<tr>
<th colspan = "2" > 用户登录 </ th >
</tr>
<tr>
<td >账号</td>
<td ><input type = "text" name="name" id="name" /></td>
</tr>
<tr>
<td>密码</td>
<td><input type = "password" name="password" id="password" /></td>
</tr>
<tr>
<td colspan = "2" align="center"><input type = "submit" name="submit" id="submit" value="登录" />
<input type = "button" name="register" id="register" value="注册" onclick="window.location.href='reg.php'"/></td>
</tr>
</table>
</form>
</body>
</html>
2、user文件,登录后跳转到这个页面,展示所有用户信息
<?
session_start();
if($_GET["quit"] == -1){
session_unset();
}
if(isset($_SESSION['user']))
{
$conn=mysql_connect("localhost","root","1"); //连接数据库服务器
mysql_query("set names 'gb2312'"); //设置字符集
mysql_select_db("login",$conn); //选择数据库
$result = mysql_query("select * from mysql");
}
else
{
echo "<script>location.href='login.php';</script>";
}
?>
<html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>欢迎登陆</title>
</head>
<body>
欢迎登陆<?= $_SESSION['user']?>
<table border = "1" width="100%">
<th width = "25%" > Name </ th >
< th width ="25%">Password</th>
<th width = "25%" > Email </ th >
< th width = "25%">Phone</th>
<? //循环输出记录到页面上
while($row=mysql_fetch_assoc($result))
{?>
<tr>
<td > <?= $row['Name']?> </td>
<td><?= $row['Password']?></td>
<td><?= $row['Email']?></td>
<td><?= $row['Phone']?></td>
</tr>
<?
}
mysql_close($conn) or die(mysql_error());
?>
</table>
<a href = "user.php? quit=-1" > 退出登录 </ a >< br >
</ body >
</ html >
3、reg文件,注册页面
<html xmlns = "http://www.w3.org/1999/xhtml" >
< meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<?
$tip = array("","","",);
$err = 0;
if($_POST['submit'])
{
$Name = $_POST['Name'];
$Password = $_POST['Password'];
$RePassword = $_POST['Repassword'];
$email = $_POST['Email'];
$Phone = $_POST['Phone'];
//检测为空
if($Name == "")
{
$err++;
$tip[0] = "用户不能为空";
}
if($Password == "")
{
$err++;
$tip[1] = "密码不能为空";
}
if($RePassword == "")
{
$err++;
$tip[2] = "确认密码不能为空";
}
if($err == 0){
if($Password != $RePassword)
{
echo "<script>alert('两次密码不一致!')</script>";
echo "<script>location.href='register.php';</script>";
exit;
}
$conn=mysql_connect("localhost","root","1"); //连接数据库服务器
mysql_query("set names 'gb2312'"); //设置字符集
mysql_select_db("login",$conn); //选择数据库
$resultSet = mysql_query("select * from mysql where Name='$Name'");//查找是否有相同的
if(mysql_num_rows($resultSet)>0){
echo "<script>alert('用户名已存在!')</script>";
echo "<script>location.href='register.php';</script>";
mysql_close($conn) or die(mysql_error());
exit;
}
$registersql = "INSERT INTO mysql (Name,Password,Email,Phone) VALUES
('$Name','$Password','$Email','$Phone')";//注意空格敏感,被坑了好久,总是插不进去
$resultSet = mysql_query($registersql,$conn);//插入
echo "<script>alert('用户已经成功注册!')</script>";
mysql_close($conn) or die(mysql_error());
echo "<script>location.href='login.php';</script>";
}
}
?>
<head>
<title>用户注册</title>
</head>
<!--设置显示位置-->
<STYLE type = "text/css" >
#form2{
position:relative;
left:30%;
top:20%;
}
</STYLE>
<body>
<form id = "form2" method="post" action="" >
<h2>用户注册</h2>
<font color = "red" > *必填
<br><br>
<!--注意属性的大小写敏感-->
<font color = "black" > Name :<input type = "text" name="Name"><font color = "red" > *<?= $tip[0]?>
<br><br>
<font color = "black" > Password:<input type = "text" name="Password"><font color = "red" > *<?= $tip[1]?>
<br><br>
<font color = "black" > RePassword:<input type = "text" name="Repassword"><font color = "red" > *<?= $tip[2]?>
<br><br>
<font color = "black" > Email:<input type = "text" name="Email">
<br><br>
<font color = "black" > Phone:<input type = "text" name="Phone">
<br><br>
<input type = "submit" name="submit" value="注册">
<input type = "reset" name="clear" value="清除">
</form>
</form>
</body>
4、其他,提供一个查看数据库信息的文件,方便调试
<? /*连接数据库*/
$conn=mysql_connect("localhost","root","1"); //连接数据库服务器
mysql_query("set names 'gb2312'"); //设置字符集
mysql_select_db("login",$conn); //选择数据库
$result=mysql_query("Select * from mysql",$conn); //创建结果集
?>
<table border = "1" width="100%">
<th width = "25%" > Name </ th >
< th width ="25%">Password</th>
<th width = "25%" > Email </ th >
< th width = "25%">Phone</th>
<? //循环输出记录到页面上
while($row=mysql_fetch_assoc($result))
{?>
<tr>
<td > <?= $row['Name']?> </td>
<td><?= $row['Password']?></td>
<td><?= $row['Email']?></td>
<td><?= $row['Phone']?></td>
</tr>
<?
}
mysql_close($conn) or die(mysql_error());
?>
</table>