Home  >  Article  >  Backend Development  >  Implementation method of using text files as database in PHP_PHP tutorial

Implementation method of using text files as database in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:52:55715browse

Based on my experience, I think the following file structure is optimal:
---------------------------------- ---------------------------------------
File extension: .php

email=ask4more@13.net & nickname=redfox & realname=A Ding & url=http://NetNote.oso.com.cn & ...
...
----------------------------------------- --------------------------------
Maybe everyone can see that it uses .php as the extension, and The first line of the file is , which effectively prevents illegal access to the data file. The format of the second line of the file is: Variable name 1 = value 1 & Variable name 2 = value 2 &...
Proposing all variables is very simple, just use the function parse_str();
For example:
$theline="email=ask4more@13.net&nickname=redfox&realname=A Ding&url=http://NetNote.oso.com.cn";
parse_str($theline);// Separate the variables $email, $nickname, $realname, $url
echo "I am $nickname,my real name is $realname
";
echo "welcome to visit my website:$urlecho "email me at:$email";
?>
Operating results:
I am redfox,my real name is A Ding
welcome to visit my website: http://NetNote.oso.com.cn
email me at:ask4more@13.net

Therefore, this article agrees that the data text structure is:
-------- --------------------------------

Variable name 1 = value 1 & Variable name 2 = value 2 &...

File extension: .php
-------------------------- --------------------------

The real data starts from the second line. Well, using such a file structure, you can easily implement GuestBook, BBS, and even community data processing:) My homepage "Netnote" http://netnote.oso.com.cn is implemented in this way. .
In order to facilitate the majority of netizens, I have compiled several functions, and the necessary explanations will be made below. Of course you can modify and implement it as you like, but you must ensure the integrity of the functionality. Please save the following code as textfun.inc (of course, other names are the same), and add a line of statement at the beginning of the file you want to use, you You can use the function I compiled for you.
There is a db object and a function p2row(); ---
class db{
var $dbfile;
function createdb($dbName){
$f=$dbName;
$this->$ dbfile=$f;
$headInfo="n";
$fp=fopen($f,"w");
fputs ($fp,$headInfo);
fclose($fp);
chmod($f,0777);//Modify the file mode, also available under Unix
return(1);
}
function opendb($f){
$this->$dbfile=$f;
if(file_exists($f)){
return true;
}else{
$this->createdb($f);
}
}
function insertline($info){
$fields=explode("|",$info);
while(list($key,$val)=each($fields)){
$therow.="$val=$".$val."&";
$var1.="$" .$val.",";
}
$var1.='$tail';
eval("global $var1;"); //To obtain environment variables
eval("$ therow="$therow";");
$fp=fopen($this->$dbfile,"a");
fputs($fp,"$therow");
fclose( $fp);
}
function readall($f){
if(file_exists($f)){
$this->$dbfile=$f;
$rows= file($f);
for($i=1;$i $temp[]=$rows[$i];
}
Return $temp;
}
}
//Read all data lines in reverse order
function revread($f){
if(file_exists($f)){
$this->$dbfile=$f;
$rows=file($f);
$d=count($rows);
$j=$d-1;
for($i=0;$i<$d;$i++){
if($i<$j){
$temprow=$rows[$i];
$rows[ $i]=$rows[$j];
$rows[$j]=$temprow;
$j--; $i $temp[]=$rows[$i];
}
return $temp;
}
}

function close(){
$this=$nothing;
}
}

//Format the paragraph text into one line of text for convenience Storage
function p2row($t){
$t=nl2br(stripslashes(htmlspecialchars($t)));
for($i=0;$i $c=substr($t,$i,1);
if(ord($c)==10) $c=" ";
$tempstr.=$c;
}
return $tempstr;
}
?>
—————————————————————————————————————————————————————— It is our customized data object for this article, including six methods: createdb(), opendb(), insertline(), readall().revread(), close();

db->createdb( string filename)
Usage example: include("textfun.inc");
$mydb=new db;
$mydb->createdb("UserInfo.php");
?>
This method creates a file UserInfo.php, the first line is

db->opendb(string filename)
Usage example: include("textfun.inc");
$mydb=new db;
$mydb->opendb("UserInfo.php");
?>
This method "opens" the data file UserInfo.php in append mode. If this file does not exist, it will be created.
Therefore, this method can replace the createdb() method. (But don’t delete the createdb() function in class db{ } :P)

db->insertline(string VarString)
Usage example: include( "textfun.inc");
$theline="email=ask4more@13.net&nickname=redfox&realname=A Ding&url=http://NetNote.oso.com.cn";
parse_str($theline); //Construct environment variables
$mydb=new db;
$mydb->opendb("UserInfo.php");
$mydb->insertline("nickname|realname|email|url" );
?>
db->insertline() can separate the corresponding environment variables from a string in the form of "nickname|realname|email|url" and store them in the form agreed in this article document. When passing in the parameters of insertline(), you must use "|" to connect the environment variable names into a string. There is no limit to the number, but do not add "$" in front. Well, it must be in the form of "nickname|realname" |email|url" such a string :~)

array db->readall(string filename)
Usage example: include("textfun.inc");
$mydb=new db;
$allrec=$mydb->readall("UserInfo.php");
?>
readall() method returns the first row except the first row (), with each row corresponding to an element of the array.

array db->revread(string filename)
Usage example: include("textfun.inc");
$mydb=new db;
$ allrec=$mydb->revread("UserInfo.php");
?>
revread() method reads in reverse order except the first line (), returns an array. This is especially useful when we are writing guest books etc.

void db->close()
Close the db object.

Okay, now we will use the db object to compile the simplest guestbook. 
---------guestbook.php------------ 
我的留言本

 

NickName:
 
E-Mail:
 
Homepage:
 
Message:

 
 
 
include("textfun.inc"); 
if($Submit){ 
  $thetime=date("Y-m-d h:m:s A"); 
  $message=p2row($message); 
  $mydb=new db; 
  $mydb->opendb("msg.php"); 
  $mydb->insertline("nickname|email|url|message|thetime"); 

  //以下读出所有的数据 
  $allrecs=$mydb->revread("msg.php"); 
  while(list($key,$theline)=each($allrecs)){ 
    parse_str($theline); 
    ?> 
    ">
 
    URL:">
 
    Message:
 
      } 
  $mydb->close(); 

?> 
----------------------------- 
好了,虽然这个留言本不是很美观,但主要是为了举例说明db对象的用法~:) 
本文在WIN98+PWS+PHP4下调试通过!

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/318817.htmlTechArticle按我的经验,本人认为,以下列文件结构为最优: ---------------------------------------------------------------------- 文件扩展名:.php ?die('ACCESSDENIED!'...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn