本文主要是介绍ubantu下php和myql实现登陆功能并且在主页显示用户名,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
直接上代码:
登陆表单【login.php】
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><br/><br/><br/><br/><br/><br/><br/><form action="logcheck.php" method="post"><div class="sign-ipt f-l"><p>用户名:</p><input type="text" name="id" placeholder="ID" /><p>密码:</p><input type="password" name="pwd" placeholder="密码" /><br /><button class="slig-btn">登录</button><p>没有账号?请<a href="register.php">注册</a></p></div></form></body>
</html>
登陆后台检测【logcheck.php】
<?phpsession_start();header("Content-type: text/html; charset=utf-8");$username = $_POST['id'];$password = $_POST['pwd'];$conn = new mysqli('localhost','led','123456','db_cexu');if ($conn->connect_error){echo '数据库连接失败!';exit(0);}else{if ($username == ''){echo '<script>alert("请输入用户名!");history.go(-1);</script>';exit(0);}if ($password == ''){echo '<script>alert("请输入密码!");history.go(-1);</script>';exit(0);}$sql = "select id,pwd,name from cexu_data where id = '$username' ";$result = $conn->query($sql);$number = mysqli_num_rows($result);$row = mysqli_fetch_array($result);
// echo $row['name'];
/*while($row = mysqli_fetch_array($result)){echo "<br/>";echo $row['id'];echo "<br/>";echo $row['pwd'];echo "<br/>";echo $row['name'];}
*/ if ($number) {$_SESSION['username'] = $row['name'];echo "<script>window.location.href='index.php'</script>";} else {echo '<script>alert("用户名或密码错误!");history.go(-1);</script>';}}
?>
主页【index.php】
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><?php
session_start();
echo $_SESSION['username'];
echo "<br/>";
?>
<h1> 欢迎来到在线测序网站 </h1></body>
</html>
输出结果:
----------------------------------分割线------------------------------------------------
参考:
https://blog.csdn.net/qq_29656961/article/details/78924085
https://liu-endong.blog.csdn.net/article/details/90987926
这篇关于ubantu下php和myql实现登陆功能并且在主页显示用户名的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!