Login function: Let’s first look at the following html code
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>欢迎登录</title> <style type="text/css"> *{margin: 0px;padding: 0px;} body{background:#eee;} #div{width:300px;height:400px;background:#B1FEF9;margin:0 auto;margin-top:150px; border-radius:20px;} h3{margin-left:48px;padding-top:60px;} h4{margin-left:120px;padding-top:60px;font-size: 18px;} #cnt{width:280px;height:370px;margin-left:33px;padding-top:60px;} .sub{width:70px;height:30px;border:1px solid #fff;background:#eee; margin-left:28px;margin-top:20px;} .sub1{ width:70px;height:30px;border:1px solid #fff;background:#eee;margin-left:150px;margin-top:20px;} </style> </head> <body> <div id="div"> <h3>欢迎登陆后台管理系统</h3> <div id="cnt"> <form method="post" action="main.php"> 用户名:<input type="text" placeholder="请输入用户名" name="username"> <br><br> 密 码:<input type="password" placeholder="请输入密码" name="password"> <br><br> <input type="submit" value="登录" class="sub"> </form> </div> </div> </body> </html>
The form is submitted to main.php. Now let’s analyze main.php
After we log in, if we have a lot of There is no web page to operate on for a long period of time. When you operate again, you need to log in. This will use our session knowledge
First we need to open the session
session_start();
Then we need to introduce the file conn.php that links to the database.
require_once('conn.php');
Get the form information, and then save the form information Enter session
$name = $_POST['username'];
$pwd = md5($_POST['password']);
$_SESSION['name']=$name;
$_SESSION['pwd']=$pwd;
Let’s go to the database to query. If there is information submitted by the form in the database, then we should make the information submitted by the form available for login operations
$sql = "select * from user where username='$name' and password='$pwd'";
$info = mysql_query($sql);
$row = mysql_fetch_row($info );
Then judge $row, if it exists, the login is successful, jump to the homepage to add a message, otherwise, return to the page and log in again
if($row) {
echo " alert('Login failed')</script>";
echo "<script>location.href='login.php';</script>"; //Login failed, jump to another one Page
}
<?php session_start(); require_once('conn.php'); $name = $_POST['username']; $pwd = md5($_POST['password']); $_SESSION['name']=$name; $_SESSION['pwd']=$pwd; $sql = "select * from user where username='$name' and password='$pwd'"; $info = mysql_query($sql); $row = mysql_fetch_row($info); if($row){ echo "<script>alert('登录成功');location.href='message.php';</script>"; }else{ echo "<script>alert('登录失败')</script>"; echo "<script>location.href='login.php';</script>"; //登录失败,跳转到另外一个页面 } ?>