PHP development...LOGIN

PHP development user login module PHP page

We introduced the main functions of user login earlier. This page implements these functions through PHP code.

Here we use POST to obtain data, such as username and password.

<?php
$username = $_POST["username"];  //用户名
$password = $_POST["password"];  //密码
$code = $_POST["code"]; //验证码
?>

You need to connect to the database and determine whether the connection is successful. We have introduced the creation of database test and table login before. You can connect directly here.

<?php
$link = mysqli_connect('localhost','root','root','test');
if (!$link) {
  die("连接失败:".mysqli_connect_error());
}
$sql = "select * from login";
$result = mysqli_query($link, $sql);
$rows = mysqli_fetch_array($result);
?>

It is necessary to verify the user name and password to prevent illegal login access

<?php
if($username == "")
{
  //echo "请填写用户名<br>";
  echo"<script type='text/javascript'>alert('请填写用户名');location='login.html'; </script>";
}
if($password == "")
{
  //echo "请填写密码<br><a href='login.html'>返回</a>";  
  echo"<script type='text/javascript'>alert('请填写密码');location='login.html';</script>";
}
?>

After the user fills in the user name and password, he needs to go to the database to check whether they are correct. Only after verification is correct can you log in normally and jump to the login success page.

<?php
if($rows) {
  //拿着提交过来的用户名和密码去数据库查找,看是否存在此用户名以及其密码
    if ($username == $rows["username"] && $password == $rows["password"]) {
      //echo "验证成功!<br>";
      echo "<script type='text/javascript'>alert('登陆成功');location='success.html';</script>";
    } else {
      //echo "用户名或者密码错误<br>";
      echo "<script type='text/javascript'>alert('用户名或者密码错误');location='login.html';</script>";
      //echo "<a href='login.html'>返回</a>";
    }
}
?>

Complete login.php file code:

<?php
    //开启Session
    session_start();
    header("Content-type:text/html;charset=utf-8");
    $link = mysqli_connect('localhost','root','root','test');
    if (!$link) {
     die("连接失败:".mysqli_connect_error());
    }
    //接受提交过来的用户名及密码
    $username = $_POST["username"];//用户名
    $password = $_POST["password"];//密码
    $code = $_POST["code"]; //验证码
    if($username == "")
    {
     //echo "请填写用户名<br>";
     echo"<script type='text/javascript'>alert('请填写用户名');location='login.html'; </script>";
    }
    if($password == "")
    {
     //echo "请填写密码<br><a href='login.html'>返回</a>";  
     echo"<script type='text/javascript'>alert('请填写密码');location='login.html';</script>";
    }
    if($code != $_SESSION['authcode']) //判断填写的验证码是否与验证码PHP文件生成的信息匹配
    {
     echo "<script type='text/javascript'>alert('验证码错误!');location='login.html';</script>";
    }
    $sql = "select * from login";
    $result = mysqli_query($link, $sql);
    $rows = mysqli_fetch_array($result);
    if($rows) {
     //拿着提交过来的用户名和密码去数据库查找,看是否存在此用户名以及其密码
        if ($username == $rows["username"] && $password == $rows["password"]) {
         //echo "验证成功!<br>";
         echo "<script type='text/javascript'>alert('登陆成功');location='success.html';</script>";
        } else {
         //echo "用户名或者密码错误<br>";
         echo "<script type='text/javascript'>alert('用户名或者密码错误');location='login.html';</script>";
         //echo "<a href='login.html'>返回</a>";
        }
    }
?>

Note: This course is just a simple demonstration of user login. Its code is for learning reference only and cannot be directly used in projects.

Next Section
<?php //开启Session session_start(); header("Content-type:text/html;charset=utf-8"); $link = mysqli_connect('localhost','root','root','test'); if (!$link) { die("连接失败:".mysqli_connect_error()); } //接受提交过来的用户名及密码 $username = $_POST["username"];//用户名 $password = $_POST["password"];//密码 $code = $_POST["code"]; //验证码 if($username == "") { //echo "请填写用户名<br>"; echo"<script type='text/javascript'>alert('请填写用户名');location='login.html'; </script>"; } if($password == "") { //echo "请填写密码<br><a href='login.html'>返回</a>"; echo"<script type='text/javascript'>alert('请填写密码');location='login.html';</script>"; } if($code != $_SESSION['authcode']) //判断填写的验证码是否与验证码PHP文件生成的信息匹配 { echo "<script type='text/javascript'>alert('验证码错误!');location='login.html';</script>"; } $sql = "select * from login"; $result = mysqli_query($link, $sql); $rows = mysqli_fetch_array($result); if($rows) { //拿着提交过来的用户名和密码去数据库查找,看是否存在此用户名以及其密码 if ($username == $rows["username"] && $password == $rows["password"]) { //echo "验证成功!<br>"; echo "<script type='text/javascript'>alert('登陆成功');location='success.html';</script>"; } else { //echo "用户名或者密码错误<br>"; echo "<script type='text/javascript'>alert('用户名或者密码错误');location='login.html';</script>"; //echo "<a href='login.html'>返回</a>"; } } ?>
submitReset Code
ChapterCourseware