PHP实现⽤户登录注册(详细教程)
教程前先给⼤家看看⼩编的实现成果吧!
图1:
图2:
图3:
教程:
实现这个功能我们需要五个php⽂件:
login.php
(登录界⾯,如图2)
<!DOCTYPE html>
<html><head>
<title>登录</title>
<meta name="content-type"; charset="UTF-8">
</head><body>
<div class="content" align="center"> <!--头部-->
<div class="header"> <h1>登录页⾯</h1> </div>
<!--中部-->
<div class="middle">
<form id="loginform" action="loginaction.php" method="post">
<table border="0"> <tr>
<td>⽤户名:</td>
<td> <input type="text" id="name" name="username"
required="required" value="<?php
echo isset($_COOKIE[""]) ? $_COOKIE[""] : ""; ?>"> </td> </tr>
<tr> <td>密码:</td> <td><input type="password" id="password" name="password"></td>
</tr> <tr> <td colspan="2"> <input type="checkbox" name="remember"><small>记住我 </td> </tr> <tr> <td colspan="2" align="center" > <!--提⽰信息--> <?php
$err = isset($_GET["err"]) ? $_GET["err"] : "";
switch ($err) {
case 1:
echo "⽤户名或密码错误!";
break;
case 2:
echo "⽤户名或密码不能为空!";
break;
} ?> </td> </tr> <tr> <td colspan="2" align="center">
<input type="submit" id="login" name="login" value="登录"> <input type="reset" id="reset"
name="reset" value="重置"> </td> </tr>
<tr>
<td colspan="2" align="center"> 还没有账号,快去<a href="register.php">注册</a>吧</td>
</tr>
</table>
</form>
</div>
<!--脚部-->
<div class="footer"> <small>Copyright © 版权所有·欢迎翻版 </div> </div>
</body>
</html>
loginaction.php
(使login.php实现与数据库的连接,并校正输⼊)
<?php
// $Id:$ //声明变量
$username = isset($_POST['username']) ? $_POST['username'] : "";
$password = isset($_POST['password']) ? $_POST['password'] : "";
$remember = isset($_POST['remember']) ? $_POST['remember'] : ""; //判断⽤户名和密码是否为空
if (!empty($username) && !empty($password)) { //建⽴连接
$conn = mysqli_connect('localhost', '', '', 'user'); //准备SQL语句
$sql_select = "SELECT username,password FROM usertext WHERE username = '$username' AND password = '$password'"; //执⾏SQL语句 $ret = mysqli_query($conn, $sql_select);
$row = mysqli_fetch_array($ret); //判断⽤户名或密码是否正确
if ($username == $row['username'] && $password == $row['password'])
{ //选中“记住我”
if ($remember == "on")
{ //创建cookie
setcookie("", $username, time() + 7 * 24 * 3600);
} //开启sessionphp修改数据库内容
session_start(); //创建session
$_SESSION['user'] = $username; //写⼊⽇志
$ip = $_SERVER['REMOTE_ADDR'];
$date = date('Y-m-d H:m:s');
$info = sprintf("当前访问⽤户:%s,IP地址:%s,时间:%s /n", $username, $ip, $date);
$sql_logs = "INSERT INTO logs(username,ip,date) VALUES('$username','$ip','$date')";
//⽇志写⼊⽂件,如实现此功能,需要创建⽂件⽬录logs
$f = fopen('./logs/' . date('Ymd') . '.log', 'a+');
fwrite($f, $info);
fclose($f); //跳转到loginsucc.php页⾯
header("Location:loginsucc.php"); //关闭数据库,跳转⾄loginsucc.php
mysqli_close($conn);
}
else
{
//⽤户名或密码错误,赋值err为1
header("Location:login.php?err=1");
}
} else { //⽤户名或密码为空,赋值err为2
header("Location:login.php?err=2");
} ?>
loginsucc.php
(登录成功后界⾯,如图3)
<head>
<title>登录成功</title>
<meta name="content-type";
charset="UTF-8">
</head>
<body>
<div>
<?php
// $Id:$ //开启session
session_start(); //声明变量
$username = isset($_SESSION['user']) ? $_SESSION['user'] : ""; //判断session是否为空if (!empty($username)) { ?>
<h1>登录成功!</h1> 欢迎您!
<?php
echo $username; ?>
<br/> <a href="login.php">退出</a> //跳转⾄主⽹页
<?php
} else { //未登录,⽆权访问
>
<h1>你⽆权访问</h1>
<?php
} ?> </div>
</body>
</html>
register.php
(注册界⾯,如图1)
<head><title>注册</title>
<meta name="content-type"; charset="UTF-8">
</head><body>
<div class="content" align="center"> <!--头部-->
<div class="header"> <h1>注册页⾯</h1> </div> <!--中部-->
<div class="middle">
<form action="registeraction.php" method="post"> <table border="0">
<tr> <td>⽤户名:</td>
<td><input type="text" id="id_name" name="username" required="required"></td>
</tr> <tr>
<td>密码:</td> <td><input type="password" id="password" name="password"
required="required"></td>
</tr> <tr>
<td>重复密码:</td> <td><input type="password" id="re_password"
name="re_password" required="required"></td> </tr> <tr>
<td>性别:</td> <td> <input type="radio" id="sex" name="sex" value="mam">男 <input type="radio" id="sex" name="sex" value="woman">⼥ </td> </tr> <tr >
<td>QQ:</td> <td><input type="text" id="qq" name="qq" required="required"></td> </tr> <tr>
<td>Email:</td> <td><input type="email" id="email" name="email" required="required"></td> </tr> <tr>
<td>电话:</td> <td><input type="text" id="phone" name="phone" required="required"></td> </tr> <tr>
<td>地址:</td> <td><input type="text" id="address" name="address" required="required"></td> </tr>
<tr> <td colspan="2" align="center" > <!--提⽰信息-->
<?php
$err = isset($_GET["err"]) ? $_GET["err"] : "";
switch ($err) {
case 1:
echo "⽤户名已存在!";
break;
case 2:
echo "密码与重复密码不⼀致!";
break;
case 3:
echo "注册成功!";
break;
}
>
</td> </tr> <tr> <td colspan="2" align="center">
<input type="submit" id="register" name="register" value="注册">
<input type="reset" id="reset" name="reset" value="重置"> </td></tr>
<tr> <td colspan="2" align="center">
如果已有账号,快去<a href="login.php">登录</a>吧! </td> </tr> </table> </form> </div>
<!--脚部-->
<div class="footer"> <small>Copyright © 版权所有·欢迎翻版 </div> </div></body></html>
registeraction.php
(实现register.php连接数据库,并向指定表单插⼊数据)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论