BBS项目(二)

  1 用户管理模块
  2 <?php
  3 // +----------------------------------------------------------------------
  4 // | 登录处理程序
  5 // +----------------------------------------------------------------------
  6 
  7 
  8     //开启session
  9     session_start();
 10     
 11     //引入公共配置
 12     require("../../public/config.php");
 13     
 14     if($_GET['a']=="login"){
 15         //判断验证码是否正确
 16         if($_POST['ucode']!=$_SESSION['code']){
 17             //验证码错误,弹出提示框并返回到登录界面
 18             echo "<script>alert('登录失败!验证码错误!');window.location.href='../login.php';</script>";
 19             die();
 20         }
 21     }
 22     //接收变量
 23     $uname=$_POST['uname'];
 24     $upass=md5($_POST['upass']);
 25     
 26     //判断用户名密码是否正确
 27     //1.链接数据库并判断
 28     $link=mysqli_connect(HOST,USER,PASS) or die("链接数据库失败!");
 29     
 30     //2.设置字符集
 31     mysqli_set_charset($link,CHARSET);
 32     
 33     //3.选择数据库
 34     mysqli_select_db($link,DBNAME);
 35     
 36     //4.定义sql语句并发送执行
 37     $sql="select * from user where userName='{$uname}' && password='{$upass}';";
 38     $result=mysqli_query($link,$sql);
 39     
 40     //5.解析结果集
 41     if($result && mysqli_num_rows($result)>0 ){
 42         $row=mysqli_fetch_assoc($result);
 43         
 44             //判断用户状态
 45             if($row['status']!=1){
 46                 echo "<script>alert('该账号已被冻结!');window.location.href='../login.php';</script>";
 47                 die();
 48             }
 49         $_SESSION['id']=$row['id'];    //存前台登录用户id的Session['id'] 
 50         
 51         $time=time();
 52         $sql="update user set lastlogin={$time} where id={$_SESSION['id']}";
 53         mysqli_query($link,$sql);
 54         
 55         echo "<script>alert('登录成功!');window.location.href='../index.php';</script>";
 56     }else{
 57         echo "<script>alert('登录失败!账号或密码错误!');window.location.href='../login.php';</script>";
 58         die();
 59     }
 60     
 61     //6.释放结果集,关闭数据库
 62     mysqli_free_result($result);
 63     mysqli_close($link);
 64     
 65 ?>
 66 
 67 
 68 <?php
 69 // +----------------------------------------------------------------------
 70 // | 注册、修改处理程序
 71 // +----------------------------------------------------------------------
 72 
 73     
 74     //引入公共配置
 75     session_start();
 76     require("../../public/config.php");
 77     
 78     //1.链接数据库并判断
 79     $link=mysqli_connect(HOST,USER,PASS) or die("链接数据库失败!");
 80     
 81     //2.设置字符集
 82     mysqli_set_charset($link,CHARSET);
 83     
 84     //3.选择数据库
 85     mysqli_select_db($link,DBNAME);
 86     
 87     switch($_GET['action']){
 88         
 89         //注册用户
 90         case "insert":    
 91             
 92             //判断验证码是否正确,防止恶意注册
 93             if($_POST['ucode']!=$_SESSION['code']){
 94                 //验证码错误,弹出提示框并返回到登录界面
 95                 echo "<script>alert('注册失败!验证码错误!');window.location.href='../register.php';</script>";
 96                 die();
 97             }
 98             
 99             //判断密码输入是否正确
100             if($_POST['upass']!=$_POST['upass1'] || empty($_POST['upass']) || empty($_POST['upass1'])){
101                 echo "<script>alert('密码不正确!');window.location.href='../register.php';</script>";
102             }
103             
104             //获取用户名,密码,邮箱
105             $userName=$_POST['uname'];
106             $password=md5($_POST['upass']);
107             $email=$_POST['uemail'];
108             
109             //4.定义sql语句,并发送执行
110             $sql="insert user(userName,password,auth) values('{$userName}','{$password}',0)";
111             $result=mysqli_query($link,$sql);
112             
113             //5.判断插入数据是否成功
114             if($result && mysqli_affected_rows($link)>0){
115                 
116                 $uid= mysqli_insert_id($link);
117                 //向用户资料表中插入数据
118                 $sql="insert userdetail(uid,email) values ({$uid},'{$email}')";
119                 mysqli_query($link,$sql);
120                 
121                 echo "<script>alert('新增用户成功!');window.location.href='../login.php';</script>";
122             }else{
123                 echo "<script>alert('新增用户失败!账号已存在');window.location.href='./main_info.php';</script>";
124             }
125         break;
126         
127         //修改个人信息
128         case "update":
129             
130             //存储用户信息的数组
131             $set=array();
132             $uid=$_SESSION['id'];    //要修改的用户uid
133             
134             if(isset($_POST['unickName'])){
135                 $set[]="nickName='{$_POST['unickName']}'";
136             }
137             if(isset($_POST['uemail'])){
138                 $set[]="email='{$_POST['uemail']}'";
139             }
140             if(isset($_POST['uqq'])){
141                 $set[]="qq='{$_POST['uqq']}'";
142             }
143             if(isset($_POST['usex'])){
144                 $set[]="sex='{$_POST['usex']}'";
145             }
146             
147             //拼接插入数据
148             $info="";
149             $info=implode(",",$set);
150             
151             
152             //4.定义sql语句发送并执行
153             $sql="update userdetail set {$info} where uid={$uid}";
154             $result=mysqli_query($link,$sql);
155             
156             //5.判断是否修改成功
157             if($result && mysqli_affected_rows($link)>0){
158                 echo "<script>alert('修改成功!');window.location.href='../personInfo.php';</script>";
159             }else{
160                 echo "<script>alert('修改失败!');window.location.href='../personInfo.php';</script>";
161             }
162             
163         break;
164         
165     }
166 
167     
168     //6.关闭数据库
169     170     mysqli_close($link);
171 ?>
172 
173 
174 
175 
176 // +----------------------------------------------------------------------
177 // | 遍历搜索分页
178 // +----------------------------------------------------------------------
179 
180 <html>
181 <head>
182 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
183 <title>主要内容区main</title>
184 <link href="../../public/admin/css/css.css" type="text/css" rel="stylesheet" />
185 <link href="../../public/admin/css/main.css" type="text/css" rel="stylesheet" />
186 <link rel="shortcut icon" href="images/main/favicon.ico" />
187 <style>
188 body{overflow-x:hidden; background:#f2f0f5; padding:15px 0px 10px 5px;}
189 #searchmain{ font-size:12px;}
190 #search{ font-size:12px; background:#548fc9; margin:10px 10px 0 0; display:inline; 100%; color:#FFF; float:left}
191 #search form span{height:40px; line-height:40px; padding:0 0px 0 10px; float:left;}
192 #search form input.text-word{height:24px; line-height:24px; 180px; margin:8px 0 6px 0; padding:0 0px 0 10px; float:left; border:1px solid #FFF;}
193 #search form input.text-but{height:24px; line-height:24px; 55px; background:url(images/main/list_input.jpg) no-repeat left top; border:none; cursor:pointer; font-family:"Microsoft YaHei","Tahoma","Arial",'宋体'; color:#666; float:left; margin:8px 0 0 6px; display:inline;}
194 #search a.add{ background:url(images/main/add.jpg) no-repeat -3px 7px #548fc9; padding:0 10px 0 26px; height:40px; line-height:40px; font-size:14px; font-weight:bold; color:#FFF; float:right}
195 #search a:hover.add{ text-decoration:underline; color:#d2e9ff;}
196 #main-tab{ border:1px solid #eaeaea; background:#FFF; font-size:12px;}
197 #main-tab th{ font-size:12px; background:url(images/main/list_bg.jpg) repeat-x; height:32px; line-height:32px;}
198 #main-tab td{ font-size:12px; line-height:40px;}
199 #main-tab td a{ font-size:12px; color:#548fc9;}
200 #main-tab td a:hover{color:#565656; text-decoration:underline;}
201 .bordertop{ border-top:1px solid #ebebeb}
202 .borderright{ border-right:1px solid #ebebeb}
203 .borderbottom{ border-bottom:1px solid #ebebeb}
204 .borderleft{ border-left:1px solid #ebebeb}
205 .gray{ color:#dbdbdb;}
206 td.fenye{ padding:10px 0 0 0; text-align:right;}
207 .bggray{ background:#f9f9f9}
208 </style>
209 </head>
210 <body>
211 <!--main_top-->
212 <table width="99%" border="0" cellspacing="0" cellpadding="0" id="searchmain">
213   <tr>
214     <td width="99%" align="left" valign="top">您的位置:用户管理&nbsp;&nbsp;>&nbsp;&nbsp;浏览用户</td>
215   </tr>
216   <tr>
217     <td align="left" valign="top">
218     <table width="100%" border="0" cellspacing="0" cellpadding="0" id="search">
219           <tr>
220             <td width="90%" align="left" valign="middle">
221              <form method="get" action="main_list.php">
222              <span>用户账号:</span>
223              <input type="text" name="uname" value="" class="text-word">
224              <input name="" type="submit" value="查询" class="text-but">
225              </form>
226          </td>
227             <td width="10%" align="center" valign="middle" style="text-align:right; 150px;"><a href="main_info.php" target="mainFrame" onFocus="this.blur()" class="add">新增用户</a></td>
228           </tr>
229     </table>
230     </td>
231   </tr>
232   <tr>
233     <td align="left" valign="top">
234     
235     <table width="100%" border="0" cellspacing="0" cellpadding="0" id="main-tab">
236       <tr>
237         <th align="center" valign="middle" class="borderright">编号</th>
238         <th align="center" valign="middle" class="borderright">管理帐号</th>
239         <th align="center" valign="middle" class="borderright">权限</th>
240         <th align="center" valign="middle" class="borderright">锁定</th>
241         <th align="center" valign="middle" class="borderright">最后登录</th>
242         <th align="center" valign="middle">操作</th>
243       </tr>
244           <?php
245         //遍历数据库用户表显示所有用户信息
246         
247         //引入公共配置
248         require("../../public/config.php");
249         
250         
251         //1.链接数据库并判断
252         $link=mysqli_connect(HOST,USER,PASS) or die("链接数据库失败!");
253         
254         //2.设置字符集
255         mysqli_set_charset($link,CHARSET);
256         
257         //3.选择数据库
258         mysqli_select_db($link,DBNAME);
259         
260         //=========================搜索代码==============================
261             
262             //定义一个存储搜索条件的变量
263             $whereList = array();
264             $urlList = array();    //用来维持条件的数组
265             
266             //判断你有没有搜索姓名
267             if(!empty($_GET['uname'])){
268                 $whereList[] = " userName like '%{$_GET['uname']}%'";
269                 $urlList[] = "uname={$_GET['uname']}";
270             }
271             
272             //定义一个存放where语句的变量
273                 $where = "";
274                 $url = "";    //存放url地址条件的变量
275                 
276             //拼装where语句
277                 if(count($whereList)>0){
278                     $where = " where ".implode("&&",$whereList);
279                     $url = "&".implode("&",$urlList);
280                 }
281         
282         //===============================================================
283         
284         
285         //===============分页代码========================================    
286             //判断分页
287             $page=isset($_GET['page'])?$_GET['page']:1;    //当前页
288             $pageSize=6;//页大小
289             $maxPage=0;    //总页数
290             $maxRows=0;    //数据总条数
291             
292             
293             
294             $sql="select * from user".$where;
295             
296             $result=mysqli_query($link,$sql);
297             //得到数据总条数
298             $maxRows=mysqli_num_rows($result);
299             
300             //得到总页数
301             $maxPage=ceil($maxRows/$pageSize);
302             
303             if($page<1){
304                 $page=1;
305             }
306             
307             if($page>$maxPage){
308                 $page=$maxPage;
309             }
310         
311             $limit = " limit ".(($page-1)*$pageSize).",".$pageSize;
312             
313             
314         //============================================================================
315         
316         //4.定义sql语句并发送执行
317         $sql="select * from user ".$where.$limit;
318         $result=mysqli_query($link,$sql);
319         
320         //5.解析结果集
321         if($result && mysqli_num_rows($result)>0){
322             
323             
324             while($rows=mysqli_fetch_assoc($result)){
325                 $time=date("Y-m-d H:i:s",$rows['lastlogin']);
326     ?>    
327     <?php
328         if($rows['id']%2==1){
329     ?>
330         <tr  onMouseOut="this.style.backgroundColor='#ffffff'" onMouseOver="this.style.backgroundColor='#edf5ff'">
331     <?php
332     }else{
333     ?>
334         <tr class="bggray" onMouseOut="this.style.backgroundColor='#f9f9f9'" onMouseOver="this.style.backgroundColor='#edf5ff'">
335     
336     <?php            
337         }
338     ?>
339     
340     
341         <td align="center" valign="middle" class="borderright borderbottom"><?php echo $rows['id'] ?></td>
342         <td align="center" valign="middle" class="borderright borderbottom"><?php echo $rows['userName'] ?></td>
343         <td align="center" valign="middle" class="borderright borderbottom"><?php echo $rows['auth']?'超级管理员':'普通用户'; ?></td>
344         <td align="center" valign="middle" class="borderright borderbottom"><?php echo $rows['status']?'开启':'禁用'; ?></td>
345         <td align="center" valign="middle" class="borderright borderbottom"><?php echo $time ?></td>
346         <td align="center" valign="middle" class="borderbottom"><a href="doEdit.php?uid=<?php echo $rows['id'] ?>" target="mainFrame" onFocus="this.blur()" class="add">编辑</a><span class="gray">&nbsp;|&nbsp;</span><a href="doAction.php?uid=<?php echo $rows['id']?>&action=delete" target="mainFrame" onFocus="this.blur()" class="add">删除</a><span class="gray">&nbsp;|&nbsp;</span><a href="doAction.php?uid=<?php echo $rows['id']?>&action=status" target="mainFrame" onFocus="this.blur()" class="add">禁用</a></td>
347       </tr>
348     <?php        
349             }
350         }
351     ?>
352     </table></td>
353     </tr>
354   <tr>
355     <td align="left" valign="top" class="fenye"><?php echo $maxRows ?>条数据 <?php echo $page ?>/<?php echo $maxPage ?> 页&nbsp;&nbsp;<a href="main_list.php?page=1<?php echo $url?>" target="mainFrame" onFocus="this.blur()">首页</a>&nbsp;&nbsp;<a href="main_list.php?page=<?php echo ($page-1).$url ?>" target="mainFrame" onFocus="this.blur()">上一页</a>&nbsp;&nbsp;<a href="main_list.php?page=<?php echo ($page+1).$url ?>" target="mainFrame" onFocus="this.blur()">下一页</a>&nbsp;&nbsp;<a href="main_list.php?page=<?php echo $maxPage.$url ?>" target="mainFrame" onFocus="this.blur()">末页</a></td>
356   </tr>
357   <?php
358         //6.释放结果集,关闭数据库
359         mysqli_free_result($result);
360         mysqli_close($link);
361         
362   ?>
363 </table>
364 </body>
365 </html>
原文地址:https://www.cnblogs.com/yexiang520/p/5585979.html