php實現登錄頁面的簡單實例

在本篇文章裏小編給大家分享的是關於php實現登錄頁面的簡單實例以及相關知識點,有需要的朋友們學習下。

開始自然是從最簡單的功能起步,我第一個任務選擇了做一個登錄操作,其實也沒想象中那麼簡單。

1、首先自然是連接和創建數據庫

這部分我寫在model.php中

$userName='root';

$passWord='';

$host='localhost';

$dataBase='login';

//創建連接

$conn=mysqli_connect($host,$userName,$passWord,$dataBase);

2、寫前臺頁面,爲了熟練前端框架,使用layui框架界面,前面有一段js代碼,來判斷用戶名密碼輸入是否爲空。

<!DOCTYPE html>

<html>

<script src="layui.js";></script>

<link rel="stylesheet" href="layui.css" rel="external nofollow" ;>

<head>

  <meta charset="UTF-8">

  <title>註冊登錄</title>

</head>

<script language=JavaScript>

  function InputCheck()

  {

    if (Login.username.value == "")

      {

       alert("請輸入用戶名!");

       Login.username.focus();

       return (false);

       }

    if (Login.password.value == "")

       {

        alert("請輸入密碼!");

        Login.password.focus();

      return (false);

       }

     }

</script>

<body style="background: #1E9FFF">

<div style="position: absolute; left: 50%; top: 50%;width: 500px; margin-left:-250px; margin-top: -200px">

  <div style="background: #FFFFFF; padding: 20px;border-radius: 4px;box-shadow: 5px 5px 20px #444444" >

    <div>

      <form action="login.php" method="post" name="Login" οnsubmit="return InputCheck()">

        <div style="color: gray">

          <h2>註冊登錄系統</h2>

        </div>

        <hr>

        <div>

          <label>用戶名</label>

          <div>

            <input type="text" name="username" id="username" placeholder="用戶名"

            autocomplete="off">

          </div>

        </div>

        <div>

          <label>密  碼</label>

          <div>

            <input type="password" name="password" id="password" placeholder="密碼"

            autocomplete="off">

          </div>

        </div>

        <div>

          <div;>

            <input type="submit" value="登錄">

            <input type="button" value="註冊">

          </div>

        </div>

      </form>

    </div>

  </div>

</div>

</body>

</html>

3、login.php 用來判斷用戶名密碼的正確性,關於這一點我看了網上的很多方法,五花八門,在我沒遇到障礙之前,我決定先用簡單的形式,就是用sql語句查詢用戶名配上密碼的結果集,結果集爲空,則不存在該用戶。

<?php

//數據庫連接

require_once 'model.php';

//從登錄頁接受來的數據

$name=$_POST['username'];

$pwd=$_POST['password'];

$sql="select id,username,password from user where username='$name' AND password='$pwd';";

$result=mysqli_query($conn,$sql);

$row=mysqli_num_rows($result);

 

if(!$row){

 

    echo "<script>alert('密碼錯誤,請重新輸入');location='login.html'</script>";

 

  }

  else{

 

    echo "<script>alert('登錄成功');location='123'</script>";

  };

4、文件目錄

5、效果如下:

以上就是php如何實現登錄頁面的詳細內容,感謝大家對神馬文庫的支持。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章