当前位置: > 手游资讯 > 新手问答 

申请聊天室(php小项目:聊天室(注册,登录,聊天==))

作者:哪吒游戏网

实现功能:

用户登录用户注册用户添加留言:可选择留言分类(建议、反馈、投诉)用户查看留言:可根据上述分类对留言信息进行筛选留言翻页:每页显示10条留言编辑:可以编辑自己的留言信息留言删除:可以删除自己的留言信息

注册:(register.php)

 1 DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4   <meta charset="utf-8">
 5   <title>particles.jstitle>
 6   <meta name="description" content="particles.js is a lightweight JavaScript library for creating particles.">
 7   <meta name="author" content="Vincent Garreau" />
 8   <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
 9   <link rel="stylesheet" media="screen" href="demo/css/style.css">
10   <style type="text/css">
11 style>
12 head>
13 <body>
14 
15 
16 <div class="count-particles">
17   <span class="js-count-particles">--span> particles
18 div>
19 
20 
21 <div id="particles-js" style="position: absolute;top: 0;width: 100%; background-color: #000" align="center">
22     <table width="360px" border="1" align="center" style="background: url(素材/回形针.png); z-index: -1" >
23     <form method="post" action="reg_do.php" enctype="multipart/form-data">
24         <tr>
25             <td align="center" colspan="2" style="font-size: 36px">用户注册td>
26         tr>
27         <tr>
28             <td>用户名:td>
29             <td><input type="text" name="username" id="username">td>
30         tr>
31         <tr>
32             <td>用户昵称:td>
33             <td><input type="text" name="nickname" id="nickname">td>
34         tr>
35         <tr>
36             <td>用户密码:td>
37             <td><input type="password" name="password" id="password">td>
38         tr>
39         <tr>
40             <td>重复密码:td>
41             <td><input type="password" name="repeatpassword" id="repeatpassword">td>
42         tr>
43         <tr>
44             <td>用户头像:td>
45             <td>
46                 
47                 <input type="file" name="profile" id="profile" >
48             td>
49         tr>
50         <tr>
51             <td colspan="2" align="center"><input type="submit" name="register" id="register">td>
52         tr>
53     form>
54     table>
55 div>
56 
57 <script src="particles.js">script>
58 <script src="demo/js/app.js">script>
59 
60 
61 <script src="demo/js/lib/stats.js">script>
62 <script src="https://cdn.bootcss.com/jquery/1.12.2/jquery.js">script>
63 <script>
64   var count_particles, stats, update;
65   stats = new Stats;
66   stats.setMode(0);
67   stats.domElement.style.position = 'absolute';
68   stats.domElement.style.left = '0px';
69   stats.domElement.style.top = '0px';
70   document.body.appendChild(stats.domElement);
71   count_particles = document.querySelector('.js-count-particles');
72   update = function() {
73     stats.begin();
74     stats.end();
75     if (window.pJSDom[0].pJS.particles && window.pJSDom[0].pJS.particles.array) {
76       count_particles.innerText = window.pJSDom[0].pJS.particles.array.length;
77     }
78     requestAnimationFrame(update);
79   };
80   requestAnimationFrame(update);
81 
82 
83 script>
84 
85 body>
86 html>

我这里加入了particles-js的粒子特效,效果如下(用table做这个有bug申请聊天室,以后还是不用table+form了)

是有动态效果的,很炫酷,由于动态图可能有bug,这里我上传的.png

申请聊天室

贯穿全剧的Config(配置)文件:

1 <?php 
2     $db = mysqli_connect('127.0.0.1', 'root', 'password', 'guestattrs');
3     session_start();
4     include 'lab/Uploads.php';
5     $upload_dir = 'uploads/'
6 ?>

注册对应的判断注册输入信息是否合法,合法就入库(reg_do.php)

  1 <?php include_once('config.php') ?>
  2 
  3 
  4 
  5     
  6 
  7 <body>
  8     <?php
  9         $username = $_POST['username'];
 10         $nickname = $_POST['nickname'];
 11         $password = $_POST['password'];
 12         $repeatpassword = $_POST['repeatpassword'];
 13 
 14         if (!$username)
 15         {
 16             exit('<script>alert("请填写用户名! ")
 17             history.back();</script>');
 18         }
 19         if (!$nickname)
 20         {
 21             exit('<script>alert("请填写用户昵称! ")
 22             history.back();</script>');
 23         }
 24         if (!$password)
 25         {
 26             exit('<script>alert("请填写密码! ")
 27             history.back();</script>');
 28         }
 29         if (!$repeatpassword)
 30         {
 31             exit('<script>alert("请填写重复密码! ")
 32             history.back();</script>');
 33         }
 34         //同时判断其他的参数是否填写
 35 
 36         //判断该用户名是否存在
 37         $sql = "select * from userlist where username = '" . $username . "' ";
 38         $query = mysqli_query($db, $sql);
 39         if (!$query)
 40         {
 41             exit('SQL语句执行错误:1 ' . mysqli_error($db));
 42         }
 43         if ($info = mysqli_fetch_array($query))
 44         {
 45             exit('<script>alert("该用户名已存在, 请重新填写!");history.back();</script>');
 46         }
 47 
 48         //判断密码是否一致
 49         if ($password != $repeatpassword)
 50         {
 51             die('<script>alert("两次填写密码不一致,请重写填写");history.back()</script>');
 52         }
 53 
 54         #deal with profile
 55         $profile = $_FILES['profile'];
 56         $upload = new Uploads($profile, $upload_dir, array('.png', '.jpg', '.gif'), 2 * 1024 * 1024 * 8);//2M
 57         $returnInfo = $upload->uploadFile();
 58         $newfilename = $upload->newfiledir;
 59         print_r($returnInfo);
 60         if ($returnInfo['status'] == 0)
 61             die($returnInfo['msg']);
 62         // $error = $profile['error'];
 63         // switch ($error) {
 64         //     case 0:
 65         //         #$profileName = $profile['name'];
 66         //         #echo "您的个人相片为: ". $profileName. "
";
67 // $profileTemp = $profile['tmp_name']; 68 // $ext = substr($profile['name'], strrpos($profile['name'], '.')); 69 // $newfilename = date('YMDHis'). rand(1000, 9999) . $ext; 70 // $destination = "Picture/"; 71 // $filedir = $destination . date('Y') . '/' . date('m') . '/'; 72 // if (!is_dir($filedir)) 73 // { 74 // $dirs = explode('/', trim($filedir, '/')); 75 // print_r($dirs); 76 // $newdir = ''; 77 // foreach($dirs as $v) 78 // { 79 // $newdir = $newdir . $v . '/'; 80 // if (!is_dir($newdir)) 81 // { 82 // mkdir($newdir); 83 // } 84 // } 85 // } 86 // $newfilename = $filedir . $newfilename; 87 // move_uploaded_file($profileTemp, $newfilename); 88 // //echo "文件上传成功!
";
89 // break; 90 // case 1: 91 // echo "上传的头像超过了php.ini中upload_max_filesize选项限制的值!
";
92 // break; 93 // case 2: 94 // echo "上传文件的头像超过了FORM表单MAX_FILE_SIZE选项指定的值!
";
95 // break; 96 // case 3: 97 // echo "文件只有部分被上传!
";
98 // break; 99 // case 4: 100 // echo "没有选择上传文件!
";
101 // break; 102 // } 103 104 //执行注册 105 $sql = "insert into userlist(username, nickname, password, regtime, regip, headimg) values('" . $username . "', '". $nickname . "', '". md5($password) . 106 "'," . time() . ",'" . $_SERVER['REMOTE_ADDR'] . "','" . $newfilename . "')"; 107 $query = mysqli_query($db, $sql); 108 if (!$query) 109 { 110 exit('SQL语句执行错误:' . mysqli_error($db)); 111 } 112 else 113 { 114 exit('<script>alert("注册成功,请登录");window.location.href = "sign_in.php"</script>'); 115 } 116 ?> 117 118

登录:

 1 DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5     <title>用户登录title>
 6 head>
 7 <body background="素材/方格.jpg" style=" background-repeat: no-repeat; background-size: 100% 100%; background-attachment: fixed;">
 8     <table width="360px" border="1" align="center">
 9     <form method="post" action="sign_do.php">
10         <tr>
11             <td align="center" colspan="2" style="font-size: 36px">用户登录td>
12         tr>
13         <tr>
14             <td>用户名:td>
15             <td><input type="text" name="username" id="username">td>
16         tr>
17         <tr>
18             <td>用户密码:td>
19             <td><input type="password" name="password" id="password">td>
20         tr>
21         <tr>
22             <td colspan="2" align="center"><input type="submit" name="register" id="register">如果您没有账号,请<a href="register.php">注册a>td>
23         tr>
24     form>
25     table>
26 body>
27 html>

登录信息是否正确判断,正确则进入聊天室

 1 <?php include_once('config.php') ?>
 2 
 3 
 4 
 5     
 6 
 7 <body>
 8     <?php  
 9         $username = $_POST['username'];
10         $password = $_POST['password'];
11         if (!$username)
12         {
13             exit('<script>alert("请填写用户名! ")
14             history.back();</script>');
15         }
16         if (!$password)
17         {    
18             exit('<script>alert("请填写密码! ")
19             history.back();</script>');
20         }
21         
22         //若用户名正确,判断密码是否正确
23         $sql = "select * from userlist where username = '" . $username ."' and password = '" . md5($password) . "' ";
24         $query = mysqli_query($db, $sql);
25         if (!$query)
26         {
27             exit('SQL语句执行错误: ' . mysqli_error($db));
28         }
29         if (!$info = mysqli_fetch_array($query))
30         {
31             exit('<script>alert("密码不正确, 请重新填写!");history.back();</script>');
32         }
33         
34         $_SESSION['uid'] = $info['uid'];
35         $_SESSION['nickname'] = $info['nickname'];
36         die('<script>alert("登录成功");window.location.href="chat.php"</script>')
37     ?>
38 
39 

聊天室(留言分为了三类,建议申请聊天室,反馈,投诉,三类分别有对应的php后台处理)

注:由于是后面才加的分类,所以对没有修改以前直接提交的form表单

先是判断是否登录:

1 <?php 
2     if (!$_SESSION['uid'])
3     {
4         die('<script>alert("未登录,请登录");window.location.href = "sign_in.php";</script>');
5     }
6  ?>

聊天室:

  1 <?php
  2     include_once('config.php');
  3     include_once('if_register.php');
  4 ?>
  5 
  6 
  7     
  8     
  9 
 10 
 16 
 17 
 18 <body background="素材/回形针.png" style=" background-repeat:no-repeat; background-size:100% 100%; background-attachment: fixed;" >    
 19 
 20     <?php
 21         $name = $_SESSION['nickname'];
 22         $sql = "select headimg from userlist where nickname = '" . $name ."' ";
 23         $query = mysqli_query($db, $sql);
 24         if (!$query)
 25             die ('sql语句执行错误'. mysqli_error($db));
 26         else
 27             $info = mysqli_fetch_array($query);
 28         // print_r($info);
 29         // print_r($info['headimg']);
 30     ?>
 31     

32 <?php echo "欢迎您,亲爱的" . $name; ?> 33 申请聊天室$info['headimg']; ?>" style=" width: 50px; height: 50px; " /> 34 注销 35

36
37 38 39 40 42 43 44 45 46 51 52 53 54 55 56 62 63 64 65 <?php 66if (isset($_GET['comment_type'])) 67$comment_type = $_GET['comment_type']; 68else 69$comment_type = 'chat'; 70 71if (isset($_GET['page'])) 72 { 73$page = $_GET['page']; 74$page_start = ($page - 1) * 10; 75 } 76else 77 { 78$page = 1; 79$page_start = 0; 80 } 81 82// print_r($_SERVER['QUERY_STRING']); 83$parameters = $_GET; 84$url = $comment_type . '.php?'; 85foreach ($parametersas$key => $value) 86 { 87if ($key != 'page') 88$url .= $key . '=' . $value . '&'; 89 } 90$url .= 'page='; 91 92// 评论的总条数 93$total_chat = 0; 94$sql = "select $comment_type.*, userlist.nickname, userlist.headimg 95 from $comment_type left join userlist on $comment_type.uid = userlist.uid";// limit 1 96$query = mysqli_query($db, $sql); 97if (!$query) 98die ('sql语句执行错误'. mysqli_error($db)); 99while ($info = mysqli_fetch_array($query)) 100$total_chat++; 101102$sql = "select $comment_type.*, userlist.nickname, userlist.headimg 103 from $comment_type left join userlist on $comment_type.uid = userlist.uid 104 order by $comment_type.addtime desc limit " . $page_start . ", 10";// limit 1105$query = mysqli_query($db, $sql); 106if (!$query) 107die ('sql语句执行错误'. mysqli_error($db)); 108while ($info = mysqli_fetch_array($query)) 109 { 110// print_r($info); 111 // echo "
;";
112 // $sql = "select username from userlist where uid = '" . $info['uid'] . "'"; 113 // $query2 = mysqli_query($db, $sql); 114 // if (!$query2) 115 // die ('sql语句执行错误'. mysqli_error($db)); 116 // $info2 = mysqli_fetch_array($query2);117 ?> 118119125128147148 <?php 149 } 150 ?> 151152
41 添加留言 申请聊天室
留言: 47 建议 48 反馈 49 投诉 50
57 您想查看什么: 58 建议 59 反馈 60 投诉 61
120 申请聊天室$info['headimg']; ?>" 121 style=" width: 50px; height: 50px;" /> 122 123 <?php echo $info['nickname']; ?>  say: 124 126 <?php echo $info['comment']; ?> 127 129

130 <?php 131 echo date("Y年m月d日h小时i分钟s秒", $info['addtime']); 132 // echo strtotime(date("Ymdhis", $info['addtime'])); 133 ?> 134 <?php 135 print_r($_SESSION['uid']); 136 if ($_SESSION['uid'] == $info['uid']) 137 { 138 ?> 139
140 $info['id']; ?>">编辑 141 $info['id']; ?>">删除 142 <?php 143 } 144 ?> 145

146
153 <?php 154 //查询评论的总条数 155 // $sql = "select count(*) from chat"; 156 // $total_chat = mysqli_query($db, $sql); 157 // // print_r($total_chat); 158 // $count = mysqli_fetch_array($total_chat); 159 // print_r($count); 160 161 // print_r($total_chat / 10); 162 // if (!$total_chat) 163 // die('sql语句执行错误' . mysqli_error($db)); 164 ?> 165
  • class="list"> 166 167 <?php 168 if ($page > 1) 169 echo $url . ($page - 1); 170 else 171 echo $url . $page; 172 ?> 173 >上一页 174 <?php 175 $offset = 1; 176 $total_page = ceil($total_chat / 10); 177 for ($i = $page - $offset; $i <= $page + $offset; $i++) 178 { 179 if ($i != $page && $i > 0 && $i <= $total_page) 180 { 181 ?> 182 echo $url . $i; ?> > <?php echo $i;?> 183 <?php 184 } 185 else if ($i == $page) 186 { 187 ?> 188 <?php echo $page;?> 189 <?php 190 } 191 } 192 ?> 193 194 <?php 195 if ($page < $total_page) 196 echo $url . ($page + 1); 197 else 198 echo $url . $page; 199 ?> 200 >下一页 201 echo $url . $total_page;?> >尾页 202
  • 203 204 205 206 207
    208 209

    对Suggest(建议)的处理(分成两层,先进入建议的留言页面,然后第二层进行入库处理):

     1 <?php
     2     include_once('config.php');
     3     include_once('if_register.php');
     4 ?>
     5 
     6 
     7     
     8     <style type="text/css">
     9 
    10     </style>
    11     
    12 
    13 <body background="素材/回形针.png" style=" background-repeat:no-repeat; background-size:100% 100%; background-attachment: fixed;" >    
    14     <?php
    15         $name = $_SESSION['nickname'];
    16     ?>
    17     

    18 <?php echo "欢迎您,亲爱的" . $name; ?> 19 注销 20 <?php 21 ?> 22

    23
    24 2526272930313233343536394041

     1 <?php
     2     include_once('config.php');
     3     include_once('if_register.php');
     4 ?>
     5 
     6 
     7 
     8     
     9 
    10 <body>
    11     <?php
    12         $comment = $_POST['comment'];
    13         $uid = $_SESSION['uid'];
    14         $addtime = time();
    15         $active = 0;
    16         $ip = $_SERVER['REMOTE_ADDR'];
    17         if (!$comment)
    18         {
    19             die('<script>alert("建议不能为空"); history.back();</script>');
    20         }
    21         $sql = "insert into chat(comment, uid, addtime, active, ip) values ('" . $comment . "','" . $uid . "','" . $addtime . "','" . $active . "','" . $ip . "')";
    22         $query = mysqli_query($db, $sql);
    23         if (!$query)
    24         {
    25             die('SQL语句执行错误'. mysqli_error($db));
    26         }
    27         else
    28         {
    29             die('<script>alert("建议成功");window.location.href = "chat.php"</script>');
    30         }
    31     ?>
    32 
    33 

    对FeedBack(反馈)的处理(分成两层,先进入反馈的留言页面,然后第二层进行入库处理):

     1 <?php
     2     include_once('config.php');
     3     include_once('if_register.php');
     4 ?>
     5 
     6 
     7     
     8     <style type="text/css">
     9 
    10     </style>
    11     
    12 
    13 <body background="素材/回形针.png" style=" background-repeat:no-repeat; background-size:100% 100%; background-attachment: fixed;" >    
    14     <?php
    15         $name = $_SESSION['nickname'];
    16     ?>
    17     

    18 <?php echo "欢迎您,亲爱的" . $name; ?> 19 注销 20 <?php 21 ?> 22

    23
    24
    28 添加建议 申请聊天室
    建议 :
    37 38
    2526272930313233343536394041

     1 <?php
     2     include_once('config.php');
     3     include_once('if_register.php');
     4 ?>
     5 
     6 
     7 
     8     
     9 
    10 <body>
    11     <?php
    12         $comment = $_POST['comment'];
    13         $uid = $_SESSION['uid'];
    14         $addtime = time();
    15         $active = 0;
    16         $ip = $_SERVER['REMOTE_ADDR'];
    17         if (!$comment)
    18         {
    19             die('<script>alert("反馈不能为空"); history.back();</script>');
    20         }
    21         $sql = "insert into feedback(comment, uid, addtime, active, ip) values ('" . $comment . "','" . $uid . "','" . $addtime . "','" . $active . "','" . $ip . "')";
    22         $query = mysqli_query($db, $sql);
    23         if (!$query)
    24         {
    25             die('SQL语句执行错误'. mysqli_error($db));
    26         }
    27         else
    28         {
    29             die('<script>alert("反馈成功");window.location.href = "chat.php"</script>');
    30         }
    31     ?>
    32 
    33 

    对Complaint(投诉)的处理(分成两层,先进入投诉的留言页面,然后第二层进行入库处理):

     1 <?php
     2     include_once('config.php');
     3     include_once('if_register.php');
     4 ?>
     5 
     6 
     7     
     8     <style type="text/css">
     9 
    10     </style>
    11     
    12 
    13 <body background="素材/回形针.png" style=" background-repeat:no-repeat; background-size:100% 100%; background-attachment: fixed;" >    
    14     <?php
    15         $name = $_SESSION['nickname'];
    16     ?>
    17     

    18 <?php echo "欢迎您,亲爱的" . $name; ?> 19 注销 20 <?php 21 ?> 22

    23
    24
    28 添加反馈 申请聊天室
    反馈 :
    37 38
    2526272930313233343536394041

     1 <?php
     2     include_once('config.php');
     3     include_once('if_register.php');
     4 ?>
     5 
     6 
     7 
     8     
     9 
    10 <body>
    11     <?php
    12         $comment = $_POST['comment'];
    13         $uid = $_SESSION['uid'];
    14         $addtime = time();
    15         $active = 0;
    16         $ip = $_SERVER['REMOTE_ADDR'];
    17         if (!$comment)
    18         {
    19             die('<script>alert("投诉不能为空"); history.back();</script>');
    20         }
    21         $sql = "insert into complaint(comment, uid, addtime, active, ip) values ('" . $comment . "','" . $uid . "','" . $addtime . "','" . $active . "','" . $ip . "')";
    22         $query = mysqli_query($db, $sql);
    23         if (!$query)
    24         {
    25             die('SQL语句执行错误'. mysqli_error($db));
    26         }
    27         else
    28         {
    29             die('<script>alert("投诉成功");window.location.href = "chat.php"</script>');
    30         }
    31     ?>
    32 
    33 

    编辑留言:

     1 <?php
     2     include_once('config.php');
     3     include_once('if_register.php');
     4 ?>
     5 
     6 
     7     
     8     <style type="text/css">
     9 
    10     </style>
    11     
    12 
    13 <body background="素材/回形针.png" style=" background-repeat:no-repeat; background-size:100% 100%; background-attachment: fixed;" >    
    14     <?php
    15         $name = $_SESSION['nickname'];
    16         $sql = "select headimg from userlist where nickname = '" . $name ."' ";
    17         $query = mysqli_query($db, $sql);
    18         if (!$query)
    19             die ('sql语句执行错误'. mysqli_error($db));
    20         else
    21             $info = mysqli_fetch_array($query);
    22         // print_r($info);
    23         // print_r($info['headimg']);
    24     ?>
    25     

    26 <?php echo "欢迎您,亲爱的" . $name; ?> 27 申请聊天室$info['headimg']; ?>" style=" width: 50px; height: 50px; " /> 28 注销 29 <?php 30 $id = intval($_GET['id']); 31 //判断id是否是正确的 32 if ($id <= 0) 33 die('<script>alert("参数错误");history.back();</script>'); 34 //判断id是否是当前登录用户的:根据id号和登陆者的会话uid进行判断 35 $sql = "select * from chat where id = " . $id . " and uid = " . $_SESSION['uid']; 36 //将查询出来的信息放到comment的值里面 37 $query = mysqli_query($db, $sql); 38 if (!$query) 39 die ('sql语句执行错误'. mysqli_error($db)); 40 $info = mysqli_fetch_array($query); 41 if (!$info) 42 { 43 die('<script>alert("参数错误");history.back();</script>'); 44 } 45 ?> 46

    47
    48
    28 添加投诉 申请聊天室
    投诉 :
    37 38
    4950515354555657585960646566

     1 <?php
     2     include_once('config.php');
     3     include_once('if_register.php');
     4 ?>
     5 
     6 
     7 
     8     
     9 
    10 <body>
    11     <?php 
    12     $id = intval($_POST['id']);
    13     $comment = $_POST['comment'];
    14     // 判断参数是否正确
    15     if ($id <= 0)
    16         die('<script>alert("参数错误");history.back();</script>');
    17     // 执行编辑SQL语句
    18     $sql = "update chat set comment = '" . $comment . "' where uid = " . $_SESSION['uid'] . " and id = " . $id;
    19     //执行SQL语句
    20     $query = mysqli_query($db, $sql);
    21     if (!$query)
    22         die ('sql语句执行错误'. mysqli_error($db));
    23     //判断是否成功,根据结果跳转页面
    24     die('<script>alert("修改成功");window.location.href = "chat.php";</script>');
    25      ?>
    26 
    27 

    删除留言:

     1 <?php
     2     include_once('config.php');
     3     include_once('if_register.php');
     4 ?>
     5 
     6 
     7 
     8     
     9 
    10 <body>
    11     <?php 
    12     $id = intval($_GET['id']);
    13     // 判断参数是否正确
    14     if ($id <= 0)
    15         die('<script>alert("参数错误");history.back();</script>');
    16     // 执行删除SQL语句
    17     $sql = "delete from chat where uid = " . $_SESSION['uid'] . " and id = " . $id;
    18     //执行SQL语句
    19     $query = mysqli_query($db, $sql);
    20     if (!$query)
    21         die ('sql语句执行错误'. mysqli_error($db));
    22     //判断是否成功,根据结果跳转页面
    23     die('<script>alert("删除成功");window.location.href = "chat.php";</script>');
    24      ?>
    25 
    26 

    转载于:

    52 添加留言 申请聊天室
    留言:
    61 62 $info['id']; ?>"> 63