用文件存储代替数据库存储(1)
  我想各位PHP Fans都会像我一样到处找PHP的主页空间,可是找到PHP的空间还不能满足,还需要有数据库的支持,但是现在这种时候支持数据库的主页空间还寥寥无几,怎么办?成天往来各免费信息网找寻支持数据库的空间?还是自己租一个台虚拟主机?还是自己想一点辙吧。

  从今天开始我要和大家一起研究数据存储的最简单解决方案,使更多像我这样找不到数据库且没钱租虚拟主机的Fans可以更好的利用PHP建立网站。

  我在这里讨论的只是基于文件读写等操作,其他的问题就不说了。本人其实也是个初学者,只是想把我在学习中的发现与大家分享,这正符合Open Source的思想,也希望大家把自己的研究成果与我分享。

  好,现在进入正题,在今后的一段时间内,我会把关于文件存储型的东东贴上来,与大家分享,也希望大家能不吝赐教。我说的东东主要有:留言板、计数器、论坛、通讯录、下载更新、自动添加连接、文章管理系统...这些一切的一切都不需要数据库的支持,只要你的空间有PHP权限就行了。

以下使我做的一个留言板的源代码:
//add.php
<?
//留言板v1.01

//版权属king([email protected])所有
//本软件是完全免费的
//使用者可以随便使用,传播,但请保持程序完整
//如有问题请发信给我,谢谢!

include("config.inc.php");
?>

<html>
<head>
<?echo $style;?>
<meta http-equiv="Refresh" content="3;url=index.php">
<body <?echo $color?>>

<?
if ($nickname=="") {
 print 
"<center><br><br><br><b>请输入您的大名?<br>";
}
else if  (
$note=="") {
 print 
"<center><br><br><br><b>您没有要说的吗?<br>";
}
else{

$permail=" <a href="mailto:$email"><img src="$imgdir$mailimg" border="0"></a>";
$perhp=" <a href="$hp"><img src="$imgdir$hpimg" border="0"></a>";
if (
$email==""$permail="";
if (
$hp=="http://"||$hp==""$perhp="";
$person=$permail.$perhp;

print 
"<p></p>";
$t date(Y年m月d日H时i分);

$note str_replace "n",  "<br>"$note);
$note stripslashes ($note);

$fileb fopen("$recdir$cfile","w");
fwrite($fileb$countplus100);
fclose($fileb);

$coun=$count+1;
$main "<table border="0" width="60%" cellspacing="0"><tr><td align="center
width="
10%"><font face="Verdana">$coun</font></td><td width="45%"><font face="Verdana">
$nickname</font>$person</td><td align="
center" width="45%"><font face="Verdana">
by $t</font></td></tr><tr><td width="
10%"></td><td valign="top" colspan="2
width="
90%">".$note."</td></tr></table><br>n";
$c3=(int) (($coun-1)/10);
$c3++;
$f fopen("$recdir$c3.txt","a");
fwrite($f,$main);
fclose($f);
print 
"<center><br><br><br><br><b>谢谢您的留言!<br>";

}
?>

<br>
<br>
<center><a href="index.php">返回</a></center>
</center>
</body>
</html>

//index.php
<?

//留言板v1.01

//版权属king([email protected])所有
//本软件是完全免费的
//使用者可以随便使用,传播,但请保持程序完整
//如有问题请发信给我,谢谢!

include("config.inc.php");
?>

<html>

<head>
<?echo $style;?>
<title><? echo $title?></title>
</head>

<body <?echo $color?>>

<table border="0" width="100%" cellspacing="0">
  <tr>
    <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td><?echo $hname?><font face="Verdana">[
<?printf("<a href="%s">%s</a>",$url,$url); ?>]</font></td>
        <td align="right">创建于<?echo $d?></td>
      </tr>
    </table>
    </td>
  </tr>
  <tr>
    <td width="100%" bgcolor="<?echo $txtc?>" height="3"></td>
  </tr>
</table>

<p align="center"><? echo $title?>
<br>支持<font face="Verdana">HTML</font>语法</p>

<form method="POST" action="add.php">
  <div align="center"><center><table>
    <tr>
      <td align="center" width="50%"><img src="img/title.gif" width="150" height="200"
      ></td>
      <td align="center" width="50%"><table border="0" width="100%">
        <tr>
          <td>您的大名:</td>
        </tr>
        <tr>
          <td align="center"><input type="text" name="nickname"
          style="<?echo $inputc;?>" size="20"></td>
        </tr>
        <tr>
          <td>电子邮件:</td>
        </tr>
        <tr>
          <td align="center"><input type="text" name="email"
          style="<?echo $inputc;?>" size="20"></td>
        </tr>
        <tr>
          <td>主页所在:</td>
        </tr>
        <tr>
          <td align="center">
            <input type="text" value="http://" name="hp" style="<?echo $inputc;?>" size="20">
          </td>
        </tr>
        <tr>
          <td>留言:</td>
        </tr>
        <tr>
          <td align="center"><textarea name="note"
          style="overflow: auto;<?echo $inputc;?>" rows="3" cols="20"></textarea></td>
        </tr>
        <tr>
          <td align="center"><table border="0" cellspacing="0" cellpadding="6">
            <tr>
              <td width="50%" align="center"><input type="submit" value=" 确定 "
              style="<?echo $bottonc;?>"></td>
              <td width="50%" align="center"><input type="reset" value=" 重写 "
              style="<?echo $bottonc;?>"></td>
            </tr>
          </table>
          </td>
        </tr>
      </table>
      </td>
    </tr></table>
    <table width="60%">
    <tr><td align="center">共<? include("$recdir$cfile"); ?>则留言 现在在第<?
    
if ($page)
    {
    
$c2=$page;
    }
    else
    {
    
$c2=$co;
    }
    echo 
$c2;
    print
"页 其他页:";
    for(
$i=1;$i<=$co;$i++)
    
printf("<a href="%s?page=%d">%d</a> ",$PATH_INFO,$i,$i);
    
    if (
$c2==1printf("上一页 <a href="%s?page=%d">下一页</a>",$PATH_INFO,$c2+1);
    else if (
$c2==$coprintf("<a href="%s?page=%d">上一页</a> 下一页",$PATH_INFO,$c2-1);
    else 
printf("<a href="%s?page=%d">上一页</a> <a href="%s?page=%d">下一页</a>",$PATH_INFO,$c2-1,$PATH_INFO,$c2+1);
    
?></td></tr>
   </table>
  
  </center></div><hr align="center" size="0" width="60%" color="<?echo $txtc?>">
<?
if ($page)
{
 
$content file("$recdir$page$ext");
}

else if (
$count==0$msg="<center><br>无留言<br></center>";

else
{
$content file("$recdir$co$ext");
}
$cou =count($content);
      do {
          
$msg.=$content[$cou];
          
$cou--;
       }while (
$cou>=0);
print 
"<center>$msg";
?>  
</form>
<hr align="center" size="0" width="60%" color="<?echo $txtc?>">
<table width="60%">
<tr><td align="center">共<? include("$recdir$cfile"); ?>则留言 现在在第<?
    
    
if ($page)
    {
    
$c2=$page;
    }
    else
    {
    
$c2=$co;
    }
    echo 
$c2;
    print
"页 其他页:";
    for(
$i=1;$i<=$co;$i++)
    
printf("<a href="%s?page=%d">%d</a> ",$PATH_INFO,$i,$i);
    
    if (
$c2==1printf("上一页 <a href="%s?page=%d">下一页</a>",$PATH_INFO,$c2+1);
    else if (
$c2==$coprintf("<a href="%s?page=%d">上一页</a> 下一页",$PATH_INFO,$c2-1);
    else 
printf("<a href="%s?page=%d">上一页</a> <a href="%s?page=%d">下一页</a>",$PATH_INFO,$c2-1,$PATH_INFO,$c2+1);
    
?></td></tr>
    </table>
<div align="center"><center>

<table width="100%" border="0" cellspacing="0">
  <tr>
    <td bgcolor="<?echo $txtc?>" height="3" colspan="2"></td>
  </tr>
  <tr>
    <td><font face="Verdana">Copyright &copy; <?echo $hename?> All Rights 
    Reserved.</font></td>
    <td align="center"><font face="Verdana">Design by <a href="mailto:[email protected]">king</a></font></td>
  </tr>
</table>
</center></div>
</body>
</html>

//config.inc.php
<?

//留言板v1.01

//版权属king([email protected])所有
//本软件是完全免费的
//使用者可以随便使用,传播,但请保持程序完整
//如有问题请发信给我,谢谢!

$title="摆友留言板"//留言板的名称

$recdir="rec/"//纪录文件存放目录

$cfile="count.txt"//计数文件

$ext=".txt"//纪录文件扩展名

$hname="华健世界"//主页名

$hename="Wakin's World"//主页英文名

$url="http://wakin.126.com"//主页地址

$imgdir="img/"//图标存放目录
$hpimg="home.gif"//主页标志
$mailimg="mail.gif"//新建标志

$d="2000年9月1日"//创建时间

$bgc="#FFDFBF"//背景颜色
$txtc="#FF8000"//文本颜色
$color="bgcolor="$bgc" text="$txtc"";
$inputc="width: 100%; border: 1px solid rgb(255,128,0)"//输入框样式设定
$bottonc="background-color: rgb(255,235,215); color: rgb(255,128,0); border: 1px solid rgb(255,128,0)"//按钮样式设定

$cssset="BODY {FONT-FAMILY: "宋体"; FONT-SIZE: 9pt; MARGIN-LEFT: 0pt; MARGIN-TOP: 0pt}
TD {FONT-FAMILY: "
宋体"; FONT-SIZE: 9pt}TH {FONT-FAMILY: "宋体"; FONT-SIZE: 9pt}
{COLOR: $txtc; text-decoration: none}
A:hover 
{COLOR: #ffffff; TEXT-DECORATION: underline}.text {LINE-HEIGHT: 140%}"
//样式表设定
$style="<style><!$cssset /--></style>";

if(!
file_exist("$recdir$cfile"))
{
 
$ft=fopen("$recdir$cfile","w");
 
fwrite($ft,0,100);
 
fclose($ft);
}

$c=fopen("$recdir$cfile","r"); //返回页数
$c1=fread($c,100);
$co=(int) (($c1-1)/10);
$co=$co+1;
fclose($c);

$file fopen("$recdir$cfile","r"); //留言计数器
$count fread($file100);
$countplus = ($count 1);
fclose($file);

?>