首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 學生管理系統簡易實作:Session 狀態、CRUD 與查詢功能

     編輯:PHP 相關知識

PHP 學生管理系統簡易實作

以 **Session** 為儲存後端 (無資料庫),示範登入驗證、學生資料 CRUD (新增、列表、查詢、畢業/刪除、更新)、關鍵字搜尋等核心功能。適合教學、原型驗證或輕量內部工具。

架構概覽

  • 儲存:`$_SESSION['students']` 陣列,鍵為學號,值為學生資料陣列。
  • 入口:`index.php` (登入) → `main.php` (管理主頁) → 各功能頁。
  • 驗證:管理員固定帳密 `Admin` / `root`,登入成功寫入 Session 與 Cookie (1200 秒)。

核心代碼模組

1. 登入驗證 (login.php)

<?php
session_start();
$user = $_POST['userName'] ?? '';
$pass = $_POST['passWord'] ?? '';

if ($user === 'Admin' && $pass === 'root') {
    $_SESSION['user'] = $user;
    setcookie('userName', $user, time() + 1200, '/', '', false, true); // HttpOnly
    header('Location: main.php');
    exit;
}
header('Location: index.php?error=1');
exit;
?>

2. 學生新增 (add.php)

<?php
session_start();
if (empty($_SESSION['user'])) { header('Location: index.php'); exit; }

$id = trim($_POST['s_ID'] ?? '');
$name = trim($_POST['Name'] ?? '');
$idCard = trim($_POST['IDcard'] ?? '');
$major = trim($_POST['Major'] ?? '');
$sex = trim($_POST['sex'] ?? '');

if ($id && $name) {
    $_SESSION['students'][$id] = [
        's_ID' => $id,
        'Name' => $name,
        'IDcard' => $idCard,
        'Major' => $major,
        'sex' => $sex,
    ];
}
header('Location: main.php?msg=added');
exit;
?>

3. 列表顯示 (list.php)

<?php
session_start();
if (empty($_SESSION['user'])) { header('Location: index.php'); exit; }
$students = $_SESSION['students'] ?? [];
?>

學生列表

學生列表 (共 筆)

學號姓名身份證號性別專業操作
編輯 | 畢業
回主頁

4. 關鍵字搜尋 (search.php)

<?php
session_start();
if (empty($_SESSION['user'])) { header('Location: index.php'); exit; }

$kw = trim($_POST['search'] ?? '');
$results = [];
if ($kw) {
    foreach ($_SESSION['students'] as $stu) {
        if (in_array($kw, $stu, true)) { // 搜尋所有欄位
            $results[$stu['s_ID']] = $stu;
        }
    }
}
$_SESSION['search_results'] = $results;
header('Location: search_result.php');
exit;
?>

5. 畢業/刪除 (graduate.php)

<?php
session_start();
if (empty($_SESSION['user'])) { header('Location: index.php'); exit; }
$id = $_GET['id'] ?? '';
if ($id && isset($_SESSION['students'][$id])) {
    // 移至歷史檔 (示範)
    $_SESSION['history'][$id] = $_SESSION['students'][$id];
    unset($_SESSION['students'][$id]);
}
header('Location: list.php');
exit;
?>

6. 編輯/更新 (edit.php → update.php)

<!-- edit.php -->
<?php
session_start();
if (empty($_SESSION['user'])) { header('Location: index.php'); exit; }
$id = $_GET['id'] ?? '';
$stu = $_SESSION['students'][$id] ?? null;
if (!$stu) { header('Location: list.php'); exit; }
?>
姓名:
專業:
性別:
<!-- update.php --> <?php session_start(); if (empty($_SESSION['user'])) { header('Location: index.php'); exit; } $id = $_POST['c_ID'] ?? ''; if ($id && isset($_SESSION['students'][$id])) { $_SESSION['students'][$id] = array_merge($_SESSION['students'][$id], [ 'Name' => $_POST['Name'] ?? '', 'Major' => $_POST['Major'] ?? '', 'sex' => $_POST['sex'] ?? '', ]); } header('Location: list.php'); exit; ?>

前端主頁 (main.php)

<?php
session_start();
if (empty($_SESSION['user'])) { header('Location: index.php'); exit; }
?>

管理主頁

歡迎 (Cookie 1200 秒)

局限與擴充建議

  • Session 儲存:預設檔案儲存,多伺服器需改用 Redis/Memcached (`session.save_handler`)。
  • 並發競爭:Session 鎖會串行化請求,高併發場景建議改用資料庫 + 乐觀鎖。
  • 資料持久化:正式專案必須使用 MySQL/PostgreSQL,Session 僅作暫態狀態。
  • 安全性:輸出轉義 `htmlspecialchars()`、CSRF Token、SQL 參數化、密碼雜湊 (此處為教學簡化)。

此簡易系統展示 PHP Session 應用、MVC 雛形與基本 CRUD 流程,可作為進一步開發的起點。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved