国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

實(shí)例文件留言本

接下來(lái)我們來(lái)看看示範(fàn)效果:

在下方一個(gè)介面中寫(xiě)入留言內(nèi)容的表單介面:
? ? ? ? ? ? ? ? ? ? ? ?? 所使用 存在留言#? 存在留言#?:


我們來(lái)看看檔案結(jié)構(gòu):

#index.php ---展示輸入方塊和留言內(nèi)容 write.php ---向message.txt寫(xiě)入資料 message.txt ---存入聊天內(nèi)容

#index.php檔案

<?Php
//設(shè)置時(shí)區(qū)
date_default_timezone_set('PRC');
 
//讀了內(nèi)容
@$string = file_get_contents('message.txt');
 
//如果$string 不為空的時(shí)候執(zhí)行,也就是message.txt中有留言數(shù)據(jù)
if (!empty($string)) {
 
    //每一段留言有一個(gè)分格符,但是最后多出了一個(gè)&^。因此,我們要將&^刪掉
    $string = rtrim($string, '&^');
 
    //以&^切成數(shù)組
    $arr = explode('&^', $string);
 
    //將留言內(nèi)容讀取
    foreach ($arr as $value) {
 
        //將用戶名和內(nèi)容分開(kāi)
        list($username, $content, $time) = explode('$#', $value);
 
        echo '用戶名為<font color="gree">' . $username . '</font>內(nèi)容為<font color="red">' . $content . '</font>時(shí)間為' . date('Y-m-d H:i:s', $time);
        echo '<hr />';
    }
 
}
 
?>
 
 
<h1>基于文件的留言本演示</h1>
<form action="write.php" method="post">
    用戶名:<input type="text" name="username" /><br />
 
    留言內(nèi)容:<textarea  name="content"></textarea><br />
 
    <input type="submit" value="提交" />
 
</form>

看了剛剛的展示內(nèi)容,我們知道文件存儲(chǔ)時(shí):

##1.???? 段與段之間進(jìn)行了分割

2.???? 內(nèi)容與用戶之前用一個(gè)特殊的符號(hào)進(jìn)行了分割

下面我們來(lái)寫(xiě)write.php寫(xiě)入留言到檔案的程式碼:

<?php
//追加方式打開(kāi)文件
$fp=fopen('message.txt','a');
 
//設(shè)置時(shí)間
$time=time();
 
//得到用戶名
$username=trim($_POST['username']);
//得到內(nèi)容
$content=trim($_POST['content']);
 
 
//組合寫(xiě)入的字符串:內(nèi)容和用戶之間分開(kāi),使用$#
//行與行之間分開(kāi),使用&^
$string=$username.'$#'.$content.'$#'.$time.'&^';
 
//寫(xiě)入文件
fwrite($fp,$string);
 
//關(guān)閉文件
fclose($fp);
 
 
header('location:index.php');
 
?>

新文字檔案message.txt

#

繼續(xù)學(xué)習(xí)
||
<?Php //設(shè)置時(shí)區(qū) date_default_timezone_set('PRC'); //讀了內(nèi)容 @$string = file_get_contents('message.txt'); //如果$string 不為空的時(shí)候執(zhí)行,也就是message.txt中有留言數(shù)據(jù) if (!empty($string)) { //每一段留言有一個(gè)分格符,但是最后多出了一個(gè)&^。因此,我們要將&^刪掉 $string = rtrim($string, '&^'); //以&^切成數(shù)組 $arr = explode('&^', $string); //將留言內(nèi)容讀取 foreach ($arr as $value) { //將用戶名和內(nèi)容分開(kāi) list($username, $content, $time) = explode('$#', $value); echo '用戶名為<font color="gree">' . $username . '</font>內(nèi)容為<font color="red">' . $content . '</font>時(shí)間為' . date('Y-m-d H:i:s', $time); echo '<hr />'; } } ?> <h1>基于文件的留言本演示</h1> <form action="write.php" method="post"> 用戶名:<input type="text" name="username" /><br /> 留言內(nèi)容:<textarea name="content"></textarea><br /> <input type="submit" value="提交" /> </form>
提交重置程式碼