Hi all

I am a beginner and i was wondering can we write a code in PHP by which we can enter keyword in search box of any website and submit.

so there will be 2 inputs to this php function:

function enter_keyword($website_url, $keyword)
{

}

It will be a great help
thanks in advance

dnewshelf commented: seach bos +0

Recommended Answers

All 7 Replies

Are you talking about a 'web page' or an entire 'web site'. Because with a webpage you can just use file_get_contents() to retrieve the data then use regex to find the keywords. However, if you are talking about a 'website' or network of pages then that will require a bot that indexs the website to a database and the search form would check the database. Both ways I can help you just need to know which one.

Are you talking about a 'web page' or an entire 'web site'. Because with a webpage you can just use file_get_contents() to retrieve the data then use regex to find the keywords. However, if you are talking about a 'website' or network of pages then that will require a bot that indexs the website to a database and the search form would check the database. Both ways I can help you just need to know which one.

Hi cwarn23

thanks for the reply
following needs to be done::

1.) I want to send a keyword for eg: 'knowing' to website for eg: "http://stagevu.com/" search box and submit it.

2.) then i want to return the url of first result from the result page.

i know that (2.) can be done using CURL but what about first.

thanks again for taking out your time for replying
jyotiu

Although you didn't entirely answer my question (scan the website or single webpage) I will assume you want to scan the website in which case will require a bot. I have recently written a bot to scan for site security holes and the bot template is as follows:

<?
set_time_limit(0);
function domain($domainb) {
	$bits = explode('/', $domainb);
	if ($bits[0]=='http:' || $bits[0]=='https:')
		{
		return $bits[0].'//'.$bits[2].'/';
		} else {
		return 'http://'.$bits[0].'/';
		}
	unset($bits);
	}
if (isset($_GET['site'])) {
    echo '<head><title>Bot scanning website - '.domain($_GET['site']).'</title></head><body>';
    } else {
    echo '<head><title>Bot scanner</title></head><body>';
    }
echo '<center><font size=5 face=\'arial black\'><b>PHP Bot Scanner</b></font><br><form method=\'get\' style=\'margin:0px; padding:0px;\'><input type=\'text\' name=\'site\' size=64 value="'.$_GET['site'].'"><input type=\'submit\' value=\'Scan\'></form></center>';
if (substr_replace($_GET['site'],'',3)=='ftp') {
exit('You may not connect to the ftp protocole');
}
if (!isset($_GET['site'])) { exit(''); }

$_GET['site']=domain($_GET['site']);

function url_exists($durl)
		{
		// Version 4.x supported
		$handle   = curl_init($durl);
		if (false === $handle)
			{
			return false;
			}
		curl_setopt($handle, CURLOPT_HEADER, true);
		curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
		curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox   
		curl_setopt($handle, CURLOPT_NOBODY, true);
		curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
		$connectable = curl_exec($handle);
		curl_close($handle);  
        if (preg_match('/200 OK/i',substr_replace($connectable,'',30))) {
            return true;
            } else {
            return false;
            }
		}
//below function will only get links within own domain and not links outside the site.
function getlinks($generateurlf) {
    $datac=file_get_contents($generateurlf);
    preg_match_all('/(href|src)\=(\"|\')[^\"\'\>]+/i',$datac,$media);
    unset($datac);
    $datac=preg_replace('/(href|src)(\"|\'|\=\"|\=\')(.*)/i',"$3",$media[0]);
    $datab=array();
    foreach($datac AS $dfile) {
        $generateurle=$generateurlf;
        if (!in_array(substr_replace($dfile,'',4),array('http','www.'))) {
            if (substr_replace($generateurle,'',0, -1)!=='/') {
                $generateurle=preg_replace('/(.*)\/[^\/]+/is', "$1", $generateurle);
                } else {
                $generateurle=substr_replace($generateurle,'',-1);
                }

            if (substr_replace($dfile,'',1)=='/') {
                if (domain($generateurle)==domain($generateurle.$dfile)) {
                    if (in_array(strtolower(preg_replace('/(.*)[.]([^.\?]+)(\?(.*))?/','$2',basename($generateurle.$dfile))),array('html','htm','xhtml','xml','mhtml','xht','mht','asp','aspx','adp','bml','cfm','cgi','ihtml','jsp','las','lasso','lassoapp','pl','php','php1','php2','php3','php4','php5','php6','phtml','shtml','search','query','forum','blog','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','01','02','03','04','05','06','07','08','09','go','page','file')) || substr($generateurle.$dfile,-1)=='/' || !preg_match('/[\.]/i',basename($generateurle.$dfile))) {
                        $datab[]=$generateurle.$dfile;
                        }
                    }
            } else if (substr_replace($dfile,'',1)=='.') {
                while (preg_match('/\.\.\/(.*)/i', $dfile)) {
                $dfile=substr_replace($dfile,'',0,3);
                $generateurle=preg_replace('/(.*)\/[^\/]+/i', "$1", $generateurle);
                }
                if (domain($generateurle)==domain($generateurle.'/'.$dfile)) {
                    if (in_array(strtolower(preg_replace('/(.*)[.]([^.\?]+)(\?(.*))?/','$2',basename($generateurle.'/'.$dfile))),array('html','htm','xhtml','xml','mhtml','xht','mht','asp','aspx','adp','bml','cfm','cgi','ihtml','jsp','las','lasso','lassoapp','pl','php','php1','php2','php3','php4','php5','php6','phtml','shtml','search','query','forum','blog','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','01','02','03','04','05','06','07','08','09','go','page','file')) || substr($generateurle.'/'.$dfile,-1)=='/' || !preg_match('/[\.]/i',basename($generateurle.'/'.$dfile))) {
                        $datab[]=$generateurle.'/'.$dfile;
                        }
                    }
                } else {
                if (domain($generateurle)==domain($generateurle.'/'.$dfile)) {
                    if (in_array(strtolower(preg_replace('/(.*)[.]([^.\?]+)(\?(.*))?/','$2',basename($generateurle.'/'.$dfile))),array('html','htm','xhtml','xml','mhtml','xht','mht','asp','aspx','adp','bml','cfm','cgi','ihtml','jsp','las','lasso','lassoapp','pl','php','php1','php2','php3','php4','php5','php6','phtml','shtml','search','query','forum','blog','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','01','02','03','04','05','06','07','08','09','go','page','file')) || substr($generateurle.'/'.$dfile,-1)=='/' || !preg_match('/[\.]/i',basename($generateurle.'/'.$dfile))) {
                        $datab[]=$generateurle.'/'.$dfile;
                        }
                    }
                }
            } else {
            if (domain($generateurle)==domain($dfile)) {
                if (in_array(strtolower(preg_replace('/(.*)[.]([^.\?]+)(\?(.*))?/','$2',basename($dfile))),array('html','htm','xhtml','xml','mhtml','xht','mht','asp','aspx','adp','bml','cfm','cgi','ihtml','jsp','las','lasso','lassoapp','pl','php','php1','php2','php3','php4','php5','php6','phtml','shtml','search','query','forum','blog','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','01','02','03','04','05','06','07','08','09','go','page','file')) || substr($dfile,-1)=='/' || !preg_match('/[\.]/i',basename($dfile))) {
                    $datab[]=$dfile;
                    }
                }
            }
        }
    unset($datac);
    unset($dfile);
    return $datab;
    }

$loopurl['sites']=array($_GET['site']);
foreach (getlinks($_GET['site']) AS $link) {
    if (!in_array($link,$loopurl['sites'])) {
        $loopurl['sites'][]=$link;
        }
    }
unset($link);






















function generate($genurl) {
    $data=file_get_contents($genurl);
    //add there what you want to do with the page contents in the variable $data.
    }


for ($loopid=0;isset($loopurl['sites'][$loopid]);$loopid++) {
    if (url_exists($loopurl['sites'][$loopid])) {
        foreach (getlinks($loopurl['sites'][$loopid]) AS $link) {
            if (!in_array($link,$loopurl['sites'])) {
                $loopurl['sites'][]=$link;
                }
            }
        unset($link);
        
        echo generate($loopurl['sites'][$loopid]);
        flush();
        }
    usleep (5000);
    }
echo '<br><b>Bot scan complete.</b></body>';
?>

And to edit this, just place in the generate() function the code you want performed on each page. Also the only parts that should be modified are after the giant space.
Just a note on the theory behind this. The bot above will scan the website and index all the pages into a database then whenever somebody searches a website that is in the index then it can check the most relevent pages within the selected website. Alternatively you can piggy back off google.

commented: Great solving, thanks! +0

okay let me explain again what i want to do.
I will create a simple php page which will have a input box and a submit button.

this input box will be use to insert any keyword. when a person will hit submit...on the back end... i need a script that will submit/post this keyword to a given websites searchbox/search page.

than i will scrap first results url and return to my page.

does that make sense?

Is this what your looking for? It uses google to search a selected site for selected keywords.

<?php

function search($search_term,$site)
{
global $dsite;
$bits = explode('/', $site);
if ($bits[0]=='http:' || $bits[0]=='https:')
	{
	$site=$bits[0].'//'.$bits[2].'/';
	} else {
	$site='http://'.$bits[0].'/';
	}
$dsite=$site;
$site=urlencode($site);
$search_term=urlencode($search_term);
$curl_handle=curl_init('http://www.google.com.au/search?hl=en&q=site%3A'.$site.'+'.$search_term.'&meta=');
curl_setopt($curl_handle, CURLOPT_HEADER, false);
curl_setopt($curl_handle, CURLOPT_FAILONERROR, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox   
curl_setopt($curl_handle, CURLOPT_POST, false);
curl_setopt($curl_handle, CURLOPT_NOBODY, false);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,4);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

$bufferb=strip_tags($buffer,'<cite>');
preg_match_all("/<cite>[^ ]+ - [0-9]+k - <\/cite>/",$bufferb,$match['url']);
unset($bufferb);
$match['url'][0]=preg_replace('/<cite>([^ ]+) - [0-9]+k - <\/cite>/','$1',$match['url'][0]);
$bufferb=strip_tags($buffer,'<br><div>');
preg_match_all("/<div[^>]+>[^<]+<br>/",$bufferb,$match['des']);
unset($bufferb);





$bufferb=strip_tags($buffer,'<a>');
preg_match_all("/<a href=\"[^\"]+\"\ class\=l[^>]+>[^<]+<\/a>/",$bufferb,$match['title']);
$id=0;
while (isset($match['title'][0][$id]))
    {
    $match['title'][0][$id]=strip_tags($match['title'][0][$id]);
    $id+=1;
    }

$result['url']=$match['url'][0];
$result['des']=$match['des'][0];
$result['title']=$match['title'][0];
unset($match);
unset($buffer);
unset($bufferb);
unset($id);

return $result;
}

echo "<form method='post' style='margin:0; padding:0;'><table border=0 cellpadding=0 cellspacing=0>
<tr><td align='right'>Website:</td><td><input type='text' size=40 name='site'></td></tr>
<tr><td align='right'>Search Term:</td><td><input type='text' size=40 name='searchval'><input type='submit' value='search'></td></tr></table></form><br>";
if (isset($_POST['searchval']) && strlen($_POST['searchval'])>=1)
    {
    $result=search($_POST['searchval'],$_POST['site']);
    $id=0;
    echo "<table border=0 cellspacing=0 cellpadding=0 width=640><tr><td bgcolor='#66CCFF'><table border=0 cellpadding=3 cellspacing=0><tr><td>".
    '<b>Website searched: '.$dsite.'<br> There were '.count($result['title'])." results found with the term '<i>".$_POST['searchval']."</i>'</b></td></tr></table></td></tr>";
    while (isset($result['url'][$id]) && isset($result['des'][$id]))
        {
        echo '<tr><td><a href="http://'.$result['url'][$id].'"><font color=#0000FF>'.$result['title'][$id].'</font></a></td></tr><tr><td>'.$result['des'][$id].'</td></tr><tr><td height="16px"></td></tr>';
        $id+=1;
        }
    echo "</table>";
    }
?>
commented: He is the Great cwarn23!!! +1

Hi cwarn23
you are my angel :)

Thanks a ton

I too am trying to create a search box on my website that will allow readers to type in search words and display results strictly from all pages of my .org. I think the last php code given above will do this? So I have created a page called "action_page.php" to process the keyword search request with the above php code you wrote. I am missing something because it is all not working. I can type a keyword in the box and click submit but no results. Do I need to create a separate display results page? Or can I make the results display on the current page? Or do I need to edit any of the php code with my specific web address?

My html is as follows

<form role="search" action="/action_page.php" method="get"> <div class="search-container"> <input type="search" id="mySearch" name="q"
     placeholder="Search the site..."
     aria-label="Search through site content"> <button type="submit"><i class="fa fa-search"></i></button> </div> </form> 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.