你好,我叫雁紫,叫我雁紫就可以了。

姓名:雁紫(網(wǎng)名哦)年齡:15QQ:378703658興趣愛好:當然是IT啦,會一點點PHP和Python。個人箴言:Everyone can be everything.(每個人都有" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運營 > PHP實現(xiàn)簡單留言系統(tǒng)

PHP實現(xiàn)簡單留言系統(tǒng)

時間:2023-09-07 15:54:02 | 來源:網(wǎng)站運營

時間:2023-09-07 15:54:02 來源:網(wǎng)站運營

PHP實現(xiàn)簡單留言系統(tǒng):在寫代碼之前先介紹一下自己。。。

你好,我叫雁紫,叫我雁紫就可以了。


做一個簡單的PHP留言系統(tǒng)。

環(huán)境:PHP5.3 + Apache +MySQL

本地:localhost

開發(fā)工具:Zend Studio 11.0.2

知識點:PHP接收數(shù)據(jù)的方式、序列化與反序列化操作等等




OK,在做這么一個小系統(tǒng)之前,我們先梳理一下思路。

  1. 做好前端頁面(展示數(shù)據(jù))。
  2. 后端接受數(shù)據(jù)并儲存
  3. 優(yōu)化系統(tǒng)

1.做一個簡單的前段頁面。

新建一個文件,取名為“index.php”這里制作過程就不說了(也確實沒啥好講的)。主要就是一些HTML的知識,做好了基本是這樣的:(里面的數(shù)據(jù)可有可無)

由于我們主要是講PHP(畢竟前端也不是強項),所以前端頁面制作的簡陋些就好。

代碼直接貼出來:

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>留言頁表頁</title> </head> <body> <div align="center"> <h2 align="center">留言頁表頁 - <a href="addMes.php">繼續(xù)添加</a></h2> <table bgcolor="#0080ff" border="1" width="80%" auto="0"> <tr> <td>編號</td> <td>標題</td> <td>內(nèi)容</td> <td>留言者</td> <td>發(fā)布時間</td> <td>心情</td> </tr> <tr> <td>1</td> <td>1111</td> <td>11111111111</td> <td>1</td> <td>2017-10-02 09:10:51</td> <td><img width="100px" height="100" src="img/face.png"alt="" /></td> </tr>現(xiàn)在,再制作一個添加頁面,命名為“addMes.php”。效果和代碼如下:

<!doctype html><html lang="en"><head> <meta charset="UTF-8" /> <title>添加留言頁</title></head><body><div align="center"> <h2>添加留言</h2> <form action="doAction.php" method="get"> <input type="hidden" name="act" value="add"/> <table border="1" bgcolor="#0080ff" width ="80%"> <tr> <td>發(fā)表人</td> <td><input type="text" name="username" placholder="請輸入昵稱" id="username"/></td> </tr> <tr> <td>標題</td> <td><input type="text" name="title" placholder="請輸入昵稱" id="titlie"/></td> </tr> <tr> <td>內(nèi)容</td> <td><textarea name="content" id="" cols="45" rows="10"></textarea></td> </tr> <tr> <td>心情</td> <td><input type="radio" name="xinqing" value="face.png" checked="checked"/><img src="img/face.png" alt="" width="70px"/> <input type="radio" name="xinqing" value="face.png"/><img src="img/face.png" alt="" width="70px"/> <input type="radio" name="xinqing" value="face.png"/><img src="img/face.png" alt="" width="70px"/> <input type="radio" name="xinqing" value="face.png"/><img src="img/face.png" alt="" width="70px"/> <input type="radio" name="xinqing" value="face.png"/><img src="img/face.png" alt="" width="70px"/> </tr> <tr> <td colspan="2"><input type="submit" value="發(fā)表"/></td> </tr> </table> </form></div></body></html>

2.接收儲存數(shù)據(jù)并讓前端展示。

新建一個文件,命名為“doAction.php”用來接收和保存數(shù)據(jù)。

注意!這時你要在添加頁(“addMes.php”)中給你要傳的數(shù)據(jù)添加name屬性。如

<input type="text" name="title" placholder="請輸入昵稱"></input>而且設(shè)置form表單中的提交方式為“GET”:

<form action="doAction.php" method="get">接下來,寫“doAction.php”中的代碼。

用 $_GET方法來接收傳過來的數(shù)據(jù),并加上一個isset判斷:如果未傳過來數(shù)據(jù)則給空值,如:

$username = isset($_GET['username'])?$_GET['username']:'';將接收到的數(shù)據(jù)存放在一個二維數(shù)組內(nèi):

$arr[] = array( 'username'=>$username, 'title'=>$title, 'content'=>$content, 'xinqing'=>$xinqing, 'time'=>$time );將二維數(shù)組序列化:

$arr = serialize($arr);//序列化將序列化數(shù)據(jù)存放如名為“Data.txt”的文件中:

$filename='Data.txt';if(file_put_contents($filename, $arr)){ echo '添加留言成功<br />'.'<a href="addMes.php">繼續(xù)添加</a>|'.'<a href="index.php">查看留言</a>|';}else { echo '添加留言失敗';}

前端展示:

這時需要在“index.php”中加入幾句代碼:

1.讀取代碼:讓前端讀取數(shù)據(jù):(加此代碼添加到<!doctype html>前即可)

<?php $filename = 'Data.txt'; if (file_exists($filename)&&filesize($filename)>0){ $str = file_get_contents($filename);//取出數(shù)據(jù) $userInfo = unserialize($str);//反序列化 }?>2.展示代碼:將二維數(shù)組通過 foreach 遍歷出來:(注意:此時要保留table標簽的第一行tr標簽):

<table bgcolor="#0080ff" border="1" width="80%" auto="0"> <tr> <td>編號</td> <td>標題</td> <td>內(nèi)容</td> <td>留言者</td> <td>發(fā)布時間</td> <td>心情</td> </tr> <?php foreach ($userInfo as $key=>$val){ ?> <tr> <td><?php echo $key+1;?></td> <td><?php echo $val['title'];?></td> <td><?php echo $val['content'];?></td> <td><?php echo $val['username'];?></td> <td><?php echo $val['time'];?></td> <td><img width="100px" height="100" src="img/<?php echo $val['xinqing'];?>"alt="" /></td> </tr> <?php }?> </table>這樣,前端就可以讀取并展示數(shù)據(jù)了!

3.幾點小優(yōu)化

1.為了防止惡意請求占用系統(tǒng)空間,我們需對系統(tǒng)做幾點優(yōu)化:

在form標簽內(nèi)再加一個按鈕為hidden:

<input type="hidden" name="act" value="add"/>在“doAction.php”中加上一個if判斷:

if ($act=='add'){//存入數(shù)據(jù) $arr[] = array( 'username'=>$username, 'title'=>$title, 'content'=>$content, 'xinqing'=>$xinqing, 'time'=>$time ); }這段代碼的目的是是為了防止惡意請求占用系統(tǒng)空間,這樣,只需要判斷一下hidden的值是否傳過來即可。

2.修復(fù)一個小BUG:這個小BUG就是所有的數(shù)據(jù)只能存儲一次,如果有新的數(shù)據(jù)進來會被覆蓋掉,為了解決這個問題,可以添加一個if判斷:

if (file_exists($filename)&&filesize($filename)>0){ //取出數(shù)據(jù) $str = file_get_contents($filename); //反序列化 $arr = unserialize($str); }這個判斷的目的就是判斷一下文件是否已經(jīng)存在,如果存在,取出文件中的數(shù)據(jù)并反序列化為二維數(shù)組,待新數(shù)據(jù)一起序列化存入文件,這樣就可以避免新的數(shù)據(jù)覆蓋掉舊數(shù)據(jù)。




好啦,現(xiàn)在一個簡單PHP留言系統(tǒng)就完成啦。

轉(zhuǎn)載請注明出處

關(guān)鍵詞:系統(tǒng),留言,簡單,實現(xiàn)

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉