实例(index.php)
<!--开启会话--> <?php session_start(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>员工管理系统</title> <style> /*样式重置*/ h2, p, ul { padding: 0; margin: 0; } /*头部样式*/ .header { height: 60px; /*background-color: lightblue;*/ border-bottom: 1px solid #333; line-height: 60px; } .header .content { width: 1000px; /*background-color: lightgray;*/ overflow: hidden; margin: 0 auto; } .header .content h2 { float:left } .header .content p { float:right; } /*主体样式*/ .main { width: 1000px; min-height: 650px; /*background-color: lightcyan;*/ margin: 0 auto; position: relative; } .main .left { width: 120px; min-height: inherit; /*background-color: lightgreen;*/ border-right: 1px solid #333; position: absolute; left: 0; top: 0; } .main .right { width: 880px; min-height: inherit; /*background-color: lightyellow;*/ position: absolute; left: 121px; top: 0; } /*左侧菜单样式*/ .main .left ul { position: absolute; left: 30px; top: 50px; } .main .left li { list-style-type: none; line-height: 50px; } .main .left li a { text-decoration-line: none; } .main .left li a:hover { text-decoration-line: underline; color: red; } /*右侧工作区样式*/ .main .right iframe { width: 880px; min-height: 650px; border: none; } </style> </head> <body> <!--头部--> <div class="header"> <div class="content"> <h2>员工管理系统</h2> <?php if(isset($_SESSION['username'])): ?> <p>管理员: <?=$_SESSION['username']?> | <a href="javascript:return false" onclick="return confirm('是否退出?') ? location.assign('logout.php') : false">退出</a> </p> <?php else: ?> <script>location.assign('login.php')</script> <?php endif; ?> </div> </div> <!--中部--> <div class="main"> <!--左侧菜单--> <div class="left"> <ul> <li><a href="staff_list.php" target="workspace">员工管理</a></li> <li><a href="system.php" target="workspace">系统设置</a></li> <li><a href="user_list.php" target="workspace">用户设置</a></li> </ul> </div> <!--右侧内容--> <div class="right"> <iframe src="staff_list.php" name="workspace"></iframe> <p style="text-align: center;margin-top: -100px;">php中文网 © 版权所有 (2017-2020)</p> </div> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例(用户登录界面login.php)
<!--重复登录验证--> <?php session_start(); ?> <?php if (isset($_SESSION['username'])): ?> <style>h2,p {text-align: center; margin-top: 60px;}</style> <h2>您已经登录过了,请不要重复登录</h2> <p>正在跳转中~~~</p> <script> setTimeout("location.href='index.php'",2000); </script> <?php else: ?> <!--用户登录界面--> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>用户登录</title> <style> h3 { text-align: center; } div { width: 300px; height: 150px; /*background-color: lightblue;*/ margin: 0 auto; text-align: center; padding: 20px; border: 1px dashed #888; border-radius: 5%; } div input { border: none; border-bottom: 1px solid #333; } button:hover { cursor: pointer; background-color: lightblue; } .success { color: green; } .error { color: red; } </style> </head> <body> <h3>用户登录</h3> <div> <form name="user"> <p> <label>邮 箱: <input type="email" name="email" placeholder="name@example.com"> </label> </p> <p> <label>密 码: <input type="password" name="password" placeholder="********"> </label> </p> <p> <button type="button" onclick="check(this.form)">登录</button> </p> <!-- 提示信息点位符--> <p></p> </form> </div> <script> // 获取表单 var user = document.forms.namedItem('user'); var tips = user.lastElementChild; function addEvent(ele,tips,msg) { ele.addEventListener('blur', function (){ if (this.value.trim().length === 0) { tips.classList.add('error'); tips.innerHTML = msg; this.focus(); } },false); ele.addEventListener('keydown', function () { tips.innerText = ''; },false); } // 给邮箱和密码元素添加事件 addEvent(user.email, tips, '邮箱不能为空'); addEvent(user.password, tips, '密码不能为空'); // 邮箱与密码需要到数据表中验证,我们通过"Ajax"异步操作实现 function check(form) { var request = new XMLHttpRequest(); request.onreadystatechange = function () { if (request.readyState === 4 && request.status === 200) { // console.log(request.responseText); var data = JSON.parse(request.responseText); // 根据返回的状态,添加适当的class样式 if (data.status === 1) { // 移除之前的样式,确保现有样式有效,如果之前没有样式也不会报错的 tips.classList.remove('error'); // 为成功添加特殊样式,即绿色 tips.classList.add('success'); tips.innerText = data.message; // 2秒后跳转到上一个页面,即用户列表页 setTimeout(function (){ // 跳转到员工管理后台首页 location.href = 'index.php'; },2000); } // 没有更新或更新错误采用同一个样式 else { tips.classList.add('error'); tips.innerText = data.message; } } }; request.open('POST', 'check.php', true); request.setRequestHeader('content-type','application/x-www-form-urlencoded'); var data = 'email='+form.email.value.trim()+'&password='+form.password.value.trim(); request.send(data); } </script> </body> </html> <?php endif; ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例(用户退出界面logout.php)
实例 <?php //开启会话 session_start(); if (isset($_SESSION['username'])) { session_destroy(); setcookie('PHPSESSION',time()-3600,'/'); header('location:login.php'); } else { echo '<h3 style="text-align: center;">朋友,您忘记登录了吧~~~</h3>'; echo '<h3 style="text-align: center;">请稍等,正在为您跳转~~~</h3>'; echo "<script>setTimeout(\"location.href='login.php'\",2000);</script>"; } 运行实例 » 点击 "运行实例" 按钮查看在线实例
实例(登录验证界面check.php)
<?php //print_r($_POST); //开启会话 session_start(); // $status = 0; $message = ''; //服务器进行二次非空验证 //邮箱的非空验证 if (empty($_POST['email'])) { $message = '邮箱不能为空'; // echo json_encode(['status'=>$status, 'message'=>$message]); // exit(); exit(json_encode(['status'=>$status, 'message'=>$message])); } else { $email = strtolower(trim($_POST['email'])); } //密码的非空验证 if (empty($_POST['password'])) { $message = '密码不能为空'; // echo json_encode(['status'=>$status, 'message'=>$message]); // exit(); exit(json_encode(['status'=>$status, 'message'=>$message])); } else { $password = sha1(strtolower(trim($_POST['password']))); } if ($email && $password) { //连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //SQL $sql = 'SELECT COUNT(*) FROM `user` WHERE `email`=:email AND `password`=:password'; $stmt = $pdo->prepare($sql); if($stmt->execute(['email'=>$email, 'password'=>$password])) { if($stmt->fetchColumn(0) > 0) { $sql = 'SELECT `id`,`name` FROM `user` WHERE `email`=:email AND `password`=:password'; $stmt = $pdo->prepare($sql); $stmt->execute(['email'=>$email, 'password'=>$password]); $user = $stmt->fetch(PDO::FETCH_ASSOC); //用session保存用户登录信息 $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $user['name']; $status = 1; $message = '登录成功,正在跳转中~~~~'; exit(json_encode(['status'=>$status, 'message'=>$message])); } } else { // die(print_r($stmt->errorInfo())); echo '登录不成功,请检查'; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例