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

注册主页面:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5     <title>用户注册</title>
 6 </head>
 7 <body>
 8     <table width="360px" border="1" align="center">
 9     <form method="post" action="reg_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="text" name="nickname" id="nickname"></td>
20         </tr>
21         <tr>
22             <td>用户密码:</td>
23             <td><input type="password" name="password" id="password"></td>
24         </tr>
25         <tr>
26             <td>重复密码:</td>
27             <td><input type="password" name="repeatpassword" id="repeatpassword"></td>
28         </tr>
29         <tr>
30             <td>用户头像:</td>
31             <td><input type="button" value="选择头像" name="profile" id="profile"></td>
32         </tr>
33         <tr>
34             <td colspan="2" align="center"><input type="submit" name="register" id="register"></td>
35         </tr>
36     </form>
37     </table>
38 </body>
39 </html>
View Code

注册主页面提交后判断提交数据是否符合要求,符合要求,跳到登录页面

 1 <?php include_once('config.php') ?>
 2 <!DOCTYPE html>
 3 <html>
 4 <head>
 5     <title></title>
 6 </head>
 7 <body>
 8     <?php
 9         $username = $_POST['username'];
10         $nickname = $_POST['nickname'];
11         $password = $_POST['password'];
12         $repeatpassword = $_POST['repeatpassword'];
13         if (!$username)
14         {
15             exit('<script>alert("请填写用户名! ")
16             history.back();</script>');
17         }
18         if (!$nickname)
19         {
20             exit('<script>alert("请填写用户昵称! ")
21             history.back();</script>');
22         }
23         if (!$password)
24         {
25             exit('<script>alert("请填写密码! ")
26             history.back();</script>');
27         }
28         if (!$repeatpassword)
29         {
30             exit('<script>alert("请填写重复密码! ")
31             history.back();</script>');
32         }
33         //同时判断其他的参数是否填写
34 
35         //判断该用户名是否存在
36         $sql = "select * from userlist where username = '" . $username . "' ";
37         $query = mysqli_query($db, $sql);
38         if (!$query)
39         {
40             exit('SQL语句执行错误:1 ' . mysqli_error($db));
41         }
42         if ($info = mysqli_fetch_array($query))
43         {
44             exit('<script>alert("该用户名已存在, 请重新填写!");history.back();</script>');
45         }
46 
47         //判断密码是否一致
48         if ($password != $repeatpassword)
49         {
50             die('<script>alert("两次填写密码不一致,请重写填写")</script>');
51         }
52 
53         //执行注册
54         $sql = "insert into userlist(username, nickname, password, regtime, regip) values('" . $username . "', '". $nickname . "', '". md5($password) . "', "
55         . time() . ", '". $_SERVER['REMOTE_ADDR'] . "')";//点怎么可以连接整数了
56         $query = mysqli_query($db, $sql);
57         if (!$query)
58         {
59             exit('SQL语句执行错误:' . mysqli_error($db));
60         }
61         else
62         {
63             exit('<script>alert("注册成功,请登录");window.location.href = "sign_in.php"</script>');
64         }
65     ?>
66 </body>
67 </html>
View Code

登录页面:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5     <title>用户登录</title>
 6 </head>
 7 <body>
 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>
View Code

判断登录信息,登录成功,跳转到聊天室:

 1 <?php include_once('config.php') ?>
 2 <!DOCTYPE html>
 3 <html>
 4 <head>
 5     <title></title>
 6 </head>
 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 </body>
39 </html>
View Code

聊天室(未更新完)

 1 <?php include_once('config.php') ?>
 2 <!DOCTYPE html>
 3 <html>
 4 <head>    
 5     <title></title>
 6 </head>
 7 <body background="">    
 8     <?php
 9         $name = $_SESSION['nickname'];
10         echo '欢迎您,', $name;
11     ?>
12 <a href="signout.php">注销</a>
13 <br/>
14 <table width="1000px" border="1" align="center">
15 <form method="post" action="record.php">
16     <tr>
17         <td  colspan="2">添加留言</td>
18     </tr>
19     <tr>
20         <td align="center" style="font-size: 36px">留言:</td>
21         <td><textarea rows="10" cols="40" name="record"></textarea></td>
22         </tr>
23     <tr>
24         <td></td>
25         <td><input type="submit" name="chat"></td>
26     </tr>
27 </form>    
28 </table>
29 </body>
30 </html>
View Code

聊天室留言判断

 1 <?php
 2     include_once('config.php');
 3     include_once('if_register.php');
 4 ?>
 5 <!DOCTYPE html>
 6 <html>
 7 <head>
 8     <title></title>
 9 </head>
10 <body>
11     <?php
12         $comment = $_POST['record'];
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 </body>
33 </html>
View Code

 未完待续

猜你喜欢

转载自www.cnblogs.com/ducklu/p/8983234.html