您的位置:首页 > 编程语言 > PHP开发

PHP登录注册系统下

2016-12-10 21:51 239 查看
没看第上篇的先看上篇

所有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("se
ca50
lect * 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>
结束

转载请声明出处

闻道有先后,术业有专攻

作者:shaynerain

出处:

欢迎大神吐槽
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: