![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
King of Portal
|
PHP search engine script
The search engine on my website (Jolly Roger's Ship ) is created using PHP. I wanted to put a search engine on my website, but I couldn't find a single tutorial out there, so instead I'm posting my source code and hope it helps people out. It uses two files 'results.php' and 'search.php' along with text files which contain all the sites the search engine can look for.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#2 |
|
King of Portal
|
Filename: search.php
Purpose: The user interface for the search engine. <html>
<head>
<title>
JRSearch
</title>
</head>
<body bgcolor="#000031" link="##91DAFF" vlink="##91DAFF">
<table height="100%" width="100%" border="0" cellpadding="0">
<td width="115" align="left" valign="top" rowspan="2">
<font color="##91DAFF" face="verdana" size="1">
JRSearch<br>
<br>
</font>
<img src="../skull.gif">
</td>
<td width="1" bgcolor="#FFFFFF"align="left" valign="top" rowspan="2">
</td>
<td background="../jr.gif" align="left" valign="top">
<font color="#F8BF24" face="verdana" size="1">
Please select a database and type in your query.
</font>
<form method="get" action="search.php">
<table cellpadding="0" cellspacing="0" width="100%">
<td valign="top">
<font color="#FFFFFF" face="verdana" size="1">
Database:<br>
<select name="database" size="4">
<option>1. Anime</option>
<option>2. Graphics</option>
<option>3. Miscellaneous</option>
<option>4. Music</option>
<option>5. Reference</option>
<option>6. Video Games</option>
<option>7. World Wide Web</option>
</select><br><br>
<input type="text" name="sendstring" size="19" maxlength="40">
<input type="submit" value="Search">
<input type="reset" value="Clear">
</font>
</td>
<td align="right" valign="bottom">
<font color="#FFFFFF" face="verdana" size="1">
<font color="#F8BF24">
*IMPORTANT*<br>
</font>
<br>
<a href="tips.html" target="_self"><b>Tips for using JRSearch.</b></a><br>
<br>
<a href="submit.html" target="_blank">Submit a site.</a><br>
<br>
<a href="about.html" target="_self">About JRSearch.</a><br>
<br>
<a href="../contact/contact.html" target="_main">Dead or broken link?</a>
</font>
</td>
</table>
</form>
<?php
switch(count($_GET))
{
case 0:
echo '<iframe src="results.php" width="100%" height="*"></iframe>';
break;
default:
echo '<iframe src="results.php?sendstring=' . stripslashes($_GET['sendstring']) . '&database=' . stripslashes($_GET['database']) . '" width="100%" height="*"></iframe>';
break;
}
?>
<font color="#F8BF24" face="verdana" size="1">
<br><br>
Jolly Roger's Ship accepts no responsibility for the content on any website featured herein. The content provided by these websites is the sole product of its respective web personnel. You are free to search at own risk if you understand this, otherwise we suggest you exit this site.
</font>
</td>
</table>
</body>
</html>
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#3 |
|
King of Portal
|
Filename: results.php
Purpose: To find sites based on user input and output them in a structured manner. <html>
<head>
<title>
Search Results
</title>
</head>
<body background="../jr.gif" bgcolor="#000031" link="##91DAFF" vlink="##91DAFF">
<font color="#FFFFFF" face="verdana" size="1">
<?php
switch(count($_GET))
{
case 0:
echo 'Welcome to JRSearch.';
break;
default:
$searchstring = strtolower($_GET['sendstring']);
$searchbase = $_GET['database'];
if($searchstring == NULL)
{
die('You did not enter a search term.');
}
if($searchbase == NULL)
{
die('You did not select a database.');
}
$searchbase = substr($searchbase, 0, strpos($searchbase, "."));
$i = 1;
switch($searchbase)
{
case $i++:
$filename = 'anime';
$searchbase = 'anime';
break;
case $i++:
$filename = 'graphics';
$searchbase = 'graphics';
break;
case $i++:
$filename = 'misc';
$searchbase = 'miscellaneous';
break;
case $i++:
$filename = 'music';
$searchbase = 'music';
break;
case $i++:
$filename = 'reference';
$searchbase = 'reference';
break;
case $i++:
$filename = 'vgames';
$searchbase = 'video games';
break;
case $i++:
$filename = 'web';
$searchbase = 'world wide web';
break;
}
$filename .= '.txt';
if(!$handle = fopen($filename, 'r'))
{
echo "Cannot open file ($filename)";
exit();
}
$contents = fread($handle, filesize($filename));
fclose($handle);
$end_tag = 'EOINPUT';
$thread_contents = explode($end_tag, $contents);
unset($thread_contents[count($thread_contents) - 1]);
echo '<font color="#F8BF24"><strong>';
echo intval(count($thread_contents) / 4);
echo '</strong></font> total sites in <font color="#F8BF24"><strong>';
echo strtoupper($searchbase);
echo'</strong></font> database<br><br>';
$n = 0;
for($i = 0; $i < count($thread_contents); $i += 4)
{
$title = $thread_contents[$i + 0];
$desc = $thread_contents[$i + 1];
$url = $thread_contents[$i + 2];
$kword = strtolower($thread_contents[$i + 3]);
$kword .= 'list_all';
if(is_int(strpos($kword, $searchstring)))
{
$n ++;
echo '<font color="#F8BF24"><strong>';
echo $n;
echo '</strong></font>. <a href="';
echo $url;
echo '" target="_blank">';
echo $title;
echo '</a><br>';
echo $desc;
echo '<hr>';
}
}
if($n == 0)
{
echo 'No results found.';
}
break;
}
?>
</font>
</body>
</html>
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#4 |
|
King of Portal
|
The text files that the search engine looks through are structured in the following manner:
TitleEOINPUTDescriptionEOINPUTurlEOINPUTkeywordsEOINPUT The EOINPUT are the tags used to separate the content based on heredoc syntax. So in the previous structure you'd only have one website. For instance, my website's particular descriptor would look like this: Jolly Roger's Ship :: Ver. 1.3EOINPUTJolly Roger's Ship, the only website on the net powered by atomic kittens.EOINPUThttp://grimpirate.t35.com/EOINPUTgrimpirate, grim pirate, jr, jolly roger's ship, ray dream, search engine, jrsearch, haohmaru faq, haohmaru strategy guide, samurai showdown trading cards, vagrant skull, online comic, guilty gear gif, guilty gear x gif, anime reviews, anime intros, anime openings, super robot wars alpha buddy icons, dungeons and dragons, dnd accurate combat engine, nyko air flo pc controller, mugen, cd cases, cd slips, ansi c program, source code, php5, jrforum, forum, webboardEOINPUT The title, description, and keywords are taken directly from the particular site from the meta tags available in the headers of most main pages of a site. If there's more than one website in a particular database, the descriptors are appended to one another.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#5 |
|
Professional Programmer
|
Hmm. Interesting stuff, thanks for sharing.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|