作者:redfox 邮件:ask4more@163.net
主页:http://netnote.oso.com.cn
相信大家在网上申请的免费PHP空间,如果是初级用户,一般都是没得MySQL可供使用,那么我们解决数据处理的方法之一就是用文本文件了。但是用什么方法才可以最快最方便的处理文本数据呢?
按我的经验,本人认为,以下列文件结构为最优:
----------------------------------------------------------------------
文件扩展名:.php
die('ACCESS DENIED!');?>
email=ask4more@13.net & nickname=redfox & realname=阿鼎 & url=http://NetNote.oso.com.cn & ...
...
----------------------------------------------------------------------
也许大家都看出来了,以.php做扩展名,并且文件的第一行是 die('ACCESS DENIED!');?>,这样就有效的阻止了对数据文件的非法访问。文件的第二行的格式都是: 变量名1=值1 & 变量名2=值2 & ...
提出所有的变量很简单,就是用函数 parse_str();
例如:
$theline="email=ask4more@13.net&nickname=redfox&realname=阿鼎&url=http://NetNote.oso.com.cn";
parse_str($theline);//分离出变量$email,$nickname,$realname,$url
echo "I am $nickname,my real name is $realname
";
echo "welcome to visit my website:$url
";
echo "email me at:$email";
?>
运行结果:
I am redfox,my real name is 阿鼎
welcome to visit my website:http://NetNote.oso.com.cn
email me at:ask4more@13.net
因此,本文约定,数据文本结构为:
----------------------------------------
die('ACCESS DENIED!');?>
变量名1=值1 & 变量名2=值2 & ...
文件扩展名: .php
----------------------------------------
真正的数据从第二行开始。好了,用这样的文件结构就可以很容易的实现GuestBook,BBS,甚至是社区的数据处理了:)我的主页“网络便签” http://netnote.oso.com.cn ,就是这样实现的。
为了方便广大网友,我编了几个函数,下面将作出必要的解释。当然你可以随便的修改和挎贝,但你必须保证功能的完整性。请将下面的代码存为 textfun.inc (当然取其它的名字也是一样的),在你要使用的文件的开始部分加入一行语句,你就可以使用我为你编的函数了。
下面一共一个db对象,一个函数p2row();
-------------textfun.inc----------------
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);//修改文件的模式,在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;"); //为了取得环境变量
eval("\$therow=\"$therow\";");
$fp=fopen($this->$dbfile,"a");
fputs($fp,"$therow\n");
fclose($fp);
}
function readall($f){
if(file_exists($f)){
$this->$dbfile=$f;
$rows=file($f);
for($i=1;$i
}
return $temp;
}
}
//以倒序的方式读入所有的数据行
function revread($f){
if(file_exists($f)){
$this->$dbfile=$f;
$rows=file($f);
$d=count($rows);
$j=$d-1;
for($i=0;$i
if($i
$temprow=$rows[$i];
$rows[$i]=$rows[$j];
$rows[$j]=$temprow;
$j--;
}
}
for($i=0;$i
}
return $temp;
}
}
function close(){
$this=$nothing;
}
}
//把段落文本格式化为一行文本,便于存储
function p2row($t){
$t=nl2br(stripslashes(htmlspecialchars($t)));
for($i=0;$i
if(ord($c)==10) $c=" ";
$tempstr.=$c;
}
return $tempstr;
}
?>
----------------------------------
db是我们自定义的本文数据对象,包括六个方法:createdb(),opendb(),insertline(),readall().revread(),close();
db->createdb(string filename)
用法例:
include("textfun.inc");
$mydb=new db;
$mydb->createdb("UserInfo.php");
?>
这个方法创建了一个文件UserInfo.php,首行是 die('ACCESS DENIED!');?>
db->opendb(string filename)
用法例:
include("textfun.inc");
$mydb=new db;
$mydb->opendb("UserInfo.php");
?>
这个方法以追加模式“打开”了数据文件UserInfo.php,如果这个文件不存在,则被创建。
因此,这个方法可以取代createdb()方法。(但千万别删了class db{ }里面的createdb()函数哦:P)
db->insertline(string VarString)
用法例:
include("textfun.inc");
$theline="email=ask4more@13.net&nickname=redfox&realname=阿鼎&url=http://NetNote.oso.com.cn";
parse_str($theline);//构造环境变量
$mydb=new db;
$mydb->opendb("UserInfo.php");
$mydb->insertline("nickname|realname|email|url");
?>
db->insertline()可以将形如"nickname|realname|email|url"的字符串,分离出相应的环境变量,并以本文约定的形式存入文件。 传入insertline()的参数,一定要用“|”把环境变量名连成字符串,个数不限,但千万别在前面加"$"哦,嗯,就是要形如"nickname|realname|email|url"这样的字符串 :~)
array db->readall(string filename)
用法例:
include("textfun.inc");
$mydb=new db;
$allrec=$mydb->readall("UserInfo.php");
?>
readall()方法返回除首行( die('ACCESS DENIED!');?>)外所有数据的数组,每行对应于数组的一个元素。
array db->revread(string filename)
用法例:
include("textfun.inc");
$mydb=new db;
$allrec=$mydb->revread("UserInfo.php");
?>
revread()方法以倒序方式读入除首行( die('ACCESS DENIED!');?>)外所有数据,返回数组。这对我们在编留言本等时候尤为有用。
void db->close()
关闭db对象。
好了,我们现在就用db对象编一个最简单的留言本。
---------guestbook.php------------
我的留言本
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下调试通过!

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
