Bootstrap 結合 PHP ,做簡單的登錄以及註冊界面及功能

来源:https://www.cnblogs.com/duxiu-fang/archive/2019/05/17/10883300.html
-Advertisement-
Play Games

登錄實現 HTML代碼 CSS: PHP: 註冊實現 HTML: CSS: PHP: ...


登錄實現

HTML代碼

<div class="container">
        <?php if (isset($error_msg)): ?>
        <div class="alert alert-danger" role="alert"><?php echo $error_msg; ?></div>
        <?php endif ?>
        <?php if (isset($success_msg)): ?>
        <div class="alert alert-info" role="alert"><?php echo $success_msg; ?></div>
        <?php endif ?>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" autocomplete="on">
            <div class="form-group">
                <label for="administrator">管理員賬號(11位手機號碼)</label>
                <input type="number" class="form-control" name="administrator" id="administrator" value="<?php echo isset($_POST['administrator']) ? $_POST['administrator'] : ''; ?>">
            </div>
            <div class="form-group">
                <label for="password">密碼</label>
                <input type="password" class="form-control" name="password" id="password">
            </div>
            <button class="btn btn-info btn-md btn-block">登錄</button>
        </form>
    </div>

CSS:

<link rel="stylesheet" type="text/css" href="css/Bootstrap.css">
    <style type="text/css">
        .container {
            margin-top: 150px;
        }
    </style>

PHP:

function login () {
    if (empty($_POST['administrator'])) {
        $GLOBALS['error_msg'] = '請輸入管理員賬號';
        return;
    }
    if (strlen($_POST['administrator']) !== 11) {
        $GLOBALS['error_msg'] = '您輸入的管理員賬號不符合相關規定';
        $_POST['administrator'] = '';
        return;
    }
    if (empty($_POST['password'])) {
        $GLOBALS['error_msg'] = '請輸入密碼';
        return;
    }

    $connection = mysqli_connect('localhost', 'root', '密碼', 'students_info_system');

    if (!$connection) {
        $GLOBALS['error_msg'] = '連接資料庫失敗';
        return;
    }

    $query = mysqli_query($connection, 'select * from administrators');

    if (!$query) {
        $GLOBALS['error_msg'] = '查詢數據失敗';
        return;
    }

    while ($administrator = mysqli_fetch_assoc($query)) {
        $administrators[] = $administrator;
    }
    // var_dump($administrators);

    for ($i = 0; $i < count($administrators); $i++) {
        $administrator[] = $administrators[$i]['administrator'];
        $passwords[] = $administrators[$i]['password'];
    }

    // var_dump($administrator);
    // var_dump($passwords);

    if (!in_array((string)$_POST['administrator'], $administrator)) {
        $GLOBALS['error_msg'] = '您輸入的管理員賬號不存在';
        return;
    }

    if (!in_array((string)$_POST['password'], $passwords)) {
        $GLOBALS['error_msg'] = '密碼錯誤';
        return;
    }

    // 賬號和密碼都存在了----判斷密碼和賬號與資料庫中是否一致
    $index = array_search($_POST['administrator'], $administrator);
    if ((string)$_POST['password'] !== $passwords[$index]) {
        $GLOBALS['error_msg'] = '密碼錯誤';
        return;
    }

    // 在資料庫找到了相應的賬號和密碼
    $GLOBALS['success_msg'] = '登錄成功';

    // 延時2秒執行後面的代碼
    time_sleep_until(time() + 2);

    // 跳轉到內容頁面(並將賬號一併傳過去,區分用戶賬號以顯示各個用戶對應的界面)
    header("Location: student_info.php?id={$_POST['administrator']}");
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    login();
}

註冊實現

HTML:

<div class="container">
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" complete="on">
            <?php if (isset($error_msg)): ?>
            <div class="alert alert-danger" role="alert"><?php echo $error_msg; ?></div>
            <?php endif ?>
            <?php if (isset($success_msg)): ?>
            <div class="alert alert-info" role="alert"><?php echo $success_msg; ?></div>
            <?php endif ?>
            <div class="form-group">
                <label for="administrator">管理員賬戶(11位手機號碼即可)</label>
                <input type="number" class="form-control" name="administrator" id="administrator" value="<?php echo isset($_POST['administrator']) ? $_POST['administrator'] : ''; ?>">
            </div>
            <div class="form-group">
                <label for="password">密碼</label>
                <input type="password" name="password" id="password" class="form-control" value="<?php echo isset($_POST['password']) ? $_POST['password'] : ''; ?>">
            </div>
            <div class="form-group">
                <label for="check-pwd">確認密碼</label>
                <input type="password" name="check-pwd" id="check-pwd" class="form-control">
            </div>
            <button class="btn btn-info btn-block btn-md">註冊</button>
        </form>
    </div>

CSS:

<link rel="stylesheet" type="text/css" href="css/Bootstrap.css">
    <style type="text/css">
        .container {
            margin-top: 100px;
        }
    </style>

PHP:

function register () {
    $connection = mysqli_connect('localhost', 'root', '密碼', 'students_info_system');

    if (!$connection) {
        $GLOBALS['error_msg'] = '連接資料庫失敗';
        return;
    }

    $query = mysqli_query($connection, 'select * from administrators');

    if (!$query) {
        $GLOBALS['error_msg'] = '查詢數據失敗';
        return;
    }

    while ($administrator = mysqli_fetch_assoc($query)) {
        $administrators[] = $administrator;
    }
    // var_dump($administrators);

    // 獲取資料庫中已經註冊的賬號
    for ($i = 0; $i < count($administrators); $i++) {
        $administrator[] = $administrators[$i]['administrator'];
    }

    if (empty($_POST['administrator'])) {
        $GLOBALS['error_msg'] = '請輸入管理員賬戶';
        return;
    }
    if (strlen($_POST['administrator']) !== 11) {
        $GLOBALS['error_msg'] = '您輸入的管理員賬戶不符合相關規定,請重新輸入';
        $_POST['administrator'] = '';
        return;
    }
    // 判斷該賬號是否已被註冊
    if (in_array($_POST['administrator'], $administrator)) {
        $GLOBALS['error_msg'] = '該賬號已被註冊,請另起賬號';
        return;
    }
    if (empty($_POST['password'])) {
        $GLOBALS['error_msg'] = '請輸入密碼';
        return;
    }
    if (empty($_POST['check-pwd'])) {
        $GLOBALS['error_msg'] = '請確認密碼';
        return;
    }
    if ($_POST['check-pwd'] !== $_POST['password']) {
        $GLOBALS['error_msg'] = '兩次密碼輸入不一致,請重新確認密碼';
        return;
    }

    $administrator = $_POST['administrator'];
    $password = $_POST['password'];$query = mysqli_query($connection, "insert into administrators values ({$administrator}, '{$password}')");

    if (!$query) {
        $GLOBALS['error_msg'] = '查詢數據失敗';
        return;
    }

    $affected_rows = mysqli_affected_rows($query);

    if ($affected_rows !== 1) {
        $GLOBALS['error_msg'] = '插入數據失敗';
    }

    $GLOBALS['success_msg'] = '註冊成功';

    // 延時2秒執行後面的語句
    time_sleep_until(time() + 2);

    header('Location: index.html');
    
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    register();
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 第1題:動態載入又對及時性要求很高怎麼處理? 如何知道一個網站是動態載入的數據? 用火狐或者谷歌瀏覽器 打開你網頁,右鍵查看頁面源代碼,ctrl +F 查詢輸入內容,源代碼裡面並沒有這個值,說明是動態載入數據。 1. Selenium+Phantomjs 2. 儘量不使用 sleep 而使用 Web ...
  • 斐波那契數列、(引用float(‘inf’)無窮大的特性來比對,從而提取數組中的最小值)float(“inf”)正無窮大 float(“-inf”)負無窮大 ...
  • 本隨筆旨在強化理解傳值與傳引用 如下代碼的運行結果 其中i沒有改變,s也沒有改變。 但model中的值均改變了。 i :100s :hellomodel :testchangemodel2 :changeModel i :100s :hellomodel :testchangemodel2 :cha ...
  • 有時候maven真的很坑! 有時候提示invalid LOC header (bad signat signature), 但又有時候什麼都不提示,工程報錯,情況有肯多中,不知道大家遇到過幾種詭異的. ...
  • 之前寫了一個版本的,不過代碼繁瑣而且不好用,效率有些問題。尤其pdf轉圖片速度太慢。下麵是優化版本的代碼。 spriing_boot 版本信息:2.0.1.RELEASE 1、配置信息: application.yml 2、轉換入口 pdf 轉圖片參考 https://gitee.com/cycmy ...
  • 好的代碼風格,給人舒服的感覺,今天介紹一下谷歌的Python風格規範 1 分號 不要在行尾加分號, 也不要用分號將兩條命令放在同一行。 2 行長度 每行不超過80個字元;不要使用反斜杠連接行。Python會將圓括弧、中括弧和花括弧的行隱式的連接起來,可以利用這個特點。如果需要,可以在表達式外圍增加一 ...
  • 本文原創並首發於公眾號【 Python貓 】,未經授權,請勿轉載。 原文地址:https://mp.weixin.qq.com/s/ fFVTgWVsydFsNu1nyxUzA Python 是一門強大的動態語言,那動態體現在哪裡,強大又體現在哪裡呢?除了好的方面,Python 的動態性是否還藏著一 ...
  • PHP Standards Recommendation PHP標準推薦 https://www.php-fig.org/psr/ PSR-1:基礎代碼標準 PHP文件必須使用<?php 和 <?= 標記。 PHP文件必須使用不帶BOM頭的UTF8編碼格式保存。 PHP文件中要麼只包含定義語句(例: ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...