Login for PHP development enterprise website (2)

Let’s take a look at how to determine whether the login is successful

login.php code is as follows:

<?php
    // 链接数据库
    session_start();
    require_once('conn.php');

    //获取表单的信息
    $name = trim($_POST['username']);
    $password = md5($_POST['password']);
    $_SESSION['username'] = trim($_POST['username']);
    $_SESSION['password'] = trim($_POST['username']);

    //查询数据库  然后取出数据库的信息,如果和表单提交的信息一致,则登录成功,进入后台管理
    $sql = "select * from user where username='$name' and password='$password'";
    $res = mysql_query($sql);
    $row = mysql_fetch_row($res);
    if($row){
        echo "<script>alert('登录成功')</script>";
        echo "<script>location.href='main.php'</script>";
    }else{
        echo "<script>alert('登录失败')</script>";
        echo "<script>history.go(-1);</script>";   //登录失败返回上一个页面
    }
?>

Let’s analyze the above code, open session log.php and submit it to login. php So we get the information submitted by the form in the login.php file

and then store the information into seesion

$_SESSION['username'] = trim($_POST['username'] );
$_SESSION['password'] = trim($_POST['username']);

Note: To save a session, you must first open the session

Link to the database , and then use sql statements to judge the information submitted by the form and the information in the database. If the same user name and password can be found in the database, then the user can log in

If the login is successful, we jump to Backend management page

Login failed, jump to login page

Continuing Learning
||
<?php // 链接数据库 session_start(); require_once('conn.php'); //获取表单的信息 $name = trim($_POST['username']); $password = md5($_POST['password']); $_SESSION['username'] = trim($_POST['username']); $_SESSION['password'] = trim($_POST['username']); //查询数据库 然后取出数据库的信息,如果和表单提交的信息一致,则登录成功,进入后台管理 $sql = "select * from user where username='$name' and password='$password'"; $res = mysql_query($sql); $row = mysql_fetch_row($res); if($row){ echo "<script>alert('登录成功')</script>"; echo "<script>location.href='main.php'</script>"; }else{ echo "<script>alert('登录失败')</script>"; echo "<script>history.go(-1);</script>"; //登录失败返回上一个页面 } ?>
submitReset Code