公选网站作业4_2.php

序:

  此实验的功能是只有已经登陆或者已经注册的用户才有发表留言的资格,否则只能够浏览留言。

1.  主要文件:

   4_2login.html// 登陆的主页面

 4_2login.php  //处理登陆信息,验证用户是否登陆正确。

  4_2register.html  //注册的页面

  4_2register.php  //处理注册的程序。

  coonectfig.php  //配置信息。

  4_1.php    //用户留言信息。

2.sql 在已有的数据库里创建user表,id,username,password三个字段

1  create table username(
2     id int(10) not null auto_increment,
3     username varchar(30),
4     password varchar(40),
5     primary key(id)
6  );

3 connectfig.php 数据库配置文件

 1 <?php
 2 /*
 3  * 
 4  * @Authors peng--jun 
 5  * @Email   1098325951@qq.com
 6  * @Date    2015-05-17 10:53:44
 7  * @Link    http://www.cnblogs.com/xs-yqz/
 8  * @version $Id$
 9  ==========================================
10  */
11  header("Content-type: text/html; charset=UTF-8"); 
12     $DBHOST = 'localhost';
13     $DBNAME = 'liuyanban';
14     $USER = 'root';
15     $PWD= '';
16     @$conn = mysql_connect($DBHOST,$USER,$PWD) or die ( "连接数据库失败" );
17     mysql_select_db ( $DBNAME, $conn ) or die ( "失败" );
18     mysql_query ( "set names utf8;" );//设置字符集编码
19 ?>

4  4_2register.html文件信息

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>用户登陆留言板</title>
 6     <link rel="stylesheet" type="text/css" href="css/4_1.css">
 7     <script type="text/javascript">
 8     windoe.onload=function(){
 9         var password = document.getElementById('pwd');
10         var password1 = document.getElementById('pwd1');
11         if (!(password.value==password1.value)) {
12             alert("密码前后不一致");
13             return false;
14         } 
15     }
16     </script>
17 </head>
18 <body>
19 <div id="list">
20     <form action="4_2register.php" method="post" name="login" onsubmit="check()">
21         姓 &nbsp;名:<input type="text" name="user">
22         <p></p>
23         密 &nbsp;码:<input type="password" name="pwd" id="pwd">
24         <p></p>
25         确认密码:<input type="password" name="pwd1" id="pwd1">
26         <p></p>
27         <input type="submit" name="submit" value="提交" class="button">
28         <a href="4_2register.html">注册</a>
29         <a href="4_2login.html">登陆</a>
30     </form>
31 </div>
32 </body>
33 </html>

5 . 4_2register.php处理注册页面的信息

 1 <?php
 2 /*
 3  * 
 4  * @Authors peng--jun 
 5  * @Email   1098325951@qq.com
 6  * @Date    2015-05-17 14:58:05
 7  * @Link    http://www.cnblogs.com/xs-yqz/
 8  * @version $Id$
 9  ==========================================
10  */
11  error_reporting(E_ALL ^ E_NOTICE); 
12  header("Content-type: text/html; charset=UTF-8"); 
13  SESSION_START();
14  include 'include/connectfig.php';//引入头文件
15 if (isset($_POST['submit'])){
16     //sql语句
17     $username = $_POST['user'];
18     $pwd = $_POST['pwd'];
19     $q="INSERT INTO user(id,user,pwd)VALUES('','$username','$pwd')";//注意:字段需要打单引号
20     //执行SQL语句
21     if(mysql_query($q)){
22         $_SESSION['logined']=1;   //判断是否已经登录的依据。
23         $_SESSION['user']=$username;  //记录当前登录用户。
24         echo "
25                     <script>
26                             document.write('注册成功,页面正在为你跳转');
27                             setTimeout(function(){window.location.href='4_1.php';},1000);
28                     </script>
29 
30                 ";//如果注册成功 使用js 1秒后跳转到界面; 
31     }else{
32         echo "<script>
           document.write('注册失败');
33 setTimeout(function(){window.location.href='4_2register.html';},1000); 34 </script>";//注册失败,1s种返回到注册页面 35 }; 36 } 37 mysql_close($conn);//关闭数据库 38 ?>

6  4_2login.html  //登陆页面

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>用户登陆留言板</title>
 6     <link rel="stylesheet" type="text/css" href="css/4_1.css">
 7 </head>
 8 <body>
 9 <div id="list">
10     
11     <form action="4_2login.php" method="post" name="login">
12         姓 &nbsp;名:<input type="text" name="user">
13         <p></p>
14         密 &nbsp;码:<input type="password" name="pwd">
15         <p></p>
16         <input type="submit" name="submit" value="提交" class="button">
17         <a href="4_2register.html">注册</a>
18         <a href="4_2login.html">登陆</a>
19     </form>
20 
21 </div>
22 </body>
23 </html>

7  4_2login.php  //登陆页面的处理

 1 <?php
 2 /*
 3  * 
 4  * @Authors peng--jun 
 5  * @Email   1098325951@qq.com
 6  * @Date    2015-05-17 14:58:05
 7  * @Link    http://www.cnblogs.com/xs-yqz/
 8  * @version $Id$
 9  ==========================================
10  */
11 error_reporting(E_ALL ^ E_NOTICE); 
12 header("Content-type: text/html; charset=UTF-8"); 
13 session_start();
14 include 'include/connectfig.php';//引入头文件
15 if (!isset($_POST["submit"])){
16     exit("错误执行");
17 }
18 $username = $_POST['user'];
19 $pwd = $_POST['pwd'];
20 if ($username && $pwd){
21     $sql = "SELECT * FROM user WHERE user = '$username' AND pwd = '$pwd'";//检测数据库是否有对应的username和password的sql
22     $result = mysql_query($sql);//执行sql语句
23     $rows = mysql_num_rows($result);//返回一个数值
24     if($rows){//0 false 1 true
25         $_SESSION['logined']=1;   //判断是否已经登录的依据。
26         $_SESSION['user']=$username;  //记录当前登录用户。
27         header("refresh:0;url=4_1.php");//如果成功跳转至4_1.php页面
28         exit;//跳转成功之后就关闭此页面。
29     }else{
30         echo "用户名或密码错误,将返回登陆界面";
31         echo "
32                     <script>
33                             setTimeout(function(){window.location.href='4_2login.html';},1000);
34                     </script>
35     
36                 ";//如果错误使用js 1秒后跳转到登录页面重试;
37     }
38 }
39 ?>

8 留言板页面

 1 <?php
 2 /*
 3  * 
 4  * @Authors peng--jun 
 5  * @Email   1098325951@qq.com
 6  * @Date    2015-05-17 10:51:56
 7  * @Link    http://www.cnblogs.com/xs-yqz/
 8  * @version $Id$
 9  =====================================================
10  */
11 
12 error_reporting(E_ALL ^ E_NOTICE); 
13 include 'include/connectfig.php';//引入头文件
14 session_start();
15 //检测是否登录
16 if(isset($_SESSION['logined']) && $_SESSION['logined']===true){
17     //$_SESSION['logined']有设置,并且值为真,表示已经登录
18     //echo "当前登录用户是: ".$_SESSION['user'];
19 }
20 if($_POST['submit']){
21     /*$user=$_POST['user'];
22     if ($user=='') {
23         echo "<script>alert("你没有登陆,请先登陆!")</script>";
24     }*/
25     //sql语句
26     $sql="insert into message(id,user,title,content,lastdate)values('','$_SESSION[user]','$_POST[title]','$_POST[content]',now())";
27     //执行sql语句
28     mysql_query($sql);
29 }
30 ?>
31 
32 <!doctype html>
33 <html>
34 <head>
35 <meta charset="UTF-8">
36 <title>简易留言板</title>
37 <link rel="stylesheet" type="text/css" href="css/4_1.css">
38 </head>
39 
40 <body>
41     <div id="div1">
42      
43             <form method="post" id="form" name="myform" action="" onsubmit="Check();">
44                 <!-- 用户:<input type="text" size="10" name="user"><br> -->
45                 标题:<input type="text" name="title"><br>
46                 内容:
47                 <textarea name="content" cols="45" rows="5" name="content"></textarea><br>
48                 <input type="submit" name="submit" value="发布留言" />
49                 <A href="4_2login.html">返回登陆</A>
50             </form>
51   
52     
53             <table cellpadding="5" cellspacing="1" >
54             <?php
55                 $sql1 = "select * from message order by id desc";//按照id倒叙排列
56                 $query = mysql_query($sql1);
57                 while ($rows = mysql_fetch_array($query)) {
58             ?>
59                 <tr bgcolor="#eff3ff">
60                   <td><b>用户:</b><?php echo $rows['user'];?> <b>标题:</b><?php echo $rows['title'] ;?> <b>发布时间:</b><?=$rows['lastdate'];?>
61                   </td>
62                 </tr>
63                 <tr bgColor="#ffffff">
64                   <td><b>内容:</b><?php echo $rows['content'];?></td>
65                 </tr>
66             <?php
67                 }
68             ?>
69             </table>
70     </div>
71     <script type="text/javascript">
72     /*function Check() {
73         if (mysorm.user.value=="") {
74             alert("用户名都不填写,你也真够懒的");
75             myform.user.focus();
76             return false;
77         };
78         if (myform.title.value.length<2) {
79             alert("标题不能少于两个字符");
80             myform.title.focus();
81             return false;
82         };
83         if (myform.content.value="") {
84             alert("必须要填写留言信息");
85             myform.content.focus();
86             return false;
87         };
88     }*/
89     </script>
90 </body>
91 </html>

OK 至此表示所有的页面程序代码就这样愉快的写完了,当然样式方面没有做多少的渲染。今天奖励自己一个鸡蛋。

原文地址:https://www.cnblogs.com/xs-yqz/p/4518157.html