readme:不使用數據庫的留言板
經過xml存取實現留言。(注:datafile.xml要設置爲隱藏)php
index.php數據庫
<?php $pin="7407840";//設置密碼 $form1="<form action='index.php' method='post'>"; $form2="密碼:<input type='password' name='pin'>"; $form3="登陸到:<input type='radio' name='to' value='read' checked/>讀取界面"." "."<input type='radio' name='to' value='write'/>寫入界面"; $form4="<input type='submit' value='提交'>"."</form>";//設置登陸密碼框 $action="passwithoutpin";//設置參數:跳過密碼進入系統 if ($_POST['pin']===$pin || $_GET['action']===$action) { if ($_REQUEST['to']==="read") {echo "<script>alert('已登陸到讀取界面')</script>"; /* 如下爲讀取界面主要代碼 */ echo "<br/>"; $datafile=fopen("datafile.xml","r"); $filedata=fread($datafile,4096); if (!$datafile) { echo "錯誤:沒法打開文件!"."<br/>"."聯繫站長!"; die(); }//以"read"模式打開文件 $parser=xml_parser_create();//開始解析xml文件 if (!xml_parse_into_struct($parser,$filedata,$data)) { echo "錯誤:".xml_error_string(xml_error_code($parser))."<br/>"; echo "當前行:".xml_get_current_line_number($parser)."<br/>"; echo "當前列:".xml_get_current_column_number($parser); die(); } else { xml_parser_free($parser); } echo "<h3>留言條目</h3>"; echo "<br/>".$data[0]['tag']; echo "<br/>".$data[1]['tag'].":".$data[1]['value']; echo "<br/>".$data[3]['tag'].":".$data[3]['value']; echo "<br/>".$data[5]['tag'].":".$data[5]['value']; echo "<br/>".$data[7]['tag'].":".$data[7]['value']; fclose($datafile); } elseif ($_REQUEST['to']==="write") { echo "<script>alert('已登陸到寫入界面')</script>"; /*如下爲寫入界面主要代碼 */ echo "<br/>"; echo "<h3>新建新留言</h3>"; echo "<form action='write.php' method='post'>"; echo "留言類型:<input type='radio' name='type' value='任務'>任務"; echo "<input type='radio' name='type' value='提醒'>提醒"; echo "<input type='radio' name='type' value='信息'>信息"."<br/>"; echo "日期:<input type='text' name='date'/>"."<br/>"; echo "任務:<input type='text' name='todo'/>"."<br/>"; echo "位置:<input type='text' name='loc'/>"."<br/>"; echo "標籤:<input type='text' name='lab'/>"."<br/>"; echo "<input type='submit' value='提交'>"; echo "</form>"; } } elseif ($_POST['password'] != $pin && isset($_POST['password'])) { echo "密碼錯誤!"; } else { echo $form1."<br/>".$form2."<br/>".$form3."<br/>".$form4; echo "請輸入密碼!"; } ?>
write.phppost
<?php //先確認,再寫入 $reminder= "<script> if (!confirm('肯定要繼續,將會覆蓋原來的留言內容!點擊取消返回。')) { history.go(-2); } </script>"; //打開文件 $datafile=fopen("datafile.xml","w"); if (!$datafile) { echo "錯誤:沒法打開文件!"."<br/>"."聯繫站長!"; die(); } else { $contents= "<?xml version='1.0' encoding='ISO-8859-1 ?> <{$_POST['type']}> <日期>{$_POST['date']}</日期> <任務>{$_POST['todo']}</任務> <位置>{$_POST['loc']}</位置> <標籤>{$_POST['lab']}</標籤> <{$_POST['type']}> "; if (!file_put_contents(datafile.xml,$contents)) { echo "錯誤:沒法寫入文件!"."<br/>"."聯繫站長!"; die(); } $return= "<script> alert('寫入文件成功,點擊確認鍵返回!'); history.go(-2); </script> "; echo $return; } ?>