任务四
1.index.php
1 <html> 2 <head> 3 <title>登陆</title> 4 <meta charset="UTF-8"> 5 <link rel="stylesheet" type="text/css" href="./css/index1.css"> 6 <style> 7 body{height:100%;} 8 </style> 9 </head> 10 11 <div class="index_01"> 12 <table style="width: 100%;height:100%;" > 13 <tr> 14 <td align="center" > 15 <table align="center" width=350 height=230; class="index_table" > 16 <form method ="POST" action = "doloadling.php" name="frmLogin"> 17 <tr align="center" style="font-size:25px;"> 18 <td colspan="2" style="font-size:35px;">用户登陆</td> 19 </tr> 20 <tr> 21 <td align="center" style="font-size:25px;">用户名</td> 22 <td><input type="name" maxlength="16" name="uid" placeholder="请输入账号" style="width:180px;font-size:20px;border-radius: 8px; "></td> 23 </tr> 24 <tr> 25 <td align="center" style="font-size:25px;">密 码</td> 26 <td><input name="password" type="password" maxlength="16" placeholder="请输入密码" style="width:180px;font-size:20px;border-radius: 8px; "></td> 27 </tr> 28 <tr align="center"> 29 <td colspan="2"> 30 <input type="submit" name="denglu" value="登陆" class="btn"> 31 <input type="reset" name="rs" value="重置" class="btn"> 32 <input type="button" name="zu" value="注册" onclick="window.location.href='register.php'" class="btn"/> 33 </td> 34 </tr> 35 </form> 36 </table> 37 </td> 38 </tr> 39 </table> 40 </div> 41 </body> 42 </html>
2.doloadling.php
1 <?php 2 require_once "connet.php"; 3 require_once 'mysqlconfig.php'; 4 $id=$_POST['uid']; 5 session_start(); 6 $_SESSION["uid"]=$id; 7 $password=$_POST['password']; 8 $ma1=new DB(); 9 $link=$ma1->connect(); 10 $sql = "select * from user where username='$id' and password='$password'"; 11 $res = $ma1->CheckUser($link,$sql); 12 if($res){ 13 header("Location:add.php"); 14 }; 15 if(!$res){ 16 echo "<script>alert('登录失败,账号或密码错误');location='index.php';</script>"; 17 }; 18 ?>
3.register.php
1 <html> 2 <head> 3 <title>注册</title> 4 <meta charset="UTF-8"> 5 <link rel="stylesheet" type="text/css" href="./css/index1.css"> 6 <style> 7 body{height:100%;} 8 </style> 9 </head> 10 11 <div class="index_01" > 12 <table style="width: 100%;height:100%;" > 13 <tr> 14 <td align="center" > 15 <form action="doregister.php " name="dl" method="post"> 16 <table align="center" width=350 height=230; style="font-family:宋体;font-size:25px;"> 17 <tr align="center"> 18 <td colspan="2" style="font-size:35px;">注册用户</td> 19 </tr> 20 <tr> 21 <td align="center">用户名</td> 22 <td> 23 <input type="name" maxlength="20" name="id" placeholder="手机号/邮箱" style="width:180px;font-size:20px;border-radius: 8px; "> 24 </td> 25 </tr> 26 <tr> 27 <td align="center">密 码</td> 28 <td > 29 <input name="password" type="password" maxlength="16" placeholder="请输入密码" style="width:180px;font-size:20px;border-radius: 8px; "> 30 </td> 31 </tr> 32 <tr> 33 <td align="center">Again</td> 34 <td> 35 <input name="confirmPassword" type="password" maxlength="16" placeholder="请再次输入密码" style="width:180px;font-size:20px;border-radius: 8px; "> 36 </td> 37 </tr> 38 <tr> 39 <td colspan="2" align="center"> 40 <input type="button" name='zu' value='登陆' οnclick="location.href='index.php'" style="font-size:17px;border-radius: 12px;" class="btn"/> 41 <input type="reset" name="zu" value="重置" style="font-size:17px;border-radius: 12px;" class="btn"> 42 <input type="submit" name="zu" value="注册" style="font-size:17px;border-radius:12px;" class="btn"/> 43 </td> 44 </tr> 45 </table> 46 </form> 47 </td> 48 </tr> 49 </table> 50 </div> 51 </body> 52 <html>
4.doregister.php
1 <?php 2 require_once "connet.php"; 3 require_once 'mysqlconfig.php'; 4 $ma1=new DB(); 5 $link=$ma1->connect(); 6 $id=$_POST['id']; 7 $password=$_POST['password']; 8 $confirmPassword=$_POST['confirmPassword']; 9 if($password!=$confirmPassword){ 10 echo "<script>alert('输入的密码和确认的密码不相等');location='register.php';</script>";; 11 } 12 $alt="select * from user where username='$id'"; 13 $res = $ma1->print1($link,$alt); 14 if($id!=null&&$password!=null){ 15 $ma=new DB(); 16 $link=$ma->connect(); 17 $sql = "insert into user (username,password) values('$id','$password')"; 18 for ($i=0; $i < count($res); $i++) { 19 if($id!=$res[$i]['username']){ 20 $res = $ma->insert($link,$sql); 21 }; 22 if($id==$res[$i]['username']){ 23 echo "<script>alert('注册失败,该账号已被注册!');location='register.php';</script>"; 24 } 25 } 26 } 27 else{ 28 echo "<script>alert('注册失败,请输入账号和密码');location='register.php';</script>"; 29 } 30 ?>
5.add.php
1 <?php 2 header('Content-type: text/html; charset=UTF8'); 3 ?> 4 <html> 5 <head> 6 <link rel="stylesheet" type="text/css" href="./css/index1.css"> 7 <title>我的留言板.添加留言</title> 8 </head> 9 <body background="./images/7.jpg" style="background-size:cover;" > 10 <center> 11 <h2>我的留言板</h2> 12 <input type = "button" value = "添加留言" οnclick="location.href='add.php'" class="button"/> 13 <input type = "button" value = "查看留言" οnclick="location.href='show.php'" class="button"/> 14 <input type = "button" value = "退出登陆" οnclick="location.href='index.php'" class="button"/> 15 <hr width = "70%"> 16 </center> 17 <div class="k1"> 18 <form action = "doAdd.php" method = "post"> 19 <h1>Add A Message 20 <span>What's New To Share With Us</span> 21 </h1> 22 <label> 23 <span>Your Name :</span> 24 <input type="text" name="author" placeholder="Your Full Name" /> 25 </label> 26 <label> 27 <span>Title :</span> 28 <input type="text" name="title" placeholder="Please input title" /> 29 </label> 30 <label> 31 <span>Message :</span> 32 <textarea name="content" placeholder="Your Message to Us"></textarea> 33 </label> 34 <div style="margin-left:125px"> 35 <input type="submit" value="提交" class="submit"> 36 <input type = "reset" value = "重置" class="reset"> 37 </div> 38 </div> 39 </form> 40 </body> 41 </html>
6.doAdd.php
1 <?php 2 session_start(); 3 $id=$_SESSION["uid"]; 4 $title = $_POST["title"]; 5 $author = $_POST["author"]; 6 $_SESSION["authorl"]=$author; 7 $content = $_POST["content"]; 8 $ip = $_SERVER["REMOTE_ADDR"]; 9 require_once "connet.php"; 10 require_once 'mysqlconfig.php'; 11 $ma1=new DB(); 12 $link=$ma1->connect(); 13 $sql = "insert into message (user,title,author,ip,liuyan,time) values('$id','$title','$author','$ip','$content',now())"; 14 if($title!=null){ 15 if($author!=null){ 16 $res = $ma1->insertl($link,$sql); 17 }; 18 if($author==null){ 19 echo "<script>alert('请输入留言者!');location='add.php';</script>"; 20 }; 21 }; 22 if($title==null){ 23 echo "<script>alert('请输入留言标题!');location='add.php';</script>"; 24 }; 25 ?>
7.show.php
1 <?php 2 header('Content-type: text/html; charset=UTF8'); 3 ?> 4 <html> 5 <head> 6 <title>我的留言板.查看留言</title> 7 <link rel="stylesheet" type="text/css" href="./css/index1.css"> 8 </head> 9 <body background="./images/7.jpg" style="background-size:cover;background-attachment: fixed;" > 10 <center> 11 <h2>我的留言板</h2> 12 <input type = "button" value = "添加留言" onclick="location.href='add.php'" class="button"/> 13 <input type = "button" value = "查看留言" onclick="location.href='show.php'" class="button"/> 14 <input type = "button" value = "退出登陆" onclick="location.href='index.php';logout()" class="button"/> 15 <hr width = "70%"> 16 </center> 17 <?php 18 //数据库连接 19 $con = @mysqli_connect("localhost","root","123","itcast"); 20 if(!$con){ 21 die("数据库连接错误".mysqli_connect_error()); 22 } 23 mysqli_query($con,"set names 'utf8'"); 24 //显示每页的留言数 25 $pagesize = 8; 26 //确定页数p参数 27 @$p = $_GET['p']?$_GET['p']:1; 28 //数据指针 29 $offset = ($p-1)*$pagesize; 30 //查询本页现实的数据 31 session_start(); 32 $id=$_SESSION["uid"]; 33 $query_sql = "select * from message where user= '$id' order by user desc limit $offset,$pagesize"; 34 # echo $query_sql; 35 $result = mysqli_query($con,$query_sql); 36 /*if (!$result) { 37 printf("Error: %s\n", mysqli_error($con)); 38 exit(); 39 }*/ 40 //循环输出 41 echo "<div style='margin-top:55px'>"; 42 while($res = mysqli_fetch_array($result)){ 43 echo "<div class='k'>"; 44 echo "<div class='ds-post-main'>"; 45 echo "<div class='ds-comment-body'> 46 <span>{$res['author']} 于 {$res['time']} 给我留言</span> 47 <span style='float:right'><a href = 'del.php?id=".$res['id']."'><input type='submit' class='button1' value='删除'></input></a></span> 48 <p>留言主题 : {$res['title']} 留言地址 : <span>{$res['ip']}</span></p> 49 <hr width=450px> 50 <p>{$res['liuyan']}</p></div><br>"; 51 echo "</div>"; 52 echo "</div>"; 53 } 54 echo "</div>"; 55 //分页代码 56 //计算留言总数 57 $count_result = mysqli_query($con,"select count(*) as count from message where user= '$id'"); 58 $count_array = mysqli_fetch_array($count_result); 59 60 //计算总的页数 61 $pagenum = ceil($count_array['count']/$pagesize); 62 //echo '共',$count_array['count'],'条留言'; 63 //echo '共',$pagenum,'页'; 64 echo "<center>"; 65 echo "<div style='display: inline-block;margin-right: 15px;margin-left:15px;'>",'共',$count_array['count'],'条留言','</div>'; 66 echo "<div style='display: inline-block;margin-right: 15px;margin-left:15px;'>",'共',$pagenum,'页','</div>'; 67 68 //循环输出个页数及链接 69 if($pagenum>1){ 70 for($i = 1;$i<=$pagenum;$i++){ 71 if($i == $p){ 72 echo "<div style='background:#e8ffc4;width:25px;display: inline-block;margin-right: 10px;'>",$i,"</div>"; 73 74 }else{ 75 //echo '<a href="show.php?p=',$i,'">',$i,'</a>'; 76 echo '<a href="show.php?p=',$i,'">',"<div style='width:25px;display: inline-block;margin-right: 10px;background:#FF9D6F'>",$i,'</div>','</a>'; 77 } 78 } 79 echo "<div style='display: inline-block;margin-right: 10px;'>",'当前在 ',$p, ' 页',"</center></div>"; 80 } 81 echo "<br>"; 82 echo "<br>"; 83 echo "<br>"; 84 echo "</div>"; 85 ?> 86 <script type="text/javascript"> 87 function logout(){ 88 <% 89 session.invalidate(); //运用invalidate()比较好,退出时使session失效 90 %> 91 } 92 </script> 93 </body> 94 </html>
8.del.php
1 <?php 2 header("content-type:text/html;charset=utf-8"); 3 session_start(); 4 require_once "connet.php"; 5 require_once 'mysqlconfig.php'; 6 $ma1=new DB(); 7 $link=$ma1->connect(); 8 $id = $_GET['id']; 9 //session_start(); 10 //$id=$_SESSION["uid"]; 11 if($link){ 12 $sql="delete from message where id =$id "; 13 //echo "$sql"; 14 $que=mysqli_query($link,$sql); 15 if($que){ 16 echo "<script>alert('删除成功,返回首页');location='show.php';</script>"; 17 }else{ 18 echo "<script>alert('删除失败');location='show.php'</script>"; 19 exit; 20 } 21 } 22 ?>
9.connet.php
1 <?php 2 define('DB_HOST', 'localhost'); 3 define('DB_USER', 'root'); 4 define('DB_PWD', '123'); 5 define('DB_CHARSET', 'UTF8'); 6 define('DB_DBNAME', 'itcast'); 7 ?>
10.mysqlconfig.php
1 <?php 2 class DB{ 3 function connect(){ 4 @$link = mysqli_connect(DB_HOST,DB_USER,DB_PWD);//连接数据库 5 mysqli_set_charset($link,DB_CHARSET);//设置数据库字体格式 6 mysqli_select_db($link,DB_DBNAME) or die('数据库打开失败');//选择数据库 7 if(mysqli_connect_errno()) 8 { 9 die('数据库连接失败 : '.mysqli_connect_errno()); 10 } 11 return $link; 12 } 13 function insert($link,$sql){ 14 if (mysqli_query($link, $sql)) { 15 echo "<script language='javascript'> alert('注册成功!');location='index.php'; </script>"; 16 } else { 17 echo "Error insert data: " . $link->error; 18 } 19 20 } 21 function CheckUser($link,$sql){ 22 $result = mysqli_query($link,$sql); 23 $row = mysqli_num_rows($result); 24 if($row != 0){ 25 return true; 26 } 27 else{ 28 return false; 29 } 30 } 31 function insertl($link,$sql){ 32 if (mysqli_query($link, $sql)) { 33 echo "<script language='javascript'> alert('留言成功!');location='show.php'; </script>"; 34 } else { 35 echo "Error insert data: " . $link->error; 36 } 37 38 } 39 function print1($link,$sql){ 40 $result = mysqli_query($link,$sql); 41 $data = array(); 42 while ($row = mysqli_fetch_array($result)) { 43 $data[] = $row; 44 } 45 if ($data) { 46 return $data; 47 } else { 48 return false; 49 } 50 } 51 52 } 53 ?>