Make a Search Engine For Your Website With PHP
by: Rory Canyon
This hands on PHP Programming article provides the knowledge necessary to design and develop a search engine for your website using PHP version 4.0 and above. Making a search engine for your website with PHP is really easy and provides substantial functionality required by most of the small to medium websites. This article introduces every steps of the development, including both design and PHP programming. Basic computer skills and knowledge of HTML fundamentals are required. Ok, let’s begin now.
Step 1: Design Search Box
Under your website root, make a file called search.htm or anything you like and type in the following code:
Step 2: Write search.php file. It is the core of your website search engine.
Under your website root, create a file called search.php or anything you like.
< ?php
//get keywords
$keyword=trim($_POST["keyword"]);
//check if the keyword is empty
if($keyword==""){
echo"no keywords";
exit;
}
?>
With above, you can give hints to your users when they forget to enter a keyword. Now let’s go through all the files or articles in your website.
< ?php
function listFiles($dir){
$handle=opendir($dir);
while(false!==($file=readdir($handle))){
if($file!="."&&$file!=".."){
//if it is a directory, then continue
if(is_dir("$dir/$file")){
listFiles("$dir/$file");
}
else{
//process the searching here with the following PHP script
}
}
}
}
?>
The following scripts read, process files and check whether the files contain $keyword. If $keyword is found in the file, the file address will be saved in an array-type variable.
< ?php
function listFiles($dir,$keyword,&$array){
$handle=opendir($dir);
while(false!==($file=readdir($handle))){
if($file!="."&&$file!=".."){
if(is_dir("$dir/$file")){
listFiles("$dir/$file",$keyword,$array);
}
else{
//read file
$data=fread(fopen("$dir/$file","r"),filesize("$dir/$file"));
//avoid search search.php itself
if($file!="search.php"){
//contain keyword?
if(eregi("$keyword",$data)){
$array[]="$dir/$file";
}
}
}
}
}
}
//define array $array
$array=array();
//execute function
listFiles(".","php",$array);
//echo/print search results
foreach($array as $value){
echo "$value"."
\n”;
}
?>
Now, combine the programs listed above, you will find all the related results in your websites will be found and listed. A further optimization of the search engine can be taken by adding the following,
1,list the title of all searching results
REPLACE THE FOLLOWING
if(eregi(”$keyword”,$data)){
$array[]=”$dir/$file”;
}
WITH
if(eregi(”$keyword”,$data)){
if(eregi(”
$title=$m[”1″];
}
else{
$title=”no title”;
}
$array[]=”$dir/$file $title”;
}
2,Add links to searching results
CHANGE THE FOLLOWING
foreach($array as $value){
echo “$value”.”
\n”;
}
TO
foreach($array as $value){
list($filedir,$title)=split(”[ ]”,$value,”2″);
echo “$value“.”
\n”;
}
3 Set time limit for PHP execution
ADD THE FOLLOWING AT THE BEGINNING OF PHP FILES
set_time_limit(”600″);
The above unit is second,so ten minutes is the litmit.
Now, combine all the above programs and get the complete search.php file as following,
< ?php
set_time_limit("600");
$keyword=trim($_POST["keyword"]);
if($keyword==""){
echo"Please enter your keyword";
exit;
}
function listFiles($dir,$keyword,&$array){
$handle=opendir($dir);
while(false!==($file=readdir($handle))){
if($file!="."&&$file!=".."){
if(is_dir("$dir/$file")){
listFiles("$dir/$file",$keyword,$array);
}
else{
$data=fread(fopen("$dir/$file","r"),filesize("$dir/$file"));
if(eregi("
]+)>(.+)”,$data,$b)){$body=strip_tags($b[”2″]);
}
else{
$body=strip_tags($data);
}
if($file!=”search.php”){
if(eregi(”$keyword”,$body)){
if(eregi(”“,$data,$m)){
$title=$m[”1″];
}
else{
$title=”no title”;
}
$array[]=”$dir/$file $title”;
}
}
}
}
}
}
$array=array();
listFiles(”.”,”$keyword”,$array);
foreach($array as $value){
list($filedir,$title)=split(”[ ]”,$value,”2″);
echo “$title “.”
\n”;
}
?>
Now, you have made a search engine for your website, enjoy it!
About The Author
Rory Canyon is the founder of ScriptMenu.com, a free web directory for PHP, ASP, ASP.NET, PERL, XML, Java, JavaScript, Flash, CFML, Python and other web programming resources.
For more information, visit http://www.scriptmenu.com
Site Planning is Crucial!
by: Werner Coetzee
If you are thinking about, or you are in the process of creating a new site, you will most definitely find this article interesting and maybe even inspiring. Actually the term ‘interesting’ is not quite what I thought as I slowly and painfully reconstructed the pages on my mother’s website several times over.
I wanted to create a site that was going to be both incredibly user friendly and informative, and at the same time earn a bit of revenue from Google Adsense! Why not?
So with this great little idea, I blindly set off and started to create what was going to be a online resource of complimentary recipes on www.mom2me.com.
This brings me to my biggest and most costly mistake ever. Lack of planning!
I cannot stress the importance of this aspect enough. I would have saved myself literally weeks, if not months, by simply sitting down with a piece of paper and planning my layout carefully.
In all fairness to myself, I must say that I am not a site designer by any stretch of the imagination, but was rather finding my way around the wonderful world of web design and pain by an uncalculated process of trial and error.
In summary: Plan, plan and then plan some more.
If you still doubt why I stress the importance of planning, I will explain in practical terms what happened to me in the creation of my site. If this doesn’t bring the message home, nothing will!
Scripts in the Header section of your site.
As I mentioned earlier, I really wanted to make the site simple and easy to navigate. All the recipes on the site should be easy to copy, paste, mail to a friend and even bookmark.
Well, this is where my first blunder crept in. I decided to make use of some great little scripts on the site to enable users to mail recipes to friends at the touch of a button and also bookmark the page rather effortlessly.
The scripts were really simple to incorporate, but after doing about 100 recipes, I read an article explaining the dangers of adding ‘full’ scripts in the header section of your pages. I learnt that by adding to many scripts in this section of the page makes it difficult for the ‘bots’ to read the content and that it could adversely effect your Page rank and Search engine placing.
This is not a good idea as you might imagine and thus I spent several days rewriting every single page and placing the scripts into separate JavaScript files. This was not the last time I did this!
Search Facility
I really wanted to make it easy for visitors to be able to search for content on my site but I decide to leave that as a phase two of my development. Mistake!
After stumbling across Google’s great ‘search my site” facility I decided to add it to my site. Great idea, but due to the fact that I didn’t add proper Meta Keywords on every page I had a small problem to overcome.
Yep you guessed it. I had to redo every single page on the site yet again. Ouch!
The base tag functionality
Deciding to make use of the base tag functionality within Dream Weaver was a great idea to simplify my designing process, but…
When finely it was time to create my sitemap I suddenly realized that I had countless (almost a hundred) broken links and although it was my own stupidity I still had to re-do almost all my links, on all my pages. Watch out for this. It is an absolute spirit crusher!
My advise, make use of the full URL in all your links on all your pages i.e. http://www.mom2me.com/promo.htm
Site Navigation
I have left the best for last. Due to a complete and utter lack of proper planning, I realized that I omitted a section of the site for people to sign up for my Newsletter.
As everybody who has probably ever done a site knows, this is a crucial part of your site. Whether you are selling directly from your site or whether you plan to do mailing campaigns at a later stage with some marketing material on them, it is crucial to collect as many e-mail addresses as possible, as often as possible.
Well, this forced me to redo the very basics of my pages and … yup redo every single page again.
Now, by now you might feel sorry for me or you might just feel that I am a complete and utter idiot. Regardless of your feelings, I think that from this very practical example it is abundantly obvious that a lack of proper planning will only lead to heart ache and countless sleepless nights. (I think I actually have even less hair!)
Plan your site carefully and live happily ever after!
About The Author
Werner Coetzee
Apprentice Site Owner and Designer
Mom2me.com
How to Design Website Which Google Likes
by: Ramdayal Yadav
Are you going to design or redesign new website or thinking for it, then first read below given points, which google likes in a website, Google Information for Webmasters. There are more than 80 factors of your website that are taken into consideration when determining the ranking of your website, Following these guidelines will help Google find, index, and rank your site. Basic principles:
1) Make pages for users, not for search engines. Don’t deceive your users or present different content to search engines than you display to users, which is commonly referred to as “cloaking.”
2) Avoid tricks intended to improve search engine rankings. A good rule of thumb is whether you’d feel comfortable explaining what you’ve done to a website that competes with you. Another useful test is to ask, “Does this help my users? Would I do this if search engines didn’t exist?” 3) Don’t participate in link schemes designed to increase your site’s ranking or PageRank. In particular, avoid links to web spammers or “bad neighborhoods” on the web, as your own ranking may be affected adversely by those links.
4) Don’t use unauthorized computer programs to submit pages, check rankings, etc. Such programs consume computing resources and violate our Terms of Service. Google does not recommend the use of products such as WebPosition Gold™ that send automatic or programmatic queries to Google. Design and Content Guidelines 1) Make a site with a clear hierarchy and text links. Every page should be reachable from at least one static text link.
2) Offer a site map to your users with links that point to the important parts of your site. If the site map is larger than 100 or so links, you may want to break the site map into separate pages.
3) Create a useful, information-rich site, and write pages that clearly and accurately describe your content.
4) Think about the words users would type to find your pages, and make sure that your site actually includes those words within it.
5) Try to use text instead of images to display important names, content, or links. The Google crawler doesn’t recognize text contained in images.
6) Make sure that your TITLE and ALT tags are descriptive and accurate.
7) Check for broken links and correct HTML.
If you decide to use dynamic pages (i.e., the URL contains a “?” character), be aware that not every search engine spider crawls dynamic pages as well as static pages. It helps to keep the parameters short and the number of them few.
9) Keep the links on a given page to a reasonable number (fewer than 100). Specific recommendations Avoid hidden text or hidden links. Don’t employ cloaking or sneaky redirects. Don’t send automated queries to Google. Don’t load pages with irrelevant words. Don’t create multiple pages, subdomains, or domains with substantially duplicate content. Avoid “doorway” pages created just for search engines, or other “cookie cutter” approaches such as affiliate programs with little or no original content. for more details Please visit below given links.
http://www.google.com/webmasters/index.html
http://www.google.com/webmasters/facts.html
http://www.google.com/webmasters/seo.html
http://www.google.com/webmasters/bot.html
http://www.google.com/webmasters/remove.html
If you want, you can publish these articles on search engine positioning in your newsletter, web site or ebook. You don’t need to take my permission for re-publishing these articles, as long as you display the resource box at the end of each article.
About The Author
Ramdayal Yadav, is Chief SEO in http://www.maharashtradirectory.com/ This is unique Industrial online Business Directoy. For more articles on search engine positioning visit my site http://www.3seo.com/