PHP登录(连接数据库)小案例

实现效果

        

     

数据库信息

 

 代码示例:

 

1. login.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<div style="width: 250px;height: 200px;border: solid 1px;">
    <form action="service.php" method="post">
        用户名:<input type="text" name="username"><br/>
        密码:<input type="text" name="password"><br/>
        <input type="checkbox" name="remember"
         <?php if(!empty($_POST['remember'])){echo 'checked="checked"';}?>>
        记住我<br/>
        
        <input type="submit" value="登录"><br/>
        <div align="right">
            <a href="recoverypassword.php">忘记密码</a><br/>
            <a href="register.php">注册</a><br/>
            <a href="changepassword.php">修改密码</a>
        </div>
    </form>
</div>
</body>
</html>

2. index.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div style="width: 250px;height: 200px;border: solid 1px;">
    <?php 
        session_start();
    ?>
    欢迎您---<?php echo $_SESSION['username']?>
    <div align="right">
        <a href="zhuxiao.php">注销</a>
    </div>
</div>
</body>
</html>

3. changepassword.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div style="width: 250px;height: 200px;border: solid 1px;">
    <h4 align="center">changepassword.php</h4>
    <form action="changepassword_service.php"  method="post">
        用户名:<input type="text" name="username"><br/>
        原密码:<input type="text" name="oldpassword"><br/>
        新密码:<input type="text" name="newpassword"><br/>
        <input type="submit" value="修改密码"><br/>
        <div align="right">
            <a href="login.php">登录</a>
        </div>
    </form>
</div>

</body>
</html>

4. changgepassword_service.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<?php
//连接数据库服务器
$link = mysqli_connect("127.0.0.1","root","liupw")
or die("连接失败".mysqli_error($link)."<br/>");
echo "连接成功"."<br/>";
//选择数据库
$selectDB=mysqli_select_db($link, "usermanager")
or die("选择数据库usermanager失败"."<br/>");
echo "选择数据库usermanager成功"."<br/>";

//获取表单提交的数据
$username = $_POST["username"];
$oldpassword = $_POST["oldpassword"];
$newpassword=$_POST["newpassword"];

//表的查询
$query=mysqli_query($link, "select * from usertable")
or die("查询失败"."<br/>");
echo "查询成功"."<br/>";
while ($result_array = mysqli_fetch_array($query)) {
    if($username==$result_array["username"]){
        if($oldpassword==$result_array["password"]){
            $query=mysqli_query($link, "update usertable set password='$newpassword' where username ='$username' ")
            or die("修改密码失败"."<br/>");
            header("location:login.php");
        }
        else {
            echo "您还没有注册";
        }
    }
}

//关闭数据库服务器
mysqli_close($link);

?>
</body>
</html>

5. recoverypassword_service.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<?php
//连接数据库服务器
$link = mysqli_connect("127.0.0.1","root","liupw")
or die("连接失败".mysqli_error($link)."<br/>");
echo "连接成功"."<br/>";
//选择数据库
$selectDB=mysqli_select_db($link, "usermanager")
or die("选择数据库usermanager失败"."<br/>");
echo "选择数据库usermanager成功"."<br/>";

//获取表单提交的数据
$username = $_POST["username"];
$eamil = $_POST["eamil"];

//表的查询
$query=mysqli_query($link, "select * from usertable")
or die("查询失败"."<br/>");
echo "查询成功"."<br/>";
while ($result_array = mysqli_fetch_array($query)) {
    if($username==$result_array["username"]){
        if($eamil==$result_array["usermail"]){
            $query=mysqli_query($link, "update usertable set password='666' where username ='$username' ")
            or die("插入失败"."<br/>");
            header("location:login.php");
            
        }
    }
}

//关闭数据库服务器
mysqli_close($link);

?>
</body>
</html>

6. recoverypassword.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div style="width: 250px;height: 200px;border: solid 1px;">
    <h4 align="center">recoverypassword.php</h4>
    <form action="recoverypassword_service.php"  method="post">
        用户名:<input type="text" name="username"><br/>
         邮箱:<input type="text" name="eamil"><br/>
        <input type="submit" value="重置密码"><br/>
    </form>
</div>
</body>
</html>

7. register_service.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<?php
//连接数据库服务器
$link = mysqli_connect("127.0.0.1","root","liupw")
or die("连接失败".mysqli_error($link)."<br/>");
echo "连接成功"."<br/>";
//选择数据库
$selectDB=mysqli_select_db($link, "usermanager")
or die("选择数据库usermanager失败"."<br/>");
echo "选择数据库usermanager成功"."<br/>";

//获取表单提交的数据
$username = $_POST["username"];
$password = $_POST["password"];
$repassword=$_POST["repassword"];
$useremail=$_POST["email"];

if($password==$repassword){
    $query=mysqli_query($link, "insert into usertable values('$username','$password','$useremail')")
    or die("插入失败"."<br/>");
    header("location:login.php");
}
//表的查询


//关闭数据库服务器
mysqli_close($link);

?>
</body>
</html>

8. regiser.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div style="width: 250px;height: 200px;border: solid 1px;">
    <h4 align="center">register.php</h4>
    <form action="register_service.php"  method="post">
        用户名:<input type="text" name="username"><br/>
        密码:<input type="text" name="password"><br/>
        确认密码:<input type="text" name="repassword"><br/>
        邮箱:<input type="text" name="email"><br/>
        <input type="submit" value="注册"><br/>
        <div align="right">
            <a href="login.php">登录</a>
        </div>
    </form>
</div>

</body>
</html>

9. service.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<?php
//连接数据库服务器
$link = mysqli_connect("127.0.0.1","root","liupw")
or die("连接失败".mysqli_error($link)."<br/>");
echo "连接成功"."<br/>";
//选择数据库
$selectDB=mysqli_select_db($link, "usermanager")
or die("选择数据库usermanager失败"."<br/>");
echo "选择数据库usermanager成功"."<br/>";
//表的查询
$query=mysqli_query($link, "select * from usertable")
or die("查询失败"."<br/>");
echo "查询成功"."<br/>";
//获取表单提交的数据
$username = $_POST["username"];
$password = $_POST["password"];

while ($result_array = mysqli_fetch_array($query)) {
    if($username==$result_array["username"]){
        if($password==$result_array["password"]){
            session_start();
            $_SESSION['username']=$username;
            header("location:index.php");
        }else{
            echo "用户密码不正确";
        }
    }
}

//关闭数据库服务器
mysqli_close($link);

?>
</body>
</html>

10. zhuxiao.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<?php
//连接数据库服务器
$link = mysqli_connect("127.0.0.1","root","liupw")
or die("连接失败".mysqli_error($link)."<br/>");
echo "连接成功"."<br/>";
//选择数据库
$selectDB=mysqli_select_db($link, "usermanager")
or die("选择数据库usermanager失败"."<br/>");
echo "选择数据库usermanager成功"."<br/>";
//表的查询
$query=mysqli_query($link, "select * from usertable")
or die("查询失败"."<br/>");
echo "查询成功"."<br/>";
//获取表单提交的数据
session_start();
$username = $_SESSION['username'];

while ($result_array = mysqli_fetch_array($query)) {
    if($username==$result_array["username"]){
            mysqli_query($link, "delete from usertable where username='$username'");
            echo "注销成功";
            break;
    }
}

//关闭数据库服务器
mysqli_close($link);

?>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/sunxiaoyan/p/9289003.html