一个php作的文本留言本的例子(二)

上一次我们研究了guest.php文件.具体的问题还要求读者朋友自己深入的去实践,而且需要一些工具书来学习,如果您一点php的知识都没有,笔者奉劝您就不要想下看了,笔者没有太多的时间和篇幅去逐句探究一个php文件的用途和意义.好的,接下来我们来作edit.php这个文件.
-----------
//edit.php

  1<html>
  2<style type="text/css">   
  3Td {FONT-SIZE: 10pt;}   
  4TEXTAREA   
  5{   
  6FONT-FAMILY: "宋体";   
  7FONT-SIZE: 10pt;   
  8}   
  9  
 10</style>
 11<head>
 12<meta content="zh-cn" http-equiv="Content-Language"/>
 13<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 14<title>修改留言</title>
 15<?   
 16require("sys.php");   
 17function readvalue($tags,$tage)   
 18{   
 19global $message,$long,$reply;   
 20$StrStart=strlen(strstr($message,$tags))-strlen($tags);//去除标记后变量所在字串的起始位置.   
 21$StrEnd=strlen(strstr($message,$tage));   
 22$len=$StrStart-$StrEnd;   
 23$StrStart=$long-$StrStart;//起始字符.   
 24$StrString=substr($message,$StrStart,$len);   
 25return $StrString;   
 26}   
 27  
 28function save($record)   
 29{   
 30global $TxtContent,$TxtEmail,$TxtHomepage,$TxtEmail,$TxtName,$TxtFrom,$TxtReply,$REMOTE_ADDR,$guestfile;   
 31$content=file($guestfile,"r");   
 32$space = "&nbsp;";   
 33$time = date(Y年m月d日H小时i分);   
 34$ip=$REMOTE_ADDR;   
 35$TxtReply=StripSlashes($TxtReply);   
 36$TxtContent=StripSlashes($TxtContent);   
 37$TxtContent=htmlspecialchars($TxtContent);   
 38$TxtContent=check_strlen_long($TxtContent);   
 39$TxtContent=nl2br($TxtContent);   
 40$Wcontent = "<tr><td><font color="#AB00E1">留言内容:</font><br/>&lt;!--content&gt;$TxtContent&lt;!--endcontent&gt; ";   
 41$Wcontent=$Wcontent."<br/><font color="#6633FF">留言人大名:</font>&lt;!--name&gt;$TxtName&lt;!--endname&gt; ";   
 42if ($TxtEmail !="")   
 43{$Wcontent=$Wcontent."<br/><font color="#9900CC">电子信箱</font><a href="mailto:$TxtEmail">&lt;!--email&gt;$TxtEmail&lt;!--endemail&gt;</a>"."$space";}   
 44if ($TxtHomepage !="http://")   
 45{$Wcontent=$Wcontent."<font color="#9900CC">主页:</font>$TxtHompage<a href="$TxtHomepage" target="new">&lt;!--homepage&gt;$TxtHomepage&lt;!--endhomepage&gt;</a>";}   
 46$Wcontent=$Wcontent."<br/><font color="#0000FF">时间:$time 来自:&lt;!--from&gt;$TxtFrom&lt;!--endfrom&gt; ".$ip."</font>";   
 47$Wcontent=ereg_replace(chr(10),"",$Wcontent);   
 48$Wcontent=$Wcontent."<hr size="1"/></td>";   
 49$TxtReply=ereg_replace(chr(10),"",$TxtReply);   
 50$WContent=$Wcontent.$TxtReply."n";   
 51$count=count($content);   
 52$fp=fopen($guestfile,"w");   
 53for ($i=0;$i&lt;$count;$i++)   
 54{   
 55if ($i==$record-1)   
 56{$content[$i]=$WContent;}   
 57fputs($fp,$content[$i]);   
 58}   
 59fclose($fp);   
 60}   
 61  
 62$content=file($guestfile,"r");   
 63$message=$content[$record-1];   
 64$long=strlen($message);   
 65$txtcontent=readvalue('&lt;!--content&gt;','&lt;!--endcontent&gt;');   
 66$txtname=readvalue('&lt;!--name&gt;','&lt;!--endname&gt;');   
 67$txtfrom=readvalue('&lt;!--from&gt;','&lt;!--endfrom&gt;');   
 68$txtemail=readvalue('&lt;!--email&gt;','&lt;!--endemail&gt;');   
 69$txthomepage=readvalue('&lt;!--homepage&gt;','&lt;!--endhomepage&gt;');   
 70$txtcontent=strip_tags($txtcontent);   
 71$tags="&lt;!--reply&gt;";   
 72$txtreply=strstr($message,$tags);   
 73$txtreply=ereg_replace(""",""",$txtreply);   
 74if ($Submit)   
 75{   
 76if ($TxtPassword==$managepwd)   
 77{   
 78if ($TxtName!="" and $TxtContent!="")   
 79{   
 80save($record);   
 81echo "<meta content="1;url=guest.php" http-equiv="Refresh"/>";   
 82exit;   
 83}else {$errorm="留言人姓名和内容必填!!";}   
 84}   
 85else {$errorm="密码错误,只有管理员有权修改!!";}   
 86}   
 87?&gt;   
 88</head>
 89<body background="back.gif" bgcolor="#FFFFFF">
 90<? include('head.htm');?>
 91<div align="center">
 92<center>
 93<table border="1" bordercolor="#E3E3E3" cellpadding="7" cellspacing="0" height="31" width="68%">
 94<form action="edit.php" method="post">
 95<?   
 96if ($errorm and $Submit)   
 97{   
 98echo"<tr>";   
 99echo"<td colspan="4" height="40">";   
100echo"<font color="red">出错了,</font>$errorm";   
101echo"</td>";   
102echo"";   
103}   
104?&gt;   
105<tr>
106<td bgcolor="#f0f0f0" height="37" width="18%"><font color="#000000">留言大名</font></td>
107<td bgcolor="#FFFFFF" height="37" width="39%">
108<input "$txtname"?="" name="TxtName" size="26" type="text" value="&lt;?echo"/>&gt;   
109</td>
110<td bgcolor="#f0f0f0" height="37" width="11%"><font color="#000000">来自</font></td>
111<td bgcolor="#FFFFFF" height="37" width="32%">
112<input "$txtfrom"?="" name="TxtFrom" size="20" type="text" value="&lt;?echo"/>&gt;   
113</td>
114</tr>
115<tr>
116<td bgcolor="#f0f0f0" height="31" width="18%"><font color="#000000">主页地址</font></td>
117<td bgcolor="#FFFFFF" height="31" width="39%">
118<input "$txthomepage"?="" name="TxtHomepage" size="26" type="text" value="&lt;?echo"/>&gt;   
119</td>
120<td bgcolor="#f0f0f0" height="31" width="11%"><font color="#000000">Email</font></td>
121<td bgcolor="#FFFFFF" height="31" width="32%">
122<input "$txtemail"?="" name="TxtEmail" size="20" type="text" value="&lt;?echo"/>&gt;   
123</td>
124</tr>
125<tr bgcolor="#FFFFFF">
126<td colspan="4" height="31"><font color="#000000">留言内容</font></td>
127</tr>
128<tr bgcolor="#f0f0f0">
129<td align="center" colspan="4" height="105" valign="middle">
130<textarea cols="50" name="TxtContent" rows="6" wrap="VIRTUAL"><? echo "$txtcontent"?>   
131</textarea>
132</td>
133</tr>
134<tr bgcolor="#FFFFFF">
135<td align="center" colspan="4" height="40" valign="middle"> <font color="#000000">修改密码</font>
136<input "$txtpassword"?="" name="TxtPassword" size="10" type="password" value="&lt;?echo"/>&gt;   
137<font color="#000000">是否保存密码</font>
138<input checked="" name="SavePassword" type="checkbox" value="on"/>
139<input "$record"?="" echo="" name="record" type="hidden" value="&lt;?"/>&gt;   
140  
141<input name="Submit" type="submit" value="我要修改了"/>
142</td>
143</tr>
144<? echo "$txtreply"?>
145<input $txtreply"="" ?="" name="TxtReply" type="hidden" value="&lt;? echo "/>"&gt;   
146</form>
147</table>
148</center>
149</div>
150<?include('bottom.htm');?>
151</body>
152</html>

----------
该注明的地方我已作了诠释.php可以嵌入html代码中,这样写起代码很惬意.
待续...

Published At
Categories with Web编程
Tagged with
comments powered by Disqus