Webmaster общности: Predpriemach.com | SearchEngines.bg

    Търсачка с подобни на Google резултати

    Автор: SandPrince

    Тъй като никой не ми даде решение на въпроса с исканата от мен търсачка седнах и си потърсих. за моя изненада намерих това което най много се доближава до това което исках. реших да ви го пусна като урок , за тези на които би им потрябвало.

    Първо ето структурата на търсачката


    Основна папка
    config.php
    index.php
    google.php
    xml-keith-devens.php
    Подпапки към основната и съдържание
    папка: img
    -logo.gif
    -search.gif
    папка: inc
    -content.php
    -focus.js
    -home.php
    -style.css

    Започваме урока
    Основна папка – файлове
    index.php

    <?php
    ob_start();
    
    include "config.php";
    include "google.php";
    include "xml-keith-devens.php";
    ?>
    
    <?php
    // Always work with $_GET variables
    if ($_POST['q']){
    $query = str_replace(" ", "+", $_POST['q']);
    header("location:?q=" . $query . "&start=0");
    }
    ?>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <?php
    if ($_GET['q']){
    ?>
    <title><?php echo stripslashes(stripslashes(html_entity_decode(str_replace("+", " ", $_GET['q'])))); ?> - Found with Search</title>
    <link rel="stylesheet" href="inc/style.css">
    <script type="text/javascript" src="inc/focus.js"></script>
    
    <?php
    }else{
    ?>
    <title>Search - Your personal search engine</title>
    <link rel="stylesheet" href="inc/home.css">
    <script type="text/javascript" src="inc/focus.js"></script>
    
    <?php
    }
    ?>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body onLoad="focus();searchbox.q.focus()">
    <?php
    if ($_GET['q']){
    ?>
    <div id="header">
    <div id="logo"><a href="/search/" title="Go to the homepage"><img src="img/logo.gif" alt="Logo"></a></div>
    
    <form name="searchbox_results" id="searchbox_results" method="post" action="">
    <input name="q" type="text" id="q" value="<?php echo stripslashes(stripslashes(html_entity_decode(str_replace("+", " ", $_GET['q'])))); ?>" />
    <input type="submit" id="submit" name="Submit" value="<?php echo $word['Search']; ?>" />
    </form>
    </div>
    
    <?php
    }else{
    ?>
    <div id="header"><a href="/search/" title="Go to the homepage"><img src="img/logo.gif" alt="Logo"></a></div>
    
    
    <form name="searchbox" id="searchbox" method="post" action="">
    <input name="q" type="text" id="q" value="<?php echo stripslashes(stripslashes(html_entity_decode(str_replace("+", " ", $_GET['q'])))); ?>" />
    <input type="submit" id="submit" name="Submit" value="" />
    </form>
    
    <?php
    }
    ?>
    
    
    <?php
    if (isset($_GET['q'])) {
    
    if (!$_GET['start']) $_GET['start'] = 0;
    
    // Determine the GoogleCH
    $rank = getpagerank($_GET['q']);
    
    $_GET['q'] = str_replace(" ", "+", $_GET['q']);
    
    // Het the search results in an PHP array
    
    $xml = file_get_contents("http://www.google.com/search?client=navclient-auto&hl=" . $language . "&ch=" . $rank . "&q=" . $_GET['q']."&start=" . ( $_GET['start'] ? $_GET['start'] : 0 ) . "&output=xml");
    $data = XML_unserialize($xml);
    
    // Number of results
    $numberOfResults = $data['GSP']['RES']['M'];
    $start = $_GET['start'] + 1;
    $eind = $start + 9;
    
    // Query
    $query = stripslashes(stripslashes($data['GSP']['Q']));
    
    echo "<div id=\"query\"><span id=\"websearch\"><script type=\"text/javascript\"><!--
    google_ad_client = \"pub-5245192906531811\";
    google_alternate_color = \"cfe0e9\";
    google_ad_width = 468;
    google_ad_height = 15;
    google_ad_format = \"468x15_0ads_al\";
    google_ad_channel = \"\";
    google_color_border = \"99cc66\";
    google_color_bg = \"99cc66\";
    google_color_link = \"000000\";
    google_color_text = \"000000\";
    google_color_url = \"A50F02\";
    //--></script>
    <script type=\"text/javascript\"
    src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
    </script></span> <span id=\"results\"><em>". $words['Results'] . " <strong>" . $start . " - " . $eind . "</strong> " . $words['of about'] . " <strong>" . $numberOfResults . "</strong> " . $words['for'] . " <strong>" . $query . "</strong> </em></span></div> <div id=\"serps\"><p style=\"float:right\"><script type=\"text/javascript\"><!--
    google_ad_client = \"pub-5245192906531811\";
    google_alternate_color = \"ffffff\";
    google_ad_width = 160;
    google_ad_height = 600;
    google_ad_format = \"160x600_as\";
    google_ad_type = \"text_image\";
    google_ad_channel = \"\";
    google_color_border = \"ffffff\";
    google_color_bg = \"ffffff\";
    google_color_link = \"0066aa\";
    google_color_text = \"000000\";
    google_color_url = \"000000\";
    //--></script>
    <script type=\"text/javascript\"
    src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
    </script></p>\n";
    
    // Print the results
    if ($data['GSP']['RES attr']['SN'] == $data['GSP']['RES attr']['EN']){
    
    echo "<p><i>" . $word['No results can be shown.'] . "</i></p>";
    
    }else{
    
    // There are multiple results
    while (list ($key, $val) = each ($data['GSP']['RES']['R'])){
    
    if (substr($key, strlen($key) - 4, 4) != "attr"){
    
    echo " <h4><a href=\"" . $val['U'] . "\">" . $val['T'] . "</a></h4>
    <p class=\"result\">" . ( strlen(trim($val['S'])) > 0 ? $val['S'] . "<br />" : "" ) . "
    <a class=\"url\" href=\"" . $val['U'] . "\">" . substr($val['U'],7,80) . "</a></p>";
    
    }
    
    }
    
    }
    
    // Prepare the query for the URL
    $_GET['q'] = stripslashes($_GET['q']);
    $_GET['q'] = htmlspecialchars($_GET['q']);
    $query = $_GET['q'];
    $query = str_replace('"', "%22", $query);
    
    // Navigation
    $start = ($_GET['start']/10) - 9;
    $eind = ($_GET['start']/10) + 10;
    $eind = max($eind, 10);
    echo "<p id=\"pagenav\"><strong>More results:</strong>\n";
    for ($i = $start; $i <= $eind; $i++){
    
    $start = 10 * ($i - 1);
    
    // Check to see if this page lies between the borders
    if ($i >= 1 && $i <= 100 && $i <= ceil($numberOfResults/10)){
    
    if ($start == $_GET['start']){
    echo $i . " \n ";
    }else{
    echo " <a href=\"index.php?q=" . $query. "&start=" . $start . "\">" . $i . "</a> \n ";
    }
    
    }
    
    }
    echo "</p></div>\n";
    
    }
    ?>
    
    <div id="footer">
    © 2007 <a href="http://www.google-script.com">Google Script</a>
    </div>
    
    </body>
    </html>
    
    <?php
    ob_get_contents();
    ?>

    config.php

    <?php
    /* Language indicator of the Google SERP's */
    $language = "en";
    
    /* Words:
    You can translate the words below into your language. */
    $words = array();
    $words['Results'] = "Results";
    $words['of about'] = "of about";
    $words['for'] = "for";
    $word['No results can be shown.'] = "No results can be shown";
    $word['Search'] = "Search";
    ?>

    google.php

    <?php
    define('GOOGLE_MAGIC', 0xE6359A60);
    
    function zeroFill($a, $b)
    {
    $z = hexdec(80000000);
    if ($z & $a)
    {
    $a = ($a>>1);
    $a &= (~$z);
    $a |= 0x40000000;
    $a = ($a>>($b-1));
    }
    else
    {
    $a = ($a>>$b);
    }
    return $a;
    }
    
    
    function mix($a,$b,$c) {
    $a -= $b; $a -= $c; $a ^= (zeroFill($c,13));
    $b -= $c; $b -= $a; $b ^= ($a<<8);
    $c -= $a; $c -= $b; $c ^= (zeroFill($b,13));
    $a -= $b; $a -= $c; $a ^= (zeroFill($c,12));
    $b -= $c; $b -= $a; $b ^= ($a<<16);
    $c -= $a; $c -= $b; $c ^= (zeroFill($b,5));
    $a -= $b; $a -= $c; $a ^= (zeroFill($c,3));
    $b -= $c; $b -= $a; $b ^= ($a<<10);
    $c -= $a; $c -= $b; $c ^= (zeroFill($b,15));
    
    return array($a,$b,$c);
    }
    
    function GoogleCH($url, $length=null, $init=GOOGLE_MAGIC) {
    if(is_null($length)) {
    $length = sizeof($url);
    }
    $a = $b = 0x9E3779B9;
    $c = $init;
    $k = 0;
    $len = $length;
    while($len >= 12) {
    $a += ($url[$k+0] +($url[$k+1]<<8) +($url[$k+2]<<16) +($url[$k+3]<<24));
    $b += ($url[$k+4] +($url[$k+5]<<8) +($url[$k+6]<<16) +($url[$k+7]<<24));
    $c += ($url[$k+8] +($url[$k+9]<<8) +($url[$k+10]<<16)+($url[$k+11]<<24));
    $mix = mix($a,$b,$c);
    $a = $mix[0]; $b = $mix[1]; $c = $mix[2];
    $k += 12;
    $len -= 12;
    }
    
    $c += $length;
    switch($len)
    {
    case 11: $c+=($url[$k+10]<<24);
    case 10: $c+=($url[$k+9]<<16);
    case 9 : $c+=($url[$k+8]<<8);
    case 8 : $b+=($url[$k+7]<<24);
    case 7 : $b+=($url[$k+6]<<16);
    case 6 : $b+=($url[$k+5]<<8);
    case 5 : $b+=($url[$k+4]);
    case 4 : $a+=($url[$k+3]<<24);
    case 3 : $a+=($url[$k+2]<<16);
    case 2 : $a+=($url[$k+1]<<8);
    case 1 : $a+=($url[$k+0]);
    }
    $mix = mix($a,$b,$c);
    return $mix[2];
    }
    
    function strord($string) {
    for($i=0;$i<strlen($string);$i++) {
    $result[$i] = ord($string{$i});
    }
    return $result;
    }
    
    function getpagerank($uri) {
    $ch = GoogleCH(strord($uri));
    $ch = "6$ch";
    return $ch;
    }
    
    ?>

    xml-keith-devens.php

    <?php
    ###################################################################################
    #
    # XML Library, by Keith Devens, version 1.2b
    # http://keithdevens.com/software/phpxml
    #
    # This code is Open Source, released under terms similar to the Artistic License.
    # Read the license at http://keithdevens.com/software/license
    #
    ###################################################################################
    
    ###################################################################################
    # XML_unserialize: takes raw XML as a parameter (a string)
    # and returns an equivalent PHP data structure
    ###################################################################################
    function & XML_unserialize(&$xml){
    $xml_parser = &new XML();
    $data = &$xml_parser->parse($xml);
    $xml_parser->destruct();
    return $data;
    }
    ###################################################################################
    # XML_serialize: serializes any PHP data structure into XML
    # Takes one parameter: the data to serialize. Must be an array.
    ###################################################################################
    function & XML_serialize(&$data, $level = 0, $prior_key = NULL){
    if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }
    while(list($key, $value) = each($data))
    if(!strpos($key, ' attr')) #if it's not an attribute
    #we don't treat attributes by themselves, so for an empty element
    # that has attributes you still need to set the element to NULL
    
    if(is_array($value) and array_key_exists(0, $value)){
    XML_serialize($value, $level, $key);
    }else{
    $tag = $prior_key ? $prior_key : $key;
    echo str_repeat("\t", $level),'<',$tag;
    if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
    while(list($attr_name, $attr_value) = each($data["$key attr"]))
    echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
    reset($data["$key attr"]);
    }
    
    if(is_null($value)) echo " />\n";
    elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
    else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
    }
    reset($data);
    if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
    }
    ###################################################################################
    # XML class: utility class to be used with PHP's XML handling functions
    ###################################################################################
    class XML{
    var $parser; #a reference to the XML parser
    var $document; #the entire XML structure built up so far
    var $parent; #a pointer to the current parent - the parent will be an array
    var $stack; #a stack of the most recent parent at each nesting level
    var $last_opened_tag; #keeps track of the last tag opened.
    
    function XML(){
    $this->parser = &xml_parser_create();
    xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
    xml_set_object($this->parser, $this);
    xml_set_element_handler($this->parser, 'open','close');
    xml_set_character_data_handler($this->parser, 'data');
    }
    function destruct(){ xml_parser_free($this->parser); }
    function & parse(&$data){
    $this->document = array();
    $this->stack = array();
    $this->parent = &$this->document;
    return xml_parse($this->parser, $data, true) ? $this->document : NULL;
    }
    function open(&$parser, $tag, $attributes){
    $this->data = ''; #stores temporary cdata
    $this->last_opened_tag = $tag;
    if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
    if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
    #this is the third or later instance of $tag we've come across
    $key = count_numeric_items($this->parent[$tag]);
    }else{
    #this is the second instance of $tag that we've seen. shift around
    if(array_key_exists("$tag attr",$this->parent)){
    $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
    unset($this->parent["$tag attr"]);
    }else{
    $arr = array(&$this->parent[$tag]);
    }
    $this->parent[$tag] = &$arr;
    $key = 1;
    }
    $this->parent = &$this->parent[$tag];
    }else{
    $key = $tag;
    }
    if($attributes) $this->parent["$key attr"] = $attributes;
    $this->parent = &$this->parent[$key];
    $this->stack[] = &$this->parent;
    }
    function data(&$parser, $data){
    if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
    $this->data .= $data;
    }
    function close(&$parser, $tag){
    if($this->last_opened_tag == $tag){
    $this->parent = $this->data;
    $this->last_opened_tag = NULL;
    }
    array_pop($this->stack);
    if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
    }
    }
    function count_numeric_items(&$array){
    return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
    }
    ?>

    Подпапка: inc – файлове
    content.php

    body {
    
    font-family:arial, verdana, sans-serif;
    font-size:.8em;
    }
    a, a:hover {
    text-decoration:underline;
    color:#06a;
    }
    
    a:visited {
    color:#6e4987;
    }
    img {
    border:0;
    margin:0;
    padding:0;
    }
    #header {
    height:100px;
    margin:0;
    padding:0;
    border-bottom:5px solid #069;
    }
    #header a {
    margin:0;
    padding:0;
    float:left;
    }
    #header span {
    float:right;
    }
    #container {
    width:720px;
    float:left;
    margin-top:25px;
    margin-bottom:25px;
    background:url(../img/background_over.gif) repeat-y;
    }
    #navigatie {
    width:200px;
    float:left;
    }
    #navigatie ul {
    list-style-type:none;
    margin:0;
    padding:0;
    }
    #navigatie ul li {
    display:block;
    background:#f1f1f1;
    border-bottom:1px solid white;
    }
    #navigatie ul li a {
    display:block;
    width:200px;
    color:#036;
    text-decoration:none;
    }
    #navigatie ul li a:hover {
    background:#ccc;
    text-decoration:none;
    }
    #content {
    width:500px;
    float:left;
    margin-left:20px;
    }
    
    
    
    
    
    
    #footer {
    clear:both;
    border-top:1px solid #369;
    margin-top:75px;
    padding-top:3px;
    text-align:center;
    font-size:.7em;
    }
    #footer a {
    text-decoration:none;
    color:#369;
    }

    home.php

    body {
    
    font-family:arial, verdana, sans-serif;
    font-size:.8em;
    }
    a, a:hover {
    text-decoration:underline;
    color:#06a;
    }
    
    a:visited {
    color:#6e4987;
    }
    img {
    border:0;
    margin:0;
    padding:0;
    }
    #header {
    height:50px;
    margin:0;
    padding:0;
    border-bottom:5px solid #069;
    }
    #header a {
    margin:0;
    padding:0;
    float:left;
    }
    #header span {
    float:right;
    }
    
    #searchbox {
    text-align:center;
    background:#9c6;
    margin-top:50px;
    padding:0;
    font-family:arial, verdana, sens-serif;
    font-weight:bold;
    text-transform:uppercase;
    font-size:16px;
    }
    #searchbox #q {
    width:250px;
    height:30px;
    margin-top:10px;
    margin-bottom:10px;
    border:2px solid #069;
    padding-left:2px;
    font-family:arial, verdana, sens-serif;
    font-weight:bold;
    font-size:20px;
    }
    #searchbox #submit {
    width:90px;
    padding:0;
    border:0;
    cursor:pointer;
    margin-top:10px;
    margin-bottom:10px;
    background:url(../img/search.gif) no-repeat;
    height:30px;
    }
    #searchbox_results {
    float:left;
    }
    #searchbox_results #q {
    width:250px;
    margin-top:15px;
    margin-left:25px;
    border:2px solid #069;
    padding-left:2px;
    }
    #searchbox_results #submit {
    width:70px;
    }
    
    #query {
    margin-top:10px;
    background:#cfe0e9;
    border-top:1px solid #025c96;
    border-bottom:1px solid #025c96;
    height:25px;
    }
    #websearch {
    float:left;
    color:#86c71d;
    font-size:1.3em;
    font-weight:bold;
    margin-top:4px;
    }
    #results {
    float:right;
    margin-top:4px;
    }
    
    #serps {
    margin:10px;
    }
    
    a.url {
    color:#36a200;
    text-decoration:none;
    }
    a.url:hover {
    color:#36a200;
    text-decoration:none;
    }
    
    h4 {
    font-weight:normal;
    color:#06a;
    margin:0;
    padding:0;
    }
    h4 a, a:hover {
    text-decoration:underline;
    color:#06a;
    }
    
    h4 a:visited {
    color:#6e4987;
    }
    
    p.result {
    margin-top:0;
    margin-bottom:10px;
    }
    p#pagenav {
    margin-top:30px;
    text-align:center;
    }
    #footer {
    border-top:1px solid #369;
    margin-top:75px;
    padding-top:3px;
    text-align:center;
    font-size:.7em;
    }
    #footer a {
    text-decoration:none;
    color:#369;
    }

    style.css

    body {
    
    font-family:arial, verdana, sans-serif;
    font-size:.8em;
    }
    a, a:hover {
    text-decoration:underline;
    color:#06a;
    }
    
    a:visited {
    color:#6e4987;
    }
    img {
    border:0;
    margin:0;
    padding:0;
    }
    #header {
    border-bottom:5px solid #069;
    height:50px;
    }
    #logo {
    float:left;
    width:150px;
    height:50px;
    }
    #searchbox {
    display:block;
    text-align:center;
    background:#9c6;
    margin-top:50px;
    padding:0;
    font-family:arial, verdana, sens-serif;
    font-weight:bold;
    text-transform:uppercase;
    font-size:16px;
    }
    #searchbox #q {
    width:250px;
    height:30px;
    margin-top:10px;
    margin-bottom:10px;
    border:2px solid #069;
    padding-left:2px;
    font-family:arial, verdana, sens-serif;
    font-weight:bold;
    font-size:20px;
    }
    #searchbox #submit {
    width:90px;
    padding:0;
    border:0;
    cursor:pointer;
    margin-top:10px;
    margin-bottom:10px;
    background:url(../img/zoeken.gif) no-repeat;
    height:30px;
    }
    #searchbox_results {
    float:left;
    }
    #searchbox_results #q {
    width:250px;
    margin-top:15px;
    margin-left:25px;
    border:2px solid #069;
    padding-left:2px;
    }
    #searchbox_results #submit {
    width:70px;
    }
    
    #query {
    margin-top:10px;
    background:#9c6;
    height:25px;
    }
    #websearch {
    float:left;
    color:#86c71d;
    font-size:1.3em;
    font-weight:bold;
    margin-top:4px;
    }
    #results {
    float:right;
    margin-top:4px;
    color:#036;
    }
    
    #serps {
    margin:10px;
    }
    
    a.url {
    color:#36a200;
    text-decoration:none;
    }
    a.url:hover {
    color:#36a200;
    text-decoration:none;
    }
    
    h4 {
    font-weight:normal;
    color:#06a;
    margin:0;
    padding:0;
    }
    h4 a, a:hover {
    text-decoration:underline;
    color:#06a;
    }
    
    h4 a:visited {
    color:#6e4987;
    }
    
    p.result {
    margin-top:0;
    margin-bottom:10px;
    }
    p#pagenav {
    margin-top:30px;
    text-align:center;
    }
    #footer {
    border-top:1px solid #369;
    margin-top:75px;
    padding-top:3px;
    text-align:center;
    font-size:.7em;
    }
    #footer a {
    text-decoration:none;
    color:#369;
    }

    focus.js

    function setBlurs(){
    var a = document.getElementsByTagName('a');
    if(a.length > 0){
    if(document.attachEvent){
    for(i = 0;i<a.length;i++){
    a[i].attachEvent('onfocus',blurElement);
    }
    }else if(document.addEventListener){
    for(i = 0;i<a.length;i++){
    a[i].addEventListener('focus',blurElement,false);
    }
    }
    }
    }
    
    function blurElement(e){
    if (!e){
    // IE kent alleen window.event
    var e = window.event;
    }
    
    if(e.target){
    // W3C methode
    var elem = e.target;
    }else if(e.srcElement){
    // IE methode
    var elem = e.srcElement;
    }
    if (elem.nodeType == 3){
    // Safari hack
    elem = elem.parentNode;
    }
    elem.blur();
    }
    window.onload=setBlurs;

    Урока е преведен до колкото можех специално за Web-tourist