PHP连接数据库实现用户注册登录功能
代码如下dbconnect.phplogin.htmlregister.phplogin.phpindex.phplogout.php)
代码如下
数据库连接,登录页面,注册功能,登录功能,后台页面,注销功能。
dbconnect.php
<?php$server = "localhost";$db_username = "root";$db_passwd = "123456";$dbname = "test";//连接数据库$db_link = mysqli_connect($server,$db_username,$db_passwd,$dbname);//连接失败if(!$db_link){die("Can't connect to MySQL Server.Errorcode:" . mysqli_connect_error());}//设置数据库编码mysqli_query($db_link,"SET NAMES 'utf8'"); ?>
login.html
<html><!--/article/172542.htm--><head><title>后台登录</title><!--解决html中文在浏览器中显示乱码--><meta http-equiv="Content-Type"content="text/html;charset=utf-8"></head><body><h1>请输入你的账号密码进行登录</h1><form action="login.php" method="post"><fieldset><legend><h2>用户登录</h2></legend><ul><li><label>用户名:</label><input type="text" name="username"></li><li><label>密码:</label><input type="password" name="passwd"></li><li><label> </label><input type="checkbox" name="remember" value="yes"> 七天内自动登录</li><li><label> </label><input type="submit" name="login" value="登录"> <input type="reset" name="reset" value="重置"> </td> </tr> </li><li><label> <label><td colspan="2" align="center"> 还没有账号,快去<a href="register.php" rel="external nofollow" >注册</a>吧</td></li></ul></fieldset></form></body></html>
register.php
<html><head><title>用户注册</title><meta name="content-type"; charset="UTF-8"></head><body><h1>用户注册</h1><form action="register.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="passwd" id="passwd" name="passwd" required="required"></td></tr><tr><td colspan="2" align="center"><input type="submit" id="register" name="register" value="注册"><input type="reset" id="rester" name="rester" value="重置"></td></tr><tr><td colspan="2" align="center">如果已有账号,快去<a href="login.html" rel="external nofollow" rel="external nofollow"> 登录</a>吧!</td></tr></table></form></body></html><?phprequire('dbconnect.php');header('Content-type:text/html;charset=utf-8');$username = $_POST['username'];$passwd = $_POST['passwd'];$sql = "select username from user where username = '$username'";$result = mysqli_query($db_link,$sql);$row = mysqli_fetch_assoc($result);//判断用户名是否存在if($username == $row['username']){echo "<script>alert('用户名.$username.已经存在!请重新注册')</script>";}else{//用户名不存在 将注册的账号密码加入数据库$sql_insert = "insert into user(username,passwd) values('$username','$passwd')";mysqli_query($db_link,$sql_insert);header('refrsh:3;url=login.html');echo "<script>alert('$username.用户注册成功,请点击登录去登录!')</script>";}?>
login.php
<?phprequire("dbconnect.php");header('Content-Type:text/html;charset=utf-8');//开启session()session_start();//处理用户登录信息if(isset($_POST['login'])){//接收用户的登录信息$username=$_POST['username'];$passwd = $_POST['passwd'];//判断提交的登录信息if(($username == '') || ($passwd == '')){//若为空则视为未填写返回login.html页面header('refresh:3;url=login.html');echo "用户名或密码不能为空,3秒后将返回登录页面,请重新输入!";exit;}else{//若不为空则判断和账号密码是否存在于数据库中$sql = "select username,passwd from user where username = '$username'"; //构造查询账号密码语句$result = mysqli_query($db_link,$sql); //查询字符串$row = mysqli_fetch_assoc($result); //从结果中取一行为关联数组if(($username != $row['username']) || ($passwd != $row['passwd'])){//账号密码错误的情况下 同样和空处理一样header('refresh:3;url=login.html');echo "用户名或密码错误,3庙后将放回登录页面,请重新输入!";exit;}elseif($passwd==$row['passwd']){#用户名和密码都正确,将用户名和密码信息存到Session中$_SESSION['username'] = $username;$_SESSION['islogin'] = 1;echo "登录成功!三秒后跳转到后台管理页面";if($_POST['remember'] == "yes"){#如果勾选七天内自动登录 将其保存到cookie中 并设置保存7天setcookie('username',$username,time()+7*24*60*60);setcookie('code',md5($username.md5($passwd)),time()+7*24*60*60);#print_r($_COOKIE);}else{#没有勾选删除cookiesetcookie('username','',time()-999999);setcookie('code','',time()-999999);}header('refresh:3;url=index.php');}} }?>
index.php
<?phpheader('Content-type:text/html; charset=utf-8');//开启sessionsession_start();//先判断cookie是否有用户信息if(isset($_COOKIE['username'])){$_SESSION['username'] = $_COOKIE['username'];$_SESSION['islogin'] = 1;#print_r($_SESSION);}if(isset($_SESSION['islogin'])){//如果已经登陆echo "你好".$_SESSION['username']." ,欢迎来到个人中心!<br>";echo "<a href='logout.php'>注销</a>";}else{//如果没有登录echo "你还没有登录,请<a href = 'login.html'>登录</a>";}?>
logout.php
<?phpheader('Contet-type:text/html; charset=utf-8');//注销后的操作session_start();//清楚session$username = $_SESSION['username']; //用于后面的提示信息$_SESSION = array();session_destroy();//清除cookiesetcookie('username','', time()-99);setcookie('code','', time()-99);//提示信息echo "期待下次登录,".$username.'<br>';echo "<a href='login.html'>重新登录</a>";?>
如果觉得《PHP连接数据库实现用户注册登录功能》对你有帮助,请点赞、收藏,并留下你的观点哦!