PHP實現基本留言板功能原理與步驟詳解
阿新 • • 發佈:2020-03-27
本文例項講述了PHP實現基本留言板功能的方法。分享給大家供大家參考,具體如下:
作為一個PHP的初學者,我試著寫了一個留言板,頁面有點醜,多多見諒,嘻嘻嘻
#我們寫留言板需要用到資料庫,所以我們先要建立三個表
user表
friend表
text表
#首先需要寫一個註冊與登入
##註冊
zhuce.html
<meta charset="utf-8"> <title>zhuce</title> </head> <body> <form method="POST" action="zhuce.php"> <div style="margin-left: 500px;margin-top:200px;height: 250px;width: 250px"> <h1>使用者註冊頁面</h1> 使用者名稱:<input type="text" name="username"> <div>密 碼:<input type="password" name="password"> <div><input type="submit" name="submit" value="註冊"></div> </div> </form> </body>
zhuce.php
<?php session_start(); header("Content-type: text/html; charset=utf-8"); //處理資料庫使用者名稱亂碼 $user=$_POST["username"]; $pwd=$_POST["password"]; if($user==""||$pwd=="") { echo "<script>alert('請確認資訊完整性!'); history.go(-1);</script>"; } else { $link=mysqli_connect("localhost","root","","liuyan");//連線資料庫 mysqli_query($link,"set names utf8"); $sql="select username from user where username='$_POST[username]'"; $result=mysqli_query($link,$sql);//執行sql語句 $num=mysqli_num_rows($result);//統計執行結果影響的行數 if($num)//如果存在該使用者 { echo "<script>alert('使用者名稱已存在!'); history.go(-1);</script>"; } else//註冊新使用者 { $sql_insert="insert into user (username,password)values('$_POST[username]','$_POST[password]')"; $res_insert=mysqli_query($link,$sql_insert); if($res_insert) { echo "<script>alert('註冊成功!');window.location='denglu.html';</script>"; } else { echo "<script>alert('系統繁忙請重試!'); history.go(-1);</script>"; } } } ?>
#效果如下
##登入
denglu.html
<head> <meta charset="utf-8"> <title>denglu</title> </head> <body> <form method="POST" action="denglu.php"> <div style="margin-left: 500px;margin-top:200px;height: 250px;width: 250px"> <h1>使用者登入頁面</h1> 使用者名稱:<input type="text" name="username"> <div>密 碼:<input type="password" name="password"></div><br/> <input type="submit" name="submit" value="登入"> <a href="zhuce.html" rel="external nofollow" >註冊</a> </div> </form> </body>
denglu.php
<?php session_start(); $user=$_POST["username"]; $_SESSION["uesrname"]=$user;//session超全域性變數 $pwd=$_POST["password"];//獲取密碼 if($user=""||$pwd="") { echo "<script>alert('請輸入使用者名稱或密碼!'); history.go(-1);</script>"; } else { $link=mysqli_connect("localhost","liuyan");//連線資料庫 mysqli_query($link,"set names utf8"); $sql = "select username,password from user where username = '$_POST[username]' and password = '$_POST[password]'"; $result=mysqli_query($link,$sql);//執行sql語句 $num=mysqli_num_rows($result);//統計影響結果行數,作為判斷條件 if($num) { echo "<script>alert('登入成功!');window.location='003.php';</script>";//登入成功頁面跳轉 } else { echo "<script>alert('使用者名稱或密碼不正確!');history.go(-1);</script>"; } } ?>
#效果如下
#下面需要我們寫一個登入進去的頁面,它顯示別人給你發來的留言以及進入你要給別人留言的頁面或者退出系統,在這裡我將html程式碼嵌入php
php程式碼如下
003.php
<?php session_start(); global $user;//定義$user global $user1; $_SESSION["username"]=$user; $username=$_SESSION["uesrname"]; $user1=implode("",$_SESSION);//將session中的陣列變為字串元素 $link=mysqli_connect("localhost","liuyan");//連線資料庫 mysqli_query($link,"set names utf8"); $sql="select * from text where recever='{$username}'"; $result=mysqli_query($link,$sql);//執行語句 if($num=mysqli_num_rows($result))//將HTML嵌入PHP中,實現從資料庫中獲得留言資訊 {?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <div> <a href="fabu.html" rel="external nofollow" >釋出資訊</a> <a href="tuichu.php" rel="external nofollow" >退出系統</a></h3></div> <br/><br/> <h2>留言資訊:</h2> <table cellpadding="0" cellspacing="0" border="1" width="60%"> <tr bgcolor="#8BBCC7"> <td>傳送人</td> <td>接收人</td> <td>傳送時間</td> <td>資訊內容</td> <?php echo '<pre>'; while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) {?> <tr bgcolor="#FFFFFF"> <td ><?php echo $row['sender'];?> </td> <td > <?php echo $row['recever'];?> </td> <td > <?php echo $row['comment'];?> </td> <td > <?php echo $row['time'];?> </td> <?php } ?> </tr> </table> </body> </html> <?php }?>
#效果如下
#接下來我們就該寫釋出的頁面以及PHP程式碼了
fabu.html
<head> <meta charset="utf-8"> <title>fabu</title> </head> <body> <form method="POST" action="fabu.php"> <h1>釋出資訊<h1> <a href="003.php" rel="external nofollow" >主頁面</a> <div>接收人:<input type="text" name="recever"> </select> </div> </select> </div> <br/> <div> 資訊內容:<textarea input type="text" name="neirong"></textarea> </div><br/> <input type="submit" value="傳送"> </form>
fabu.php
<?php session_start(); header("Content-type: text/html; charset=utf-8"); global $user; $re=$_POST["recever"];//獲取recever $comment=$_POST["neirong"];//獲取留言 @date_default_timezone_set(PRC);//將陣列變為字串函式 $time=date("Y-m-d G:i:s");//獲取時間,G為24小時制 $_SESSION["username"]=$user;//開啟session $user1=implode("",$_SESSION);//將陣列轉為字串 $link=mysqli_connect("localhost","set names utf8"); $sql="insert into text(sender,recever,comment,time) values('$user1','$re','$comment','$time')"; $result=mysqli_query($link,$sql);//執行語句 $sql1="insert into friend(me,friend) values('$user1','$re')";//將me,friend存入資料庫 $result=mysqli_query($link,$sql1);//執行語句 if($recever=""||$comment="") { echo "<script>alert('釋出失敗!');window.location='fabu.html';</script>"; } else { echo "<script>alert('釋出成功!');window.location='fabu.html';</script>"; } ?>
#效果如下
#最後是退出系統
<?php session_start(); unset($_SESSION["uesrname"]); echo "<script>alert('退出成功!');window.location='denglu.html';</script>"; ?>
#效果如下
更多關於PHP相關內容感興趣的讀者可檢視本站專題:《PHP+MySQL留言板開發專題》、《php快取技術總結》、《PHP陣列(Array)操作技巧大全》、《php字串(string)用法總結》、《PHP錯誤與異常處理方法總結》、《php面向物件程式設計入門教程》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧彙總》
希望本文所述對大家PHP程式設計有所幫助。