登錄實現 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(); }