PHP+MYSQL实现简单的登录index.html
登录框:使⽤HTML、CSS、DIV 通过POST⽅式把⽤户名、密码提交给login.php处理。
<!--登录-->
<div class="login_box">
<form action="login.php" method="post">
<h3 class="login_font1">账号登录</h3>
<div><label class="login_font_user">⽤户名</label></div>
<input class="login_user_box" type="text" name="username" placeholder="请输⼊⽤户名">
<div><label class="login_font_pwd">密码</label></div>php远程连接mysql数据库
<input class="login_pwd_box" type="password" name="password" placeholder="请输⼊密码">
<div><input class="login_submit" type="submit" value="登录"></div>
<div class="copyright"><a class="copyright_mail" href ="mailto:tech_li@126">© 2019 保留所有权利</a></div> </form>
</div>
通过mysqli扩展库使⽤⾯向对象的⽅式连接数据库,选择库。
connect.php
<?php
$server="localhost";
$db_username="root";
$db_password="xiaoqiang123";
$dbname="test";
$conn = new mysqli($server,$db_username,$db_password,$dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
>
包含connect.php,获取index.html的表单数据,查询数据库是否有正确的数据库条⽬,根据结果进⾏返回。<?php
header("Content-Type: text/html; charset=utf8");
include('connect.php');
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
if ($username && $password){//如果⽤户名和密码都不为空
$sql = "select * from user where username = '$username' and password=md5('$password')";
$result = $conn->query($sql);
$rows=$result->num_rows;
if($rows){//0 false 1 true
header("refresh:0;url=welcome.html");//如果成功跳转⾄welcome.html页⾯
exit;
}else{
//echo "⽤户名或密码错误";
echo "<script>alert('⽤户名或密码错误!')</script>";
echo "
<script>
setTimeout(function(){window.location.href='index.html';},1000);
</script>
";//如果错误使⽤js 1秒后跳转到登录页⾯重试;
}
}
//释放结果集
$result->free();
>
welcome.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>welcome</title>
</head>
<body>
登录成功
</body>
</html>
Mysql操作⾮常简单,根据⾃⼰需求建⽴相应的库和表项即可,根据需求进⾏调整。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。