Correction status:qualified
Teacher's comments:
初始判断: is_session.php
<?php // 判断是否存在session 意味着已登录就存在,否则不跳转到登录页面 session_start(); if($_SESSION['user_id'] && $_SESSION['username']){ define('DSN','mysql:host=127.0.0.1;dbname=php;charset=utf8'); define('USER','root'); define('PWD','root'); try{ $pdo = new PDO(DSN,USER,PWD); }catch(PDOException $e){ echo "数据库连接失败! ".$e->getMessage(); die; } }else{ header('location:login.php'); } ?>
登录页面: login.php
<?php session_start(); // 开启session ?> <?php if(isset($_SESSION['username'])): ?> <p>您已经登录了,请不要重复登录</p> <p>正在跳转中...</p> <script>setTimeout('location.href="home.php"',2000)</script> <?php else: ?> <html> <head> <style> form{width:240;height:200px;margin:5% auto;} input{border:0;border-bottom:1px solid #616161;} .btn{width:60;height:20px;margin:5px 80px;} p{margin:20px 80px;width:240px;} .success{color:green;} .error{color:red;} </style> </head> <body> <form name="user"> <p> <label>账号:<label> <input type="text" name="account" placeholder="123456"> </p> <p> <label>密码:<label> <input type="password" name="password" placeholder="******"> </p> <div class="btn"> <button type="button" onclick="check(this.form);return false;">登录</button> </div> <p><!-- 登陆状态提示 --></p> </form> </body> </html> <script> var user = document.forms.namedItem('user'); // 获取整个form元素包括子元素 var tips = user.lastElementChild; // 获取form元素最后一个子元素 // console.log(user.tips) /* @param ele 事件对象 @param tips 提示信息的显示元素 @param msg 提示信息 addEventListener('事件','函数','bool值')方法用于指定元素添加事件 blur click keydown keyup 等 注意:去掉on 如果onclick = click */ 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.innerHTML = ''; },false); } addEvent(user.account,tips,'请输入账号'); addEvent(user.password,tips,'请输入密码'); // 触发ajax登录验证 function check(form){ // console.log(form.account.value.trim()); var request = new XMLHttpRequest(); request.onreadystatechange = function(){ if(request.readyState ===4 && request.status === 200){ // 将返回的json_encode数据转为对象 var res = JSON.parse(request.responseText); // console.log(res); if(res.status === 1){ tips.classList.remove('error'); tips.classList.add('success'); tips.innerHTML = res.message; setTimeout(function(){ location.href = 'home.php'; },2000); }else{ tips.classList.add('error'); tips.innerHTML = res.message; } } } request.open('POST', './check_login.php', true); request.setRequestHeader('content-type','application/x-www-form-urlencoded'); var data = 'account='+form.account.value.trim()+'&password='+form.password.value.trim(); request.send(data); } </script> <?php endif; ?>
检查提交登录的账号密码: check_login.php
<?php // 开启session_start() session_start(); // 接收提交过来的数据 $account = trim($_POST['account']); $password = trim($_POST['password']); // 对账号密码判断是否为空 $status = 0; // 初始化状态 $message = ''; // 初始化信息 if(empty($account)){ $messge = '账号不能为空!'; exit(json_encode(['status'=>$status, 'message'=>$message])); // 将json返回到Ajax }else{ $account = strtolower($account); // strtolower()函数将字母转为小写 } if(empty($password)){ $message = '密码不能为空!'; exit(json_encode(['status'=>$status, 'message'=>$message])); }else{ $password = sha1($password); // sha1() 对密码进行40位长度加密 } // 判断前面账号密码是否处理完毕,ok就进行与数据库验证操作 if($account && $password){ $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); $sql = "SELECT COUNT(*) FROM `user` WHERE `account`=:account AND `password`=:password"; $stmt = $pdo->prepare($sql); // 准备查询 $result = $stmt->execute(['account'=>$account, 'password'=>$password]); if($result){ if($stmt->fetchColumn(0) > 0){ $sql = "SELECT * FROM `user` WHERE `account`=:account AND `password`=:password"; $stmt = $pdo->prepare($sql); $stmt->bindParam(':account',$account,PDO::PARAM_INT); $stmt->bindParam(':password',$password,PDO::PARAM_STR); $stmt->execute(); $user = $stmt->fetch(PDO::FETCH_ASSOC); // echo '<pre>';print_r($user); // 将从数据库获取的用户id和用户名赋给$_SESSION $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $user['username']; $ststus = 1; $message = '登录成功,正在跳转...'; exit(json_encode(['status'=>$ststus, 'message'=>$message])); }else{ $message = '账号或密码错误!'; exit(json_encode(['status'=>$status, 'message'=>$message])); } }else{ die(print_r($stmt->errorInfo())); } }
登录成功跳转到home页面: home.php
<?php require "./is_session.php"; $title = $pdo->prepare("SELECT `sitename`,`status` FROM `system`"); $title->execute(); $h2 = $title->fetch(PDO::FETCH_ASSOC); $pdo = NULL; ?> <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-3.1.1.min.js"></script> <style> *{margin:0;padding:0;font-family:'楷体';} h2{margin-left:10px;color:#888;} a{text-decoration: none;} li{list-style: none;color:#888;} .big-box{width: 1000px;height: 600px;margin: 10px auto;} .menu-box{width: 150px;height: 600px;background: skyblue;float: left;} .menu-box li{width: 140px;height: 30px;line-height: 30px; margin: 5px 0px;padding-left: 10px;} .menu-box li:hover{background: #ff6700;cursor: pointer;color:#fff;} .menu-box li a{margin-left: 20px;} .right-box{width: 840px;height: 600px;float: right;border-right:10px solid skyblue;} .right-box .user{margin-top:8px;float:left;} button{margin-top:8px;margin-left:10px;float:right;display:block;} </style> </head> <body> <div class="big-box"> <div class="menu-box"> <ul> <h2 name="welcome.php"><?=$h2['sitename']?></h2> <li name="account_lists.php">用户账号</li> <?php if($h2['status']==1): ?> <li name="user_lists.php">用户列表</li> <?php endif;?> <li name="system.php">系统设置</li> </ul> </div> <div class="right-box"> <div style="width:100%;height:32px;background:skyblue;"> <?php if(isset($_SESSION['username'])): ?> <p class="user">用户名:<?=$_SESSION['username']?><span> | </span> <a href="javascript:;" onclick="logout()">退出</a></p> <?php else: ?> <script>location.href='login.php';</script> <?php endif; ?> <button>中文</button> <button onclick="jump()">English</button> </div> <iframe src="welcome.php" name="tag" width="100%" height="532" scrolling="no" frameborder="0"></iframe> <div style="width:100%;height:32px;background:skyblue;"></div> </div> </div> </body> </html> <script type="text/javascript"> function jump(){ window.location.href = 'admin_en/home.php'; } $(document).ready(function(){ $('.menu-box li').click(function(){ var src = $(this).attr('name'); $('iframe').attr('src',src); }); $('h2').click(function(){ var src = $(this).attr('name'); $('iframe').attr('src',src); }); }); function logout(){ var out = confirm('你想退出吗?'); if(out==true){ location.href = 'logout.php'; }else{ false; } } </script>
账号密码管理列表: account_lists.php
<?php require "./is_session.php"; $user_acc = "SELECT * FROM `user`"; $stmt = $pdo->prepare($user_acc); $stmt->execute(); // print_r($stmt->fetch(PDO::FETCH_ASSOC)); $pdo = NULL; ?> <html> <head> <style> table{margin:0 auto;width:100%;} tr,td,th{border:1px solid #888;text-align:center;} th{background:pink;} caption{font-size:22px;margin:10px 0;font-weight:bold;} .success{color:green;} .error{color:red;}; </style> </head> <body> <table border="0" cellpadding="0" cellspacing="0"> <caption>账号管理</caption> <tr> <th>ID</th> <th>名称</th> <th>账号</th> <th>密码</th> <th>邮箱</th> <th>状态</th> <th>添加时间</th> <th>操作<button style="float:right;">新增</button></th> </tr> <?php // 返回索引为结果集列名的数组,循环输出到表格里 while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ ?> <tr> <td><?=$row['id'] ?></td> <td><?=$row['username'] ?></td> <td><?=$row['account'] ?></td> <td><?=$row['password'] ?></td> <td><?=$row['email'] ?></td> <td><?=($row['status']==1)?'Ok':'No' ?></td> <td><?=$row['addtime'] ?></td> <td> <button onclick="edit(<?=$row['id']?>)">编辑</button> <button onclick="del(<?=$row['id']?>)">删除</button> </td> </tr> <?php } unset($pdo); ?> </table> <p id="msg"></p> </body> </html> <script> function edit(id){ location.href = 'account_add.php?id='+id; } function del(id){ if(confirm('你确定要删除吗?')){ var request = new XMLHttpRequest(); request.onreadystatechange = function(){ if(request.readyState == 4 && request.status == 200){ // 特级注意: JSON.parse() 不能同时处理2个回调 var res = JSON.parse(request.responseText); var tips = document.getElementById('msg'); tips.innerHTML = res.mssage; if(res.status == 1){ tips.classList.add('success'); }else{ tips.classList.add('error'); } } setTimeout(function(){ location.reload(); },1000); } request.open('POST','./account_manage.php?action=del'); request.setRequestHeader('content-type','application/x-www-form-urlencoded'); request.send('id='+id); }else{ return false; } } </script>
账号密码添加: account_add.php (添加和编辑差不多所以没有写添加只写了编辑)
<?php require "./is_session.php"; $id = (int)trim($_GET['id']); $sql_edit = "SELECT * FROM `user` WHERE `id`=:id"; $stmt = $pdo->prepare($sql_edit); $stmt->bindParam(':id',$id,PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); unset($pdo); ?> <html> <head> <style> label{width:65px;display:block;float:left;padding-right:5px;} .box{width:320px;height:240px;background:#FFFAFA;margin:30px auto;} .success{color:green;} .error{color:red;} </style> </head> <body> <div class="box"> <form method="POST"> <input type="hidden" name="id" value="<?=$row['id']?>"> <p> <label>账号:</label> <input type="text" name="account" value="<?=$row['account']?>" disabled> </p> <p> <label>名称:</label> <input type="text" name="username" value="<?=$row['username']?>"> </p> <p> <label>密码:</label> <input type="password" name="password" value="<?=$row['password']?>"> </p> <p> <label>邮箱:</label> <input type="text" name="email" value="<?=$row['email']?>"> </p> <p> <label>显示:</label> <input type="radio" name="status" value="<?=($row['status']==1) ? $row['status'] : 1 ?>" <?=($row['status']==1) ? 'checked' : '' ?> > </p> <p> <label>隐藏:</label> <input type="radio" name="status" value="<?=($row['status']==0) ? $row['status'] : 0 ?>" <?=($row['status']==0) ? 'checked' : '' ?> ><br> </p> <p style="margin-left:65px;"> <button onclick="save(this.form);return false;">保存</button> <button onclick="history.back();return false;" style="margin-left:60px;">返回</button> </p> <p style="margin-left:65px;"></p> </form> <div> </body> </html> <script> function save(form){ var request = new XMLHttpRequest(); request.onreadystatechange = function(){ if(request.readyState === 4 && request.status === 200){ var res = JSON.parse(request.responseText); // console.log(request.responseText) var tips = form.lastElementChild; // 获取form标签最后一个子元素 tips.innerHTML = res.mssage; // 给子元素添加json返回的信息 if(res.status == 1){ tips.classList.add('success'); setTimeout(function(){ self.location = document.referrer; },1000); }else{ tips.classList.add('error'); } } } request.open('POST','./account_manage.php?action=save'); request.setRequestHeader('content-type','application/x-www-form-urlencoded'); var data = 'id='+form.id.value+'&username='+form.username.value+'&password='+form.password.value+'&email='+form.email.value+'&status='+form.status.value; request.send(data); } </script>
编辑账号处理: account_manage.php
<?php require "./is_session.php"; // 将接收的get值变为小写和去除左右空格 $action = strtolower(trim($_GET['action'])); $id = (int)$_POST['id']; switch($action){ case 'save': $username = $_POST['username']; $password = trim($_POST['password']); $email = $_POST['email']; $status = $_POST['status']; $sql = 'UPDATE `user` SET `username`=:username,`password`=:password,`email`=:email,'; $sql .= '`status`=:status WHERE `id`=:id'; $result = $pdo->prepare($sql); $result->bindParam(':username',$username,PDO::PARAM_STR); $result->bindParam(':password',$password,PDO::PARAM_STR); $result->bindParam(':email',$email,PDO::PARAM_STR); $result->bindParam(':status',$status,PDO::PARAM_INT); $result->bindParam(':id',$id,PDO::PARAM_INT); if($result->execute()){ if($result->rowCount() === 1){ $status = 1; $mssage = '更新成功'; }else if($result->rowCount() ==0 ){ $status = 0; $mssage = '无效更新'; }else{ $status = -1; $mssage = '更新错误,请检查!'; } } echo json_encode(['status'=>$status,'mssage'=>$mssage]); break; case 'del'; $sql = 'DELETE FROM `user` WHERE `id`=:id'; $res = $pdo->prepare($sql); $res->execute(['id'=>$id]); if($res->rowCount() == 1){ $status = 1; $mssage = '删除成功'; }else if($res->rowCount() == 0){ $status = 0; $mssage = '无效删除'; }else{ $status = -1; $mssage = '删除错误,请检查!'; } exit(json_encode(['status'=>$status, 'mssage'=>$mssage])); break; } $pdo = NULL; // 断开PDO连接
人员管理列表: user_lists.php
<?php require "./is_session.php"; // 引入sql连接文件 $pageNum = 3; // 设置每页显示的数量 $page = isset($_GET['p']) ? $_GET['p'] : $_GET['p'] = 1; // URL p 参数 $stmt = $pdo->prepare("SELECT COUNT(*) FROM `staff`"); // 准备查询 $stmt->execute(); // 执行查询 $total = $stmt->fetchColumn(0); // 从结果集中返回单独一列 $pages = ceil($total / $pageNum); // ceil()函数小数点向上取整 // print $pages; $offset = ($page - 1) * $pageNum; // $_GET的数量减1再乘每页总数,得出结果就是LIMIT的偏移量 $sql = "SELECT `id`,`name`,`age`,`course`,`mobile`,`intodate`,`status`"; $sql .= " FROM `staff` LIMIT {$offset},{$pageNum}"; $stmt = $pdo->prepare($sql); $stmt->execute(); $staff = $stmt->fetchAll(PDO::FETCH_ASSOC); // echo '<pre>';print_r($staff);die; $info = $pdo->prepare("SELECT `company`,`tel` FROM `system`"); $info->execute(); $res = $info->fetch(PDO::FETCH_ASSOC); ?> <html> <head> <style> table{margin:0 auto;width:100%;} tr,td,th{border:1px solid #888;text-align:center;} th{background:pink;} caption{font-size:22px;margin:10px 0;font-weight:bold;} .success{color:green;} .error{color:red;} .page{width:22px;height:22px;border:0px solid #ccc;display:block; text-decoration:none;text-align:center;line-height:22px;float:left; margin:10px 5px;} .info{width:100%;height:50px;margin-top:320px;} .info span{margin:0 auto;display:block;} </style> </head> <body> <table border="0" cellpadding="0" cellspacing="0"> <caption>用户信息表</caption> <tr> <th>ID</th> <th>名称</th> <th>年龄</th> <th>课程</th> <th>联系</th> <th>添加时间</th> <th>状态</th> <th>操作<button style="float:right;" onclick="add();">新增</button></th> </tr> <?php // fetchAll 返回为二维数组,遍历输出到表格 foreach($staff as $row){ ?> <tr> <td><?=$row['id']?></td> <td><?=$row['name'] ?></td> <td><?=$row['age'] ?></td> <td><?=$row['course'] ?></td> <td><?=$row['mobile'] ?></td> <td><?=date('Y-m-d',$row['intodate']) ?></td> <td><?=($row['status']==1)?'show':'hide' ?></td> <td> <button onclick="edit(<?=$row['id'];?>);">编辑</button> <button onclick="del(<?=$row['id'];?>)">删除</button> </td> </tr> <?php } unset($pdo); ?> </table> <a class="page" href="<?=$_SERVER['PHP_SELF']?>?p=1">首</a> <?php for($i=1;$i<=$pages;$i++): ?> <?php if(isset($_GET['p']) && $_GET['p']==$i){ $bg = 'style="background:lightblue;"'; }else{ $bg = ""; } ?> <a class="page" <?=$bg?> href="javascript: location.href='<?=$_SERVER['PHP_SELF']?>?p=<?=$i?>'"> <?=($i==3)?'...':$i?> </a> <?php endfor ?> <a class="page" href="<?=$_SERVER['PHP_SELF']?>?p=<?=$pages?>">尾</a> <p id="msg"></p> <div class="info"> <span><?=$res['company']?></span> <span><?=$res['tel']?></span> </div> </body> </html> <script> function edit(id){ location.href = 'user_add.php?id='+id; } function add(){ location.href = 'user_add.php'; } function del(id){ if(confirm('你确定要删除吗?')){ var request = new XMLHttpRequest(); request.onreadystatechange = function (){ if(request.readyState === 4 && request.status === 200){ var res = JSON.parse(request.responseText); var tips = document.getElementById('msg'); tips.innerHTML = res.message; if(res.status == 1){ tips.classList.add('success'); }else{ tips.classList.add('error'); } setTimeout(function(){ location.reload(); },1000); } } request.open('POST','./user_manage.php?action=del',true); request.setRequestHeader('content-type','application/x-www-form-urlencoded'); request.send('id='+id); }else{ return false; } } </script>
人员添加和编辑: user_add.php (添加和编辑同一个页面,有id传过来就是编辑反之添加)
<?php require "./is_session.php"; if(!empty($_GET['id'])){ $sql = "SELECT `id`,`name`,`age`,`course`,`mobile`,`status`"; $sql .= " FROM `staff` WHERE `id`=:id"; $stmt = $pdo->prepare($sql); $stmt->bindValue(':id',(int)$_GET['id'],PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); // echo '<pre>';print_r($row);die; } ?> <html> <head> <style> label{width:65px;display:block;float:left;padding-right:5px;} .box{width:320px;height:240px;background:#FFFAFA;margin:30px auto;} .success{color:green;} .error{color:red;} form{margin-left:30%;margin-top:30px;display:block;} </style> </head> <body> <form method="POST" name="user"> <input type="hidden" name="id" value="<?=isset($row['id'])?$row['id']:NULL;?>"> <p> <label>名称:</label> <input type="text" name="name" value="<?=isset($row['name'])?$row['name']:NULL;?>"> </p> <p> <label>年龄:</label> <input type="number" name="age" value="<?=isset($row['age'])?$row['age']:18;?>" min="18" max="200"> </p> <p> <label>课程:</label> <input type="text" name="course" value="<?=isset($row['course'])?$row['course']:NULL;?>"> </p> <p> <label>联系:</label> <input type="text" name="mobile" value="<?=isset($row['mobile'])?$row['mobile']:NULL;?>"> </p> <p> <label>显示:</label> <input type="radio" name="status" value="<?=isset($row['status'])&&($row['status']==1) ? $row['status'] : 1;?>" <?=isset($row['status'])&&($row['status']==1) ? 'checked' : '';?> > </p> <p> <label>隐藏:</label> <input type="radio" name="status" value="<?=isset($row['status'])&&($row['status']==0) ? $row['status'] : 0;?>" <?=isset($row['status'])&&($row['status']==0) ? 'checked' : '';?> > </p> <p> <button onclick="save(this.form);return false;" style="margin-left:65px;">保存</button> <button onclick="history.back();return false;" style="margin-left:65px;">返回</button> </p> <p style="margin-left:65px;"></p> </form> </body> </html> <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.innerHTML = ''; },false); } addEvent(user.name,tips,'请输入你的名字!'); addEvent(user.course,tips,'请输入你的课程!'); addEvent(user.mobile,tips,'请输入你的联系方式!'); function save(form){ var request = new XMLHttpRequest(); request.onreadystatechange = function (){ if(request.readyState === 4 && request.status === 200){ var res = JSON.parse(request.responseText); tips.innerHTML = res.message; if(res.status == 1){ tips.classList.add('success'); }else{ tips.classList.add('error'); } setTimeout(function(){ self.location = document.referrer; },1000); } } request.open('POST','./user_manage.php?action=edit',true); request.setRequestHeader('content-type','application/x-www-form-urlencoded'); var data = 'id='+form.id.value +'&name='+form.name.value +'&age='+form.age.value +'&course='+form.course.value +'&mobile='+form.mobile.value +'&status='+form.status.value; request.send(data); } </script>
添加,编辑,删除处理: user_manage.php
<?php require "./is_session.php"; $action = strtolower(trim($_GET['action'])); $id = (int)$_POST['id']; switch($action){ case 'edit': $name = trim($_POST['name']); $age = (int)$_POST['age']; $course = trim($_POST['course']); $mobile = trim($_POST['mobile']); $state = (int)$_POST['status']; if($id){ $sql = "UPDATE `staff` SET `name`=:name,`age`=:age,`course`=:course,"; $sql .= "`mobile`=:mobile,`status`=:state WHERE `id`=:id"; $stmt = $pdo->prepare($sql); $stmt->bindParam(':name',$name,PDO::PARAM_STR); $stmt->bindParam(':age',$age,PDO::PARAM_INT); $stmt->bindParam(':course',$course,PDO::PARAM_STR); $stmt->bindParam(':mobile',$mobile,PDO::PARAM_STR); $stmt->bindParam(':state',$state,PDO::PARAM_INT); $stmt->bindParam(':id',$id,PDO::PARAM_INT); if($stmt->execute()){ if($stmt->rowCount() == 1){ $status = 1; $message = '更新成功'; }else if($stmt->rowCount() == 0){ $status = 0; $message = '无效更新'; }else{ $status = -1; $message = '更新错误,请检查!'; } } }else{ $in = time(); $sql = "INSERT INTO `staff`(`name`,`age`,`course`,`mobile`,`intodate`,`status`)"; $sql .= " VALUES(:name,:age,:course,:mobile,:intodate,:state)"; $stmt = $pdo->prepare($sql); $stmt->bindValue(':name',$name,PDO::PARAM_STR); $stmt->bindValue(':age',$age,PDO::PARAM_INT); $stmt->bindValue(':course',$course,PDO::PARAM_STR); $stmt->bindValue(':mobile',$mobile,PDO::PARAM_STR); $stmt->bindValue(':intodate',$in,PDO::PARAM_STR); $stmt->bindValue(':state',$state,PDO::PARAM_INT); if($stmt->execute()){ if($stmt->rowCount() > 0){ $status = 1; $message = '新增成功'; }else if($stmt->rowCount() == 0){ $status = 0; $message = '无效新增'; }else{ $status = -1; $message = '新增错误,请检查!'; } } } exit(json_encode(['status'=>$status, 'message'=>$message])); break; case 'del': $sql = "DELETE FROM `staff` WHERE `id`=:id"; $stmt = $pdo->prepare($sql); $stmt->execute(['id'=>$id]); if($stmt->rowCount() > 0){ $status = 1; $message = "删除成功"; }else if($stmt->rowCount() == 0){ $status = 0; $message = "无效删除"; }else{ $status = 0; $message = "删除错误,请检查!"; } exit(json_encode(['status'=>$status, 'message'=>$message])); break; }
后台设置: system.php
<?php error_reporting(0); require "./is_session.php"; $stmt = $pdo->prepare("SELECT * FROM `system`"); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); $sql = "UPDATE `system` SET `sitename`=:name,`company`=:company,`tel`=:tel,`status`=:status WHERE `id`=:id"; $result = $pdo->prepare($sql); $result->bindValue(':name',$_POST['sitename'],PDO::PARAM_STR); $result->bindValue(':company',$_POST['company'],PDO::PARAM_STR); $result->bindValue(':tel',$_POST['tel'],PDO::PARAM_STR); $result->bindValue(':status',$_POST['status'],PDO::PARAM_INT); $result->bindValue(':id',$_POST['id'],PDO::PARAM_INT); $result->execute(); ?> <html> <head> <style> label{width:70px;display:block;float:left;padding-right:5px;} .box{width:320px;height:240px;background:#FFFAFA;margin:30px auto;} .success{color:green;} .error{color:red;} form{margin-left:30%;margin-top:30px;display:block;} </style> </head> <body> <form method="POST" action="<?=$_SERVER['PHP_SELF']?>"> <input type="hidden" name="id" value="<?=$row['id']?>"> <p> <label>网站名称:</label> <input type="text" name="sitename" value="<?=$row['sitename']?>"> </p> <p> <label>公司名称:</label> <input type="text" name="company" value="<?=$row['company']?>"> </p> <p> <label>公司电话:</label> <input type="text" name="tel" value="<?=$row['tel']?>"> </p> <p> <label>菜单显示:</label> <input type="radio" name="status" value="<?=($row['status']==1)?$row['status']:1?>" <?=($row['status']==1)?checked:''?> </p> <p> <label>菜单隐藏:</label> <input type="radio" name="status" value="<?=($row['status']==0)?$row['status']:0?>" <?=($row['status']==0)?checked:''?>> </p> <p> <button type="submit" style="margin-left:65px;">保存</button> </p> <p style="margin-left:65px;"></p> </form> </body> </html>
home.php登录后默认显示的页面: welcome.php
<html> <body bgcolor="#ccc"> <?php $sysinfo = array( '操作系统' => PHP_OS, '运行环境' => $_SERVER['SERVER_SOFTWARE'], 'PHP运行方式' => php_sapi_name(), '上传附件限制' => ini_get('upload_max_filesize'), '执行时间限制' => ini_get('max_execution_time').'秒', '服务器时间' => date("Y年n月j日 H:i:s"), '北京时间' => gmdate("Y年n月j日 H:i:s",time()+8*3600), '服务器域名/IP'=> $_SERVER['SERVER_NAME'].'['.gethostbyname($_SERVER['SERVER_NAME']).']', '剩余空间' => round((disk_free_space(".")/(1024*1024)),2).'M', ); echo '<pre>'; print_r($sysinfo); ?> </body> </html>
退出登录: logout.php
<?php session_start(); if($_SESSION['user_id'] && $_SESSION['username']){ session_destroy(); setcookie('PHPSESSID','',time()-3600,'/'); header('location:login.php'); }
MySQL结构 : php.sql
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for staff -- ---------------------------- DROP TABLE IF EXISTS `staff`; CREATE TABLE `staff` ( `id` int(5) unsigned NOT NULL AUTO_INCREMENT, `pid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '父id', `name` char(40) NOT NULL COMMENT '员工名字', `age` tinyint(3) unsigned NOT NULL COMMENT '年龄', `course` varchar(50) DEFAULT NULL COMMENT '职位', `mobile` char(11) DEFAULT NULL, `intodate` char(50) NOT NULL COMMENT '入职时间', `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '1:显示, 0隐藏', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=45 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of staff -- ---------------------------- INSERT INTO `staff` VALUES ('13', '0', 'Jhon', '23', 'HTML5', '14514962864', '1550915121', '1'); INSERT INTO `staff` VALUES ('14', '0', 'Jack', '23', 'PHP', '19514972867', '1550911289', '0'); INSERT INTO `staff` VALUES ('16', '0', 'Pack', '25', 'C语言', '15614972867', '1550911206', '1'); INSERT INTO `staff` VALUES ('17', '0', 'Jerry', '28', 'Java', '17514972867', '1550911253', '1'); INSERT INTO `staff` VALUES ('18', '0', 'Tom', '31', 'CSS3', '13632159165', '1551097759', '1'); INSERT INTO `staff` VALUES ('19', '0', 'Paul', '22', 'C语言', '13632159165', '1551097901', '0'); INSERT INTO `staff` VALUES ('20', '0', 'Amy', '21', 'PHP', '13632159165', '1551097981', '0'); INSERT INTO `staff` VALUES ('21', '0', 'Marr', '22', 'Java', '13665425832', '1551097981', '0'); INSERT INTO `staff` VALUES ('22', '0', 'Bill', '18', 'C#', '13665425832', '1551331796', '1'); INSERT INTO `staff` VALUES ('30', '0', 'Hony', '18', 'JavaEE', '0757-52124', '1551331796', '1'); INSERT INTO `staff` VALUES ('42', '0', 'Bird', '22', 'Python', '13912536525', '1551359668', '1'); INSERT INTO `staff` VALUES ('40', '0', 'Dog', '23', 'jQuery', '13625845624', '1551359566', '1'); INSERT INTO `staff` VALUES ('41', '0', 'Cat', '19', 'Javascript', '15836912362', '1551359592', '1'); INSERT INTO `staff` VALUES ('43', '0', 'Sheep', '30', 'C++', '13912536525', '1551359769', '0'); INSERT INTO `staff` VALUES ('44', '0', 'Monkey', '26', 'Vue', '13912536678', '1551359884', '1'); -- ---------------------------- -- Table structure for system -- ---------------------------- DROP TABLE IF EXISTS `system`; CREATE TABLE `system` ( `id` int(10) NOT NULL, `sitename` char(20) NOT NULL COMMENT '网站名称', `company` char(20) NOT NULL COMMENT '公司名称', `tel` char(20) NOT NULL COMMENT '公司电话', `status` tinyint(1) unsigned NOT NULL COMMENT '1显示 0隐藏', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of system -- ---------------------------- INSERT INTO `system` VALUES ('1', '资源管理', '某某科技公司', '0757-0662', '1'); -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `account` int(10) unsigned NOT NULL COMMENT '登录账号', `password` char(40) NOT NULL COMMENT '密码', `username` char(10) NOT NULL COMMENT '用户名称', `email` char(20) DEFAULT NULL COMMENT '邮箱', `status` tinyint(1) unsigned DEFAULT '1' COMMENT '1:正常 , 0:禁用', `addtime` char(50) NOT NULL COMMENT '账号添加时间', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=52 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', '147147', '601f1889667efaebb33b8c12572835da3f027f78', 'Admin', 'admin@155.com', '1', '1551335673'); INSERT INTO `user` VALUES ('2', '258258', '601f1889667efaebb33b8c12572835da3f027f78', 'Jerry', 'Jerry@133.com', '1', '1551335676'); INSERT INTO `user` VALUES ('36', '369369', '601f1889667efaebb33b8c12572835da3f027f78', 'Tom', 'Tom@142.com', '1', '1551335678');
效果图: