作者 来源
时间 2000年11月30日 阅读次数 247
版本 ----- 价值 17
[投他一票]
来源:奥索网
用PHP&XML编制迷你搜索引擎(四)
五、mini的搜索引擎
作了如干的铺垫,令人激动的时刻到来了。
分页等版式输出和用SQL的搜索引擎差不多,我就不加注释了。
第一段为仿sina,yahoo的按照类别查询
第二段为搜索查询部分(其实就是把整个树遍历一遍)显示符合的
内容。
说明:
"网络狂飙之谜你搜索引擎"适用于小数据量的数据索引查询,根据测试,当数据量超过1000条以后便不再适用,相当浪费主机资源,建议您超过400条记录采用mysql等数据库语言构建。希望您通过我的程序,对PHP对XML的解析函数有所认识。所有范例程序欢迎大家适用,如果您要将其用于自己的主页中请于我联系说明(其实就是将其用于的网站的名称告知于我)。
sfs(sfsz@chinese.com)
以上的所有范例请到我的主页fire.oso.com.cn
上下载,我学PHP不到一个月,也刚来oso,希望文章中的错误之处大家能谅解。以后,我将为大家奉献出更多的源创范例。
废话少说,尽请看来。
__________________________________________________________
xml2.php
html>
body>
style type=text/css>
td,p,li,input,select {font-size:12px;}
A:link {font-size:12px;color:#00007f;}
A:visited {font-size:12px;color:#00007f;}
A:active {font-size:12px;color:#ff0000;}
A:hover {font-size:12px;color:#ff0000;}
.title {font-family:Tahoma; width=420 ;font-size :16px; font-weight :bold; color :steelblue; filter:Shadow(color="LightGrey", Direction="130");}
.counter{font-family:Tahoma; color=green; font-size : 12px;}
/style>
// XML文件
$file = "demo.xml";
$pagecount = 10;
class Cweb { //网页
var $name;
var $url;
var $memo;
}
class Cwebs {
var $items = array(Cweb);
var $count = 0;
}
class Csub { //类别
var $name;
var $url;
}
class Csubs {
var $items = array(Csub);
var $count = 0;
}
function xml_parse_from_file($parser, $file)
{
if(!file_exists($file))
die("Can’t find file "$file".");
if(!($fp = @fopen($file, "r")))
die("Can’t open file "$file".");
while($data = fread($fp, 4096)) {
if(!xml_parse($parser, $data, feof($fp)))
return(false);
}
fclose($fp);
return(true);
}
function start_element($parser, $name, $attrs)
{
global $show,$level,$levelcount,$maxlevel,$hide,$lev,$num,$PHP_SELF;
global $webs,$subs;
$level += 1;
if($level>$maxlevel)$maxlevel=$level;
$levelcount[$level]+=1;
if($hide){
if($level==$lev&&$levelcount[$level]==$num)$hide=FALSE;
}else{
if($level
}
if(!$hide){
switch($name){
case "sub":
$show="sub";
break;
case "web":
$show="web";
break;
default:
break;
}
if($level==$lev+1&&$level>0){
switch($show){
case "sub":
$subs->count+=1;
$subs->items[$subs->count]->url = "$PHP_SELF?lev=$level&num=$levelcount[$level]";
break;
case "web":
$webs->count+=1;
while ( list( $key, $val ) = each( $attrs ) ) {
switch(trim($key)){
case "url" :
$webs->items[$webs->count]->url=trim($val);
break;
case "memo" :
$webs->items[$webs->count]->memo=trim($val);
break;
}
}
break;
default:
break;
}
}
}
}
function stop_element($parser, $name)
{
global $level;
$level -= 1;
}
function data($parser, $data)
{
global $level,$hide,$show,$lev,$levelcount,$num;
global $webs,$subs,$title;
if($level==$lev&&$levelcount[$level]==$num&&trim($data)!="")$title=trim($data);
if(!$hide)
if(trim($data)!=""&&($level==$lev+1&&$level>0)){
switch($show){
case "sub":
$subs->items[$subs->count]->name=trim($data);
break;
case "web":
$webs->items[$webs->count]->name=trim($data);
break;
}
}
}
//main start
global $lev,$num,$PHP_SELF;
global $title,$webs,$subs;
$level = -1;
$hide = TRUE;
$webs = new Cwebs;
$subs = new Csubs;
if($lev==""){$lev=0;$num=1;}
if($page=="")$page=0;
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "stop_element");
xml_set_character_data_handler($parser, "data");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
$ret = xml_parse_from_file($parser, $file);
if(!$ret)
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
echo " ";
echo "
$i=0;
echo "
"; echo "".$subs->items[$i]->name.""; echo " | ";
";
$i=$page*$pagecount;
if ($webs->count > 0){
echo "
echo Ceil($webs->count/$pagecount);}
echo "
".$i." | ". $webs->items[$i]->name." |
".$webs->items[$i]->memo; echo " |
if ($webs->count > 0){
if($page>0)echo "上一页 ";
if(($page+1)
}
?>
/body>
/html>
__________________________________________________________
xml3.php
关键字匹配采用eregi函数,功能相对简单,且有些bug,
如输入C++会报错(因为+是关键字)这点就不如用SQL查询了。
您可以在下面的程序的基础上加以完善,我这个迷你的就
起到抛砖引玉的作用吧。
html>
body>
style type=text/css>
td,p,li,input,select {font-size:12px;}
A:link {font-size:12px;color:#00007f;}
A:visited {font-size:12px;color:#00007f;}
A:active {font-size:12px;color:#ff0000;}
A:hover {font-size:12px;color:#ff0000;}
.title {font-family:Tahoma; width=420 ;font-size :16px; font-weight :bold; color :steelblue; filter:Shadow(color="LightGrey", Direction="130");}
.counter{font-family:Tahoma; color=green; font-size : 12px;}
/style>
// XML文件
$file = "demo.xml";
$pagecount = 10;
class Cweb { //网页
var $name;
var $url;
var $memo;
}
class Cwebs {
var $items = array(Cweb);
var $count = 0;
}
class Csub { //类别
var $name;
var $url;
}
class Csubs {
var $items = array(Csub);
var $count = 0;
}
// 解析XML文件的函数
function xml_parse_from_file($parser, $file)
{
if(!file_exists($file))
die("Can’t find file "$file".");
if(!($fp = @fopen($file, "r")))
die("Can’t open file "$file".");
while($data = fread($fp, 4096)) {
if(!xml_parse($parser, $data, feof($fp)))
return(false);
}
fclose($fp);
return(true);
}
function start_element($parser, $name, $attrs)
{
global $show,$level,$levelcount,$maxlevel,$PHP_SELF;
global $webs,$subs;
global $search,$finded;
$finded=FALSE;
$level += 1;
if($level>$maxlevel)$maxlevel=$level;
$levelcount[$level]+=1;
switch($name){
case "sub":
$show="sub";
break;
case "web":
$show="web";
break;
default:
break;
}
switch($show){
case "sub":
$subs->count+=1;
$subs->items[$subs->count]->url = "xml2.php?lev=$level&num=$levelcount[$level]";
break;
case "web":
$webs->count+=1;
while ( list( $key, $val ) = each( $attrs ) ) {
if(eregi($search,$val))$finded=TRUE;
switch(trim($key)){
case "url" :
$webs->items[$webs->count]->url=trim($val);
break;
case "memo" :
$webs->items[$webs->count]->memo=trim($val);
break;
}
}
break;
default:
break;
}
}
function stop_element($parser, $name)
{
global $level;
$level -= 1;
}
function data($parser, $data)
{
global $level,$show,$levelcount;
global $webs,$subs;
global $search,$finded;
if(trim($data)!=""){
switch($show){
case "sub":
$subs->items[$subs->count]->name=trim($data);
if(!eregi($search,$data))$subs->count-=1;
break;
case "web":
$webs->items[$webs->count]->name=trim($data);
if((!eregi($search,$data))&&(!$finded))$webs->count-=1;
break;
}
}
}
//main start
global $PHP_SELF;
global $search,$webs,$subs;
$level = -1;
$hide = TRUE;
$webs = new Cwebs;
$subs = new Csubs;
if($page=="")$page=0;
if($search=="")$search="请输入关键字";
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "stop_element");
xml_set_character_data_handler($parser, "data");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
$ret = xml_parse_from_file($parser, $file);
if(!$ret)
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
// 输出
echo "";
echo "
$i=0;
echo "
"; echo "".$subs->items[$i]->name.""; echo " | ";
";
$i=$page*$pagecount;
if ($webs->count > 0){
echo "
echo Ceil($webs->count/$pagecount);}
echo "
".$i." | ". $webs->items[$i]->name." |
".$webs->items[$i]->memo; echo " |
if ($webs->count > 0){
if($page>0)echo "上一页 ";
if(($page+1)
}
?>
/body>
/html>

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.


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

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

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
