How it works - V1.3
2010-06-07Short intro
You can use Live Search for smaller and medium websites. A database is not needed. The website will be crawled from the defined baseurl. The links will be collected and the content cached - so future searches are faster. Almost found search results are stored in session files too to increase search speed.
The textparts with the found searchstrings are cropped in the search results so only the part with the searchstring is displayed (like displaying on Google)
Various options help you to handle Live Search. A Search Word Cloud is available too.
Features
- Easy to include and setup
- Should work with every smaller and middle website
- No need of any database
- Pagination function
- Caching of searchresults and content
- Include unlinked files new since 1.2
- Exclude paths and files from being searched for links new since 1.2
- Exclude blocks of your Site from indexing by using comment tags new since 1.3
- ...
Requirements
- PHP 5.x
- activated allow_url_fopen
New since V1.3 2010-06-07
- Added some error setup - handlers
- Possibility to exclude complete blocks from indexing. i.e. your menues, ...
- Added prior 5.1.2 fix for parse_url()
New since V1.2 2010-02-14
- Double quote and escaped characters fixing for the collected Searchwords (Cloud)
- Possibility to exclude files and directories under the basedir
- Possibility to include unlinked files and directories under the basedir
- Some major bugfixes to prevent from endless loops and to make caching faster
New since V1.1 2010-02-13
- Some fixes
- Possibility add GET-Variables for pagination ($add2query)
- Possibility add GET-Variables for drawing searchresults ($add2query)
- Possibility add GET-Variables for the SearchCloud ($add2query)
Searchform
Just post any form with your searchfield and use as form action i.e. search.php
Setting for your web
upload ls-folder to your webproject - the ls-folder contains livesearch.class.php, livesearch.css, cache
Include file and initialize Class
Include livesearch.class.php and initialize Class on every page where you want to use the Live Search functions
i.e.
<?php
include("ls/livesearch.class.php");
$LiveSearch = new LiveSearch();
?>
Settings in the livesearch.class.php
The baselink from where the grabbing should start
var $baseurl = "http://www.homac.at/";
The name of you search results page to prevent from endless loops (within the $basedir path)
var $searchresultspage = "search.php";
new since V1.2 - Exlude paths or individual files under the $basedir from being checked for links
var $excl = array("dont_index",
new since V1.2 - Include individual files under the $basedir which aren't linked anywhere on your site,
"hideme.html",
"private/",
);
for example hallo.txt isn't linked anywhere on demo page, but content can be found.
var $incl = array("hallo.txt",
Extensions for grabbing links
);
var $checkext = array("htm","html","php","txt");
Hours between caching processes (-1 for caching every search process could be okay for smaller dynamic content sites)
var $cachetime = 12; //-1 for caching every access
Results per page, if more results are found you can use the pagination function
var $srch_res_per_page = 15;
Min and max fontsize for the SearchCloud (px)
var $cloud_min = 10;
Cache directory - have to be writeable, in the example below the path to cache directory will be calculated automatically relative to livesearch.class.php
var $cloud_max = 45;
$this->cachedir = realpath(dirname(__FILE__)) . "/cache";
Live Search Functions
Cache Files
Just caching the files without searching - this action could take a while an will be called automatically while search process if no files are cached or the age of the cached files is older than the defined $cachetime
$LiveSearch->cacheFiles();
Search
necessary to initiate the search, if no files are cached or the age of the cached files is older than the defined $cachetime the cacheFiles function will be called by the search function too
$LiveSearch->search($_REQUEST["q"],$_REQUEST["p"]);
or, if you like to design the results by yourself (you will get an array)
$searchresults = $LiveSearch->search($_REQUEST["q"],$_REQUEST["p"]);
- $_REQUEST["q"]
- The value of the search, the searchstring
- $_REQUEST["p"]
- The current page, needed for the pagination
- url
- The url
- title
- The pagetitle
- content
- The snippet where the the searchstring is embedded
Available variables
After a successfull search you have access to some variables
- $LiveSearch->searchcount
- Total number of searchresults
- $LiveSearch->p
- Current Page
- $LiveSearch->pages
- Total pages
- ...
- ...
Clear Cache
Function to delete all cached files. Note: cached files will be deleted automatically if they are too old ($cachetime exceeded) or on every other caching process (-1)
$LiveSearch->clearCache();
Clear Search Results
Function to remove stored search results
$LiveSearch->clearSrch();
Clear Stored Search Counts
Function to remove stored searchstrings (used by the Search Word Cloud)
$LiveSearch->clearSrchStr();
Pager
Returns paging information after search was successfull and results are more than the defined $srch_res_per_page, with these information you could build your own pagination
$LiveSearch->pager();
Returns array with the following keys:
- current
- Current page
- Total
- Total pages
Pagination Example
Returns an example output for the pagination if results are more than the defined $srch_res_per_page
$LiveSearch->drawPagination();or
$LiveSearch->drawPagination("p","q")or, new since V1.1
$LiveSearch->drawPagination("p","q","&action=search")
Syntax
$LiveSearch->drawPagination([PageVarName], [SearchStringVarName], [Add2Query])
The Parameters
- [PageVarName]
- The name of the page variable (default p)
- [SearchStringVarName]
- The name of the search string (default q)
- [Add2Query]
- If needed you could add some Variables to the pagination Links (i.e. &action=search) - dont't forget the leading &
Searchresults Example
An example output for the search results, including the pagination from above
$LiveSearch->drawSearchresults();or
$LiveSearch->drawSearchresults("p","q")or, new since V1.1
$LiveSearch->drawSearchresults("p","q","&action=search")
Syntax
$LiveSearch->drawSearchresults([PageVarName], [SearchStringVarName], [Add2Query])
The Parameters
- [PageVarName]
- The name of the page variable (default p)
- [SearchStringVarName]
- The name of the search string (default q)
- [Add2Query]
- If needed you could add some Variables to the pagination Links (i.e. &action=search) - dont't forget the leading &
Show collected URLs
Shows you the collected and cached Urls
$LiveSearch->showUrls();
output for this website:
Search Cloud
Shows you the Search Word Cloud
$LiveSearch->printSrchCloud()or
$LiveSearch->printSrchCloud("q") or, new since V1.1
$LiveSearch->printSrchCloud("q","&action=search")
Syntax
$LiveSearch->printSrchCloud([SearchStringVarName], [Add2Query])
The Parameters
- [SearchStringVarName]
- The name of the search string (default q)
- [Add2Query]
- If needed you could add some Variables to the pagination Links (i.e. &action=search) - dont't forget the leading &
Excluding blocks from being indexed
since V 1.3 you're able to exclude/hide blocks from your website from LiveSearch by setting simple comment tags. This makes sense for menues on every page
Start hiding
<!--LSHIDE-->
Stop hiding
<!--/LSHIDE-->
Examples
On this example page the Ciao Codecanyon part on the index page can't be found. (ciao too :) )
other Examples
#1
Some words, can be found but <!--LSHIDE-->this combination can't be<!--/LSHIDE--> found
#2
blabla
<!--LSHIDE-->
Mainmenue #1
Mainmenue #2
Mainmenue #3
<!--/LSHIDE-->
some text ...
<!--LSHIDE-->
Submenue #1
Submenue #2
<!--/LSHIDE-->
...
Examples
Just the Formular
<form method="post" action="search.php">
<input type="text" name="q">
<input type="submit">
</form>
The Searchresults
<?php
include("ls/livesearch.class.php");
$LiveSearch = new LiveSearch();
?>
...
<?php
$LiveSearch->search($_REQUEST["q"],$_REQUEST["p"]);
echo "<p>" . $LiveSearch->drawSearchresults() . "</p>";
?>
...
or
<?php
include("ls/livesearch.class.php");
$LiveSearch = new LiveSearch();
?>
...
<?php
$search_results = $LiveSearch->search($_REQUEST["q"],$_REQUEST["p"]);
echo "Found: " . $LiveSearch->searchcount;
echo "Pages: " . $LiveSearch->pages;
echo "Current Page: " . $LiveSearch->p;
echo "<pre><b>Search Results</b><code>" .
print_r($search_results,true) . "</code><pre>";
?>
...