Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:看来, 还是离开老师代码, 一句也写不出来, 全程抄写
<?php
// 判断是否已经登录?
if (isset($_COOKIE['user'])) $user = unserialize($_COOKIE['user']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理系统</title>
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<!-- 顶部导航 -->
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">管理系统</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<!-- 根据cookie判断用户是否登录,显示不同的内容 -->
<?php if (isset($user)) : ?>
<li><a href="" id="logout"><span style="color:red"><?php echo $user['name']?></span> 退出</a></li>
<?php else: ?>
<li><a href="login.php">登录</a></li>
<?php endif ?>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
</div>
<script>
// 为退出按钮创建事件监听器
document.querySelector('#logout').addEventListener('click', function(event) {
if (confirm('是否退出')) {
// 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
event.preventDefault();
// 跳转到退出事件处理器
window.location.assign('handle.php?action=logout');
}
});
</script>
</body>
</html>
<?php
$pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');
$stmt = $pdo->prepare('SELECT * FROM `user`');
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
//print_r( $users);
$action = $_GET['action'];
switch (strtolower($action))
{
//登陆
case 'login':
if($_SERVER['REQUEST_METHOD']='POST'){
$email = $_POST['email'];
$password = sha1($_POST['password']);
$results = array_filter($users, function($user) use ($email, $password) {
return $user['email'] === $email && $user['password'] === $password;
});
if(count($results) === 1){
setcookie('user',serialize(array_pop($results)));
exit('<script>alert("验证通过");location.href="index.php"</script>');
}else{
exit('<script>alert("邮箱或密码错误,或者还没有帐号");location.href="login.php";</script>');
}
}else {
die('请求类型错误');
}
break;
//退出
case 'logout':
if(isset($_COOKIE['user']))
{
setcookie('user',null,time()-3600);
exit('<script>alert("退出成功");location.assign("index.php")</script>');
}
break;
case 'register':
$name = $_POST['name'];
$email = $_POST['email'];
$password = sha1($_POST['p1']);
$register_time = time();
$sql = "INSERT `user` SET `name`='{$name}', `email`='{$email}', `password`='{$password}', `create_time`={$register_time}";
$stmt = $pdo->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() === 1) exit('<script>alert("注册成功");location.assign("login.php")</script>');
else exit('<script>alert("注册失败");location.assign("register.php")</script>');
break;
// 未定义
default:
exit('未定义操作');
}
<?php
// 判断是否已登录
if (isset($_COOKIE['user']))
exit('<script>alert("请不要重复登录");location.href="index.php";</script>');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理系统</title>
<!-- 使用load标签加载资源文件 -->
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
<div class="page-header text-center">
<h3>管理系统登陆</h3>
</div>
<form class="form-horizontal"
action="handle.php?action=login" method="post">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">邮箱:</label>
<div class="col-sm-10">
<input type="email" name="email" class="form-control" id="inputEmail3" placeholder="邮箱">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10">
<input type="password" name="password" class="form-control" id="inputPassword3" placeholder="密码">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary btn-block">登录</button>
</div>
</div>
</form>
<div class="form-group">
<label ><a href="register.php">还没有帐号, 注册一个吧</a></label>
</div>
</div>
<div class="col-md-4">
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理系统</title>
<!-- 使用load标签加载资源文件 -->
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
<div class="page-header text-center">
<h3>管理系统注册</h3>
</div>
<form class="form-horizontal"
action="handle.php?action=register" method="post">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">用户:</label>
<div class="col-sm-10">
<input type="text" name="name" class="form-control" id="name" placeholder="用户名" required>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">邮箱:</label>
<div class="col-sm-10">
<input type="email" name="email" class="form-control" id="inputEmail3" placeholder="邮箱" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10">
<input type="password" name="password1" class="form-control" id="inputPassword3" placeholder="密码" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10">
<input type="password" name="password2" class="form-control" id="inputPassword4" placeholder="必须与上面一致" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary btn-block">注册</button>
</div>
</div>
</form>
<a href="login.php">我有帐号,直接登录</a>
<script>
// 验证二次密码是否相等?
function compare() {
if (document.forms[0].p1.value.trim() !== document.forms[0].p2.value.trim()) {
document.querySelector('#tips').innerText = '二次密码不相等';
return false;
}
}
</script>
</div>
<div class="col-md-4">
</div>
</div>
</div>
</body>
</html>
<?php
session_start();
// 判断是否已经登录?
if (isset($_SESSION['user'])) $user = unserialize($_SESSION['user']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理系统</title>
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<!-- 顶部导航 -->
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">管理系统</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<!-- 根据cookie判断用户是否登录,显示不同的内容 -->
<?php if (isset($user)) : ?>
<li><a href="" id="logout"><span style="color:red"><?php echo $user['name']?></span> 退出</a></li>
<?php else: ?>
<li><a href="login.php">登录</a></li>
<?php endif ?>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
</div>
<script>
// 为退出按钮创建事件监听器
document.querySelector('#logout').addEventListener('click', function(event) {
if (confirm('是否退出')) {
// 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
event.preventDefault();
// 跳转到退出事件处理器
window.location.assign('handle.php?action=logout');
}
});
</script>
</body>
</html>
<?php
session_start();
// 判断是否已登录
if (isset($_SESSION['user']))
exit('<script>alert("请不要重复登录");location.href="index.php";</script>');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理系统</title>
<!-- 使用load标签加载资源文件 -->
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
<div class="page-header text-center">
<h3>管理系统登陆</h3>
</div>
<form class="form-horizontal"
action="handle.php?action=login" method="post">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">邮箱:</label>
<div class="col-sm-10">
<input type="email" name="email" class="form-control" id="inputEmail3" placeholder="邮箱">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10">
<input type="password" name="password" class="form-control" id="inputPassword3" placeholder="密码">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary btn-block">登录</button>
</div>
</div>
</form>
<div class="form-group">
<label ><a href="register.php">还没有帐号, 注册一个吧</a></label>
</div>
</div>
<div class="col-md-4">
</div>
</div>
</div>
</body>
</html>
<?php
session_start();
$pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');
$stmt = $pdo->prepare('SELECT * FROM `user`');
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
//print_r( $users);
$action = $_GET['action'];
switch (strtolower($action))
{
//登陆
case 'login':
if($_SERVER['REQUEST_METHOD']='POST'){
$email = $_POST['email'];
$password = sha1($_POST['password']);
$results = array_filter($users, function($user) use ($email, $password) {
return $user['email'] === $email && $user['password'] === $password;
});
if(count($results) === 1){
$_SESSION['user'] = serialize(array_pop($results));
exit('<script>alert("验证通过");location.href="index.php"</script>');
}else{
exit('<script>alert("邮箱或密码错误,或者还没有帐号");location.href="login.php";</script>');
}
}else {
die('请求类型错误');
}
break;
//退出
case 'logout':
if (isset($_SESSION['user']))
{
session_destroy();
exit('<script>alert("退出成功");location.assign("index.php")</script>');
}
break;
case 'register':
$name = $_POST['name'];
$email = $_POST['email'];
$password = sha1($_POST['p1']);
$register_time = time();
$sql = "INSERT `user` SET `name`='{$name}', `email`='{$email}', `password`='{$password}', `create_time`={$register_time}";
$stmt = $pdo->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() === 1) exit('<script>alert("注册成功");location.assign("login.php")</script>');
else exit('<script>alert("注册失败");location.assign("register.php")</script>');
break;
// 未定义
default:
exit('未定义操作');
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理系统</title>
<!-- 使用load标签加载资源文件 -->
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" >
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
<div class="page-header text-center">
<h3>管理系统注册</h3>
</div>
<form class="form-horizontal"
action="handle.php?action=register" method="post">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">用户:</label>
<div class="col-sm-10">
<input type="text" name="name" class="form-control" id="name" placeholder="用户名" required>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">邮箱:</label>
<div class="col-sm-10">
<input type="email" name="email" class="form-control" id="inputEmail3" placeholder="邮箱" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10">
<input type="password" name="password1" class="form-control" id="inputPassword3" placeholder="密码" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10">
<input type="password" name="password2" class="form-control" id="inputPassword4" placeholder="必须与上面一致" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary btn-block">注册</button>
</div>
</div>
</form>
<a href="login.php">我有帐号,直接登录</a>
<script>
// 验证二次密码是否相等?
function compare() {
if (document.forms[0].p1.value.trim() !== document.forms[0].p2.value.trim()) {
document.querySelector('#tips').innerText = '二次密码不相等';
return false;
}
}
</script>
</div>
<div class="col-md-4">
</div>
</div>
</div>
</body>
</html>