View Full Version : Problem with search script

02-29-2012, 04:01 PM
hello guys!

when i search for "health" i get good results in my website but the title of every search is title of my website, not the title of the product!

here is my code:

include 'config.php';


$cfg['base_dir'] = getcwd(); //get the full path to public_html in this case getswc()
$cfg['domain'] = "http://".$domain;
$cfg['min_str_len'] = 3;
$cfg['excerpt_count'] = 3; //maximum excerpts of the the search term to be displayed on the results section
$cfg['reccursive_search'] = false;
$cfg['file_exceptions'] = array("textsearch.php", "menu.php","search.php","category.php", "cart1.php", "cart.php", "managecart.php", "sjw.php"); //always include the filesname of this file
//$cfg['file_types'] = array("htm","txt", "php"); //files type to search
$cfg['file_types'] = array("php"); //files type to search

$msg['empty'] = "No search term specified";
$msg['too_short'] = "A minimum of $cfg[min_str_len] characters are required";
$msg['no_files'] = "No files to search";
$msg['no_matches'] = "No matches found for '%string%'";
$msg['matches'] = "Matches found for '%string%'";

function getFiles($base,&$files, $exts = array(), $reccursive = false, $exceptions = array()){
$handle = opendir($base);
$root = ($base == "/") ? "" : $base;

while (false !== ($item = readdir($handle))) {
if ($item!="." && $item!="..") {
if (is_file("$root/$item")) {
$ext = strtolower(substr(strrchr($item, "."), 1));
if (in_array($ext,$exts) && !in_array($item,$exceptions)) {
$files[] = "$root/$item";
} else {
if ($reccursive) {
getFiles("$root/$item", $files, $exts, $reccursive, $exceptions);
<?php include 'config.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>test </title>
<meta name="description" content="test" />
<meta name="keywords" content=test" />
<meta name="robots" content="index,follow" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="style.css" type="text/css" />
<script src="js/product.js"></script>
<script src="js/thumbnailviewer.js"></script>
<link rel="stylesheet" href="js/thumbnailviewer.css" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="js/ddaccordion.js"></script>
<script type="text/javascript" src="js/jquery.cycle.all.js"></script>
<script type="text/javascript">

headerclass: "headerbar", //Shared CSS class name of headers group
contentclass: "submenu", //Shared CSS class name of contents group
revealtype: "mouseover", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
mouseoverdelay: 500, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
collapseprev: true, //Collapse previous content (so only one open at any time)? true/false
defaultexpanded: [0], //index of content(s) open by default [index1, index2, etc] [] denotes no content
onemustopen: true, //Specify whether at least one header should be open always (so never all headers closed)
animatedefault: false, //Should contents open by default be animated into view?
persiststate: true, //persist state of opened contents within browser session?
toggleclass: ["", "selected"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
togglehtml: ["", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively ["position", "html1", "html2"] (see docs)
animatespeed: "normal", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
//do nothing
onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
//do nothing


<script type="text/javascript">

$(function() {

$('#slideshow img:first').fadeIn(1000, function() {





<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="iehacks.css">

<style type="text/css">

form, h1, h2, h3 {

margin: 0;

padding: 0;


h1, h2, h3 { font-weight: normal; }

h1 { font-size: 2em; }

h2 { font-size: 1.5em; }

h3 { font-size: 1em; }

small {

display: block;

color: #666;


a {

font-weight: bold;

text-decoration: none;

color: #48629c


a:hover {

color: #9D9D9D


.result {

padding: 10px 0 20px 0;


.hilight {

color: #000;

background: yellow;


.error {

text-align: center;

color: #F90;

font-weight: bold;


.success {

text-align: center;

color: #009610;




<div id="container">
<a id="logo" href="index.php"></a>
<br />
<div id="menu">
<div class="menu">
<div id="wrapper-menu">
<li><a href="index.php" class="active"><span>HOME</span></a></li>
<li><a href="faq.php"><span>FAQ</span></a></li>
<li><a href="managecart.php"><span>MANAGE CART</span></a></li>
<li><a href="contact.php"><span>CONTACT US</span></a></li>
</div><!--end menu-->

<?php include 'searchbox.php'; ?>
<table width="1000" border="0" cellspacing="0" cellpadding="0">
<td valign="top" width="250" align="left"><img src="images/dscnt.jpg" alt="discount" style="margin:0px 0 10px 0;" /><br /><br />
<img src="images/ccheck.jpg" alt="cards" />

<td valign="top" width="710">
<div class="slider-wrapper">

<img src="images/slide.png" alt="high quality herbal products" />


<!--end right panel--></td>
<img src="images/tp.png" style="margin-left:-30px;" />
<table width="1000" border="0" cellspacing="0" cellpadding="0" background="images/cnt.png" style="margin-left:-15px;">
<td valign="top" width="226"><div id="left"><?php include 'category.php'; ?></div> </td><!--end left-->

<td valign="top" width="774">
<div id="text">
<h1><font color="green"><img src="images/mini.png" align="left" />Search</font> </h1>
<span class="style1"><?

//if ($_POST['submitbtn']) { //doesn't work in IE. IE doesn't pass the input type image val
$string = htmlentities(strip_tags(trim($_POST['searchstring'])));
if ($string) {
if ($string == "") { //empty search value
echo "<h1 class='error'>$msg[empty]</h1>";
} elseif(strlen($string) < $cfg['min_str_len']) {
echo "<h1 class='error'>$msg[too_short]</h1>";
}else {
getFiles($cfg['base_dir'], $files, $cfg['file_types'], $cfg['reccursive_search'], $cfg['file_exceptions']);
if (count($files)) {
$x = 0;
//$canter = 0;
foreach ($files as $file) {


//if ($canter <= 11) {
//$data = file_get_contents($file); //this doesn't parse php code

//we do this so we parse php code first
$data = ob_get_contents();

//exclude certain parts of a file
$data = preg_replace ('/(\<!--START:excludesearch--\>)(.+?)(\<!--STOP:excludesearch--\>)/s','',$data);

if(preg_match_all("/((\s\S*){0,8})($string)((\s?\S*){0,8})/i", strip_tags($data), $match, PREG_SET_ORDER)) {
$rc = count($match);
$link = str_replace($cfg['base_dir'], "", $file);

$excerpt_count = ($cfg['excerpt_count'] > $rc) ? $rc : $cfg['excerpt_count'];
$excerpt = "";
for($i = 0; $i < $excerpt_count; $i++) {
$excerpt .= $match[$i][1] . "<span class='hilight'>" . $match[$i][3] . "</span>" . $match[$i][4] . "...";

$results[$x] = $file;
$rcount[$x] = $rc;
if (preg_match("/<title>(.*)<\/title>/i", $data, $title)) {
$titles[$x] = (trim($title[1]) != "") ? trim($title[1]) : "$cfg[domain]$link";
} else {
$titles[$x] = "$cfg[domain]$file";
$excerpts[$x] = $excerpt;

if (count($results)) {
echo "<h1 class='success'>". str_replace("%string%", $string, $msg['matches']) . "</h1>";
echo "<ol>";
foreach ($rcount as $key => $val) {
$link = str_replace($cfg['base_dir'], "", $results[$key]);
echo "<li class='result'><h3><a href='".$cfg['domain'].$link."'>$titles[$key]</a></h3>$val occurances<small>$excerpts[$key]</small></li>";
echo "</ol>";
unset($results); unset($rcount); unset($titles); unset($excerpts);
} else {
echo "<h1 class='error'>". str_replace("%string%", $string, $msg['no_matches']) . "</h1>";
} else {
echo "<h1 class='error'>$msg[no_files]</h1>";

</td><!--end right-->
<img src="images/bannerbottom.jpg" style="margin-left:-30px" />

<!--end container-->
<?php include 'footer.php'; ?>


02-29-2012, 04:26 PM
Is it this line?

echo "<li class='result'><h3><a href='".$cfg['domain'].$link."'>$titles[$key]</a></h3>$val occurances<small>$excerpts[$key]</small></li>";

Try this:

echo "<li class='result'><h3><a href='".$cfg['domain'].$link."'>".$titles[$key]."</a></h3>$val occurances<small>".$excerpts[$key]."</small></li>";

02-29-2012, 04:52 PM
Nope, problem is still here. :(

02-29-2012, 06:01 PM
OK ...
So the problem is most likely in this area .... ?

if (preg_match("/<title>(.*)<\/title>/i", $data, $title)) {
$titles[$x] = (trim($title[1]) != "") ? trim($title[1]) : "$cfg[domain]$link";
} else {
$titles[$x] = "$cfg[domain]$file";

I have no way to test your script ... so I'm just guessing.
Maybe a URL to the site might help too ... so I can see what is happening.


02-29-2012, 09:02 PM
nope, still dont work. i cant provide URL , coz its on a local machine

02-29-2012, 09:10 PM
In post #4, I didn't change anything ...
I was just wondering if that could be the trouble area.

Maybe it's always hitting this line ...
$titles[$x] = "$cfg[domain]$file";

because the if statement fails.

Put some misc. characters in the string to see if that line is getting hit ...

$titles[$x] = "test".$cfg[domain]$file;