登录实现
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();
}