Welcome to Vestris Inc.
Internet Interactive Solutions Company



Software Documentation

How can I integrate Alkaline search in a php website?

The details and the code in this FAQ allow alkaline to run on the same port as your web content, demonstrates boolean and or not operator implementation, and provides php the result next and results count to be parsed within the overall html document as separate variables. It also disallows the * search string, if so required.

  • Create two php files both with the same name (i.e index.php).

  • Place one file in your alkaline server directory and the other in your root web directory.

  • Ensure the directories also have the same name, i.e. http://www.foo.com:9999/bar/index.php should correspond with http://www.foo.com/bar/index.php.

  • Copy and paste the following code into your alkaline php document.
    <!--SET MAP--
     <table>
      <tr>
        <td width="5%">&nbsp;</td>
        <td><a href="$url">$title</a></td>
        <td align="right" width="5%">$quality%</td>
      </tr> 
      <tr>
        <td><a href="$url">*</a></td>
        <td colspan="2">
          <br>$header ...
          <br>$size bytes; modified: $modif
        </td>
      </tr>
      <tr>
        <td colspan="2">url: <a href="$url" >$url</a></td>
        <td>&nbsp;</td>
      </tr>
     </table>
    -->
    
    <next>
     <!--SEARCH-NEXT-->
     <!--SET NEXT--<B>next</B>-->
     <!--SET C-BEFORE--<b><font color="red">-->
     <!--SET C-AFTER--</b></font>-->
     <!--SET N-BEFORE--<b class="block4">-->
     <!--SET N-AFTER--</b>-->
    </next>
    <hits>
     <!--SEARCH-GENERAL total hits: $total--> 
    </hits>
    <results>
     <!--SEARCH-RESULTS--> 
     <!--SET SEARCH-NORESULTS--no results <hr noshade>--> 
    </results>

  • Copy the following code into your web server php document.
    <?php
    // define server variabl
    // keep server as localhost unless your web server 
    // doesn't allow this as this provides optimal retrieval time
    
    $server = "localhost";  // server that Alkaline runs from
    $port = "9999";       // port that Alkaline runs from
    
    // the following two must be identical on your web server and Alkaline server
    // ie www.foo.com/search/index.php must correspond to 
    // www.foo.com:9999/search/index.php
    
    $dirname = "search/"; // the directory from which you run this script from (note the '/')
    $docname = "index.php"; // name of this script.
    
    // define Alkaline variables
    
    $boolean = "on"; // allow AND OR NOT boolean terms
    $allrecords = "off"; // ensures * cannot be used to return all records
    
    // main section
        
    $rawsearch = urldecode($search);
    if (strlen($rawsearch) < 1)
    {
     $rawsearch = urldecode(substr(stristr($QUERY_STRING, "search="), 7));
    }
    
    $runfrom = $dirname.$docname;
    if (trim($search) == "" || ($allrecords = "off" && trim($search)== "*" )) 
     unset($search);    // kills the $search variable with undesirable searches
    
    if (isset($search)) 
    {
     if ($boolean = "on")    
     {
      $search = str_replace("and ", urlencode("+"), $search);
      $search = str_replace("or ", " ", $search);
       $search=str_replace("not ", urlencode("-"), $search);
     }
    
     $fp = fopen("http://$server:$port/$runfrom?search=$search", "r");
    }
    elseif (isset($QUERY_STRING))
    {
     $fp = fopen("http://$server:$port/$runfrom?$QUERY_STRING","r");
    }
    
    if($fp) 
    {
     while (!feof ($fp))
     {
      $buffer .= fgets($fp, 4096);
     }
    
     $results = get_tag($buffer, "results");
     $next = get_tag($buffer, "next");
     $hits = get_tag($buffer, "hits");
    }
    
    // define html variables
    
    // any head content    
    $headcontent = "<title>searching for $search with Alkaline</title>";
    // any body header content
    $body_top = "<hr>";
    // any body footer content
    $body_bottom = "<hr>";
    // page text, shown if nothing has been searched
    $alternate_text = "Welcome to Alkaline";
    
    // search form
    
    $topform=\"
     <form action=\"$docname\" method=\"post\">
      <input name=\"search\" type=\"text\" value=\"$rawsearch\">
      <input type=\"submit\" value=\"Search!\">
     </form>";
    
    // write body
    
    function get_tag($source, $tagname) 
    {  
     ereg("<$tagname>(.*)</$tagname>", $source, $tmp);
     return $tmp[1];
    }
    
    echo "<html>
           <head>$headcontent</head>
           <body>
            $hits
            $topform
            $body_top";
    
    if (isset($results))
    {
     echo $results;
     echo $next;
    }
    else
    {
     echo $alternate_text;
    }
    
    echo "  $body_bottom
           </body>
          </html>";
    ?>

The basics for this example are courtesy of www.gespong.com . The current full example is courtesy if www.lawgenecentre.org .