Jump to content

Saving keywords in an Array


natasha_thomas

Recommended Posts

Friends,

 

Requirement:

I want to Extract the Keywords from All the Search Engine Referer URLs and save all the keywords in an Array so i can Echo/show all the "popular Terms" which drive traffic to my Site from Search Engines.

 

I already have the Code for Extracting the Keyword from URL, can someone tell me how can i save the $keyword in an array that whenever i visit my site, i can see that What all keywords brought visitors to my site from Search Engines?

 

Code to Extract Keywords from URL:

<?
$referrer = $_SERVER['HTTP_REFERER'];
$referrer ='http://www.google.co.id/search?hl=id&client=firefox-a&hs=IKn&rls=org.mozilla%3Aen-US%3Aofficial&channel=s&q=pemahaman+pelajaran+ips+tentang+endogen+dan+eksogen&aq=f&aqi=&aql=&oq=&gs_rfai=';
$referrer_query = parse_url($referrer);
$referrer_host = $referrer_query['host'];
$referrer_query = $referrer_query['query'];
$q = "[q|p|qkw|key|query|searchfor|Keywords|searchterm]"; //Yahoo uses both query strings, I am using switch() for each search engine
preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword);
$keyword = urldecode($keyword[1]);

$potongnya = array('&q=','&p=','key=','query=','searchfor=','Keywords=','searchterm=');
$referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query);
$arr = explode("&", "$referrer_query");
for($k = 0, $l = count($arr); $k < $l; ++$k)
{
$ygdicari = "$arr[$k]";
$findmeyach = "|pencarian=";
$posnyaaa = strpos($ygdicari, $findmeyach);
if ($posnyaaa === false)
{}
else
{
$keyword = str_replace('|pencarian=', "", $ygdicari);
$potongkeynya = array('+','%20');
$keyword = str_replace($potongkeynya, " ", $keyword);
}
}
if ($keyword=="")
{
$potongnya = array('q=','p=');
$referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query);
$arr = explode("&", "$referrer_query");
for($k = 0, $l = count($arr); $k < $l; ++$k)
{
$ygdicari = "$arr[$k]";
$findmeyach = "|pencarian=";
$posnyaaa = strpos($ygdicari, $findmeyach);
if ($posnyaaa === false)
{}
else
{
$keyword = str_replace('|pencarian=', "", $ygdicari);
$potongkeynya = array('+','%20');
$keyword = str_replace($potongkeynya, " ", $keyword);
}
}

}
$keyword = strtolower($keyword);
$urikeyword = array('sex ','porn ','porno',', ','penis ','penis ','http://www.','http://','http','gamble','"',"'");
$keyword = str_replace($urikeyword, "", "$keyword");
echo "$keyword";
exit;
?>

 

 

Many Thanks

Natasha T

Link to comment
Share on other sites

I hate to be one of those people who writes a comment that doesn't answer your question.... but....

 

you're better off saving all the keywords in a database.  If you used an array you would need to then convert it to a string and store it in a flat file somewhere on your site, then when you wanted to view the data, convert it back to an array and parse through it programatically.  A database would be much easier and provide simple searchability as well.

 

Link to comment
Share on other sites

I hate to be one of those people who writes a comment that doesn't answer your question.... but....

 

you're better off saving all the keywords in a database.  If you used an array you would need to then convert it to a string and store it in a flat file somewhere on your site, then when you wanted to view the data, convert it back to an array and parse through it programatically.  A database would be much easier and provide simple searchability as well.

 

What you said make lot of sense.

 

I do not want Database because of some reasons.

 

But saving it all in a flatfile (.txt) and fetching back from Text file make sense.

 

So, could you help me with the codes as in, how to save the $keyword whenever a New Keyword s Extracted from URL.

 

and displaying all the keywords form the flat file on page.

 

Will be Grateful for your help.

 

Natasha T

Link to comment
Share on other sites

but anyways..if you insist on moving forward with your plan...if you are looking to save an array to a flatfile and retrieve and add to it, etc... look into serialize and unserialize.  Basically you would do something like :

 

// get current list from your file
$keywords = file_get_contents('file.txt');
$keywords = unserialize($keywords);

// add new keyword to list
$keywords[] = $keyword;

// put it back into text file
$keywords = serialize($keywords);
file_put_contents('file.txt',$keywords);

 

But depending on traffic and what kind of history of keywords you want to keep track of...file could get to large for something like this. Will have to read /write one line at a time with fopen/fread/fwrite.  But even then...using a database is your best overall option.

Link to comment
Share on other sites

but anyways..if you insist on moving forward with your plan...if you are looking to save an array to a flatfile and retrieve and add to it, etc... look into serialize and unserialize.  Basically you would do something like :

 

// get current list from your file
$keywords = file_get_contents('file.txt');
$keywords = unserialize($keywords);

// add new keyword to list
$keywords[] = $keyword;

// put it back into text file
$keywords = serialize($keywords);
file_put_contents('file.txt',$keywords);

 

But depending on traffic and what kind of history of keywords you want to keep track of...file could get to large for something like this. Will have to read /write one line at a time with fopen/fread/fwrite.  But even then...using a database is your best overall option.

 

Dear Crayon,

 

My purpose is not to track Keywords but to Hyperlink those Keywords in my Web pages for SEO Purposes, so i can rank higher for these keywords which i am not optimized for but still get traffic for.

 

I thank you for your script.

 

My observations:

After adding your codes the output is like:

a:2:{i:0;s:8:"keyword1";i:1;s:8:"keyword2";}

 

It should rather be like:

keyword1

keyword2

 

And, i want to hyperlink each of these outputs so is it posible to call these keywords from Flatflile with Foreach() or so....

 

I want to hyperlink each output keyword with this code:

$keywordname_url = stripslashes(str_replace('+', '-', urlencode($keywordname)));
$navigation.= "<class='departments'><a href='".($keywordname_url).".htm'>".Ucwords($keywordname)."</a> | ";

 

What code changes shall we do?

 

Cheers

Natasha T

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

okay then as mentioned, you need to use fopen, fread and fwrite along with a loop to loop through each word on in the file.

 

Dear Crayon,

I have come out with

 

$fp = fopen('file.txt', 'w');
fwrite($fp, $keyword);

while (!feof($fp)) { 
        $content .= fread($fp, 1024);

    }
    fclose($fp);
foreach ($content as $kw)
{
echo $kw;

}

 

I am not able to make it work  :-[

 

Could you tell me, what changes in the code required?

 

Cheers

NT

Link to comment
Share on other sites

<?php

$filename = 'test.txt';


// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

   
    if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $kewyword) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($keyword) to file ($filename)";
    
    while (!feof($filename) { 
        $content .= fread($fp, 1024);

    }
    fclose($fp);
foreach ($content as $kw)
{
echo $kw;

} else {
    echo "The file $filename is not writable";
}


?>

 

Hope this helps  :shy:

Link to comment
Share on other sites

<?php

$filename = 'test.txt';


// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

   
    if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $kewyword) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($keyword) to file ($filename)";
    
    while (!feof($filename) { 
        $content .= fread($fp, 1024);

    }
    fclose($fp);
foreach ($content as $kw)
{
echo $kw;

} else {
    echo "The file $filename is not writable";
}


?>

 

Hope this helps  :shy:

 

Many Thanks darkFresk

 

Based on your code, here is my Full Code, which Giving an Error:

<?
$referrer = $_SERVER['HTTP_REFERER'];
$referrer ='http://www.google.com/search?hl=en&source=hp&biw=1109&bih=736&q=law+of+attraction&aq=f&aqi=&aql=&oq=&gs_rfai=';
$referrer_query = parse_url($referrer);
$referrer_host = $referrer_query['host'];
$referrer_query = $referrer_query['query'];
$q = "[q|p|qkw|key|query|searchfor|Keywords|searchterm]"; //Yahoo uses both query strings, I am using switch() for each search engine
preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword);
$keyword = urldecode($keyword[1]);

$potongnya = array('&q=','&p=','key=','query=','searchfor=','Keywords=','searchterm=');
$referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query);
$arr = explode("&", "$referrer_query");
for($k = 0, $l = count($arr); $k < $l; ++$k)
{
$ygdicari = "$arr[$k]";
$findmeyach = "|pencarian=";
$posnyaaa = strpos($ygdicari, $findmeyach);
if ($posnyaaa === false)
{}
else
{
$keyword = str_replace('|pencarian=', "", $ygdicari);
$potongkeynya = array('+','%20');
$keyword = str_replace($potongkeynya, " ", $keyword);
}
}
if ($keyword=="")
{
$potongnya = array('q=','p=');
$referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query);
$arr = explode("&", "$referrer_query");
for($k = 0, $l = count($arr); $k < $l; ++$k)
{
$ygdicari = "$arr[$k]";
$findmeyach = "|pencarian=";
$posnyaaa = strpos($ygdicari, $findmeyach);
if ($posnyaaa === false)
{}
else
{
$keyword = str_replace('|pencarian=', "", $ygdicari);
$potongkeynya = array('+','%20');
$keyword = str_replace($potongkeynya, " ", $keyword);
}
}

}
$keyword = strtolower($keyword);
$urikeyword = array('sex ','porn ','porno',', ','penis ','penis ','http://www.','http://','http','gamble','"',"'");
$keyword = str_replace($urikeyword, "", "$keyword");



$filename = 'file.txt';


// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

   
    if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $kewyword) === FALSE) {
        //echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($keyword) to file ($filename)";
    
    while (!feof($filename)) { 
        $content .= fread($fp, 1024);

    }
    fclose($fp);
foreach ($content as $kw)
{
echo $kw;
} ?>

 

 

Error Message:

Parse error: syntax error, unexpected $end in /home/******/public_html/master/dev/kwlogger.php on line 83

 

 

May you help me spotting the Error?

 

Cheers

Natasha T

Link to comment
Share on other sites

You have an opening "{" on this line:

<?php
if (is_writable($filename)) {
?>

but no closing "}".

 

You can rewrite the two blocks

<?php
	if ($posnyaaa === false)
	{}
	else
	{
		$keyword = str_replace('|pencarian=', "", $ygdicari);
		$potongkeynya = array('+','%20');
		$keyword = str_replace($potongkeynya, " ", $keyword);
	}
?>

as

<?php
	if ($posnyaaa !== false)
	{
		$keyword = str_replace('|pencarian=', "", $ygdicari);
		$potongkeynya = array('+','%20');
		$keyword = str_replace($potongkeynya, " ", $keyword);
	}
?>

 

Ken

Link to comment
Share on other sites

You have an opening "{" on this line:

<?php
if (is_writable($filename)) {
?>

but no closing "}".

 

You can rewrite the two blocks

<?php
	if ($posnyaaa === false)
	{}
	else
	{
		$keyword = str_replace('|pencarian=', "", $ygdicari);
		$potongkeynya = array('+','%20');
		$keyword = str_replace($potongkeynya, " ", $keyword);
	}
?>

as

<?php
	if ($posnyaaa !== false)
	{
		$keyword = str_replace('|pencarian=', "", $ygdicari);
		$potongkeynya = array('+','%20');
		$keyword = str_replace($potongkeynya, " ", $keyword);
	}
?>

 

Ken

 

Dear ken,

 

I have applied your first suggestions and removed the File Writablity Check completely, as its not required,

 

Here is the code aftr that:

<?
$referrer = $_SERVER['HTTP_REFERER'];
$referrer ='http://www.google.com/search?hl=en&source=hp&biw=1109&bih=736&q=law+of+attraction&aq=f&aqi=&aql=&oq=&gs_rfai=';
$referrer_query = parse_url($referrer);
$referrer_host = $referrer_query['host'];
$referrer_query = $referrer_query['query'];
$q = "[q|p|qkw|key|query|searchfor|Keywords|searchterm]"; //Yahoo uses both query strings, I am using switch() for each search engine
preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword);
$keyword = urldecode($keyword[1]);

$potongnya = array('&q=','&p=','key=','query=','searchfor=','Keywords=','searchterm=');
$referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query);
$arr = explode("&", "$referrer_query");
for($k = 0, $l = count($arr); $k < $l; ++$k)
{
$ygdicari = "$arr[$k]";
$findmeyach = "|pencarian=";
$posnyaaa = strpos($ygdicari, $findmeyach);
if ($posnyaaa === false)
{}
else
{
$keyword = str_replace('|pencarian=', "", $ygdicari);
$potongkeynya = array('+','%20');
$keyword = str_replace($potongkeynya, " ", $keyword);
}
}
if ($keyword=="")
{
$potongnya = array('q=','p=');
$referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query);
$arr = explode("&", "$referrer_query");
for($k = 0, $l = count($arr); $k < $l; ++$k)
{
$ygdicari = "$arr[$k]";
$findmeyach = "|pencarian=";
$posnyaaa = strpos($ygdicari, $findmeyach);
if ($posnyaaa === false)
{}
else
{
$keyword = str_replace('|pencarian=', "", $ygdicari);
$potongkeynya = array('+','%20');
$keyword = str_replace($potongkeynya, " ", $keyword);
}
}

}
$keyword = strtolower($keyword);
$urikeyword = array('sex ','porn ','porno',', ','penis ','penis ','http://www.','http://','http','gamble','"',"'");
$keyword = str_replace($urikeyword, "", "$keyword");



$filename = 'file.txt';


// Let's make sure the file exists and is writable first.


   
    if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $kewyword) === FALSE) {
        //echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($keyword) to file ($filename)";
    
    while (!feof($filename)) { 
        $content .= fread($fp, 1024);

    }
    fclose($fp);
foreach ($content as $kw)
{
echo $kw;
} ?>

 

Once run, there is an Infinite loop Going on Fread(0

 

Output:

Success, wrote (law of attraction) to file (file.txt)

Warning: feof(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 75

 

Warning: fread(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 76

 

Warning: feof(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 75

 

Warning: fread(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 76

 

Warning: feof(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 75

 

Warning: fread(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 76

 

Warning: feof(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 75

 

So on...............

 

 

What could be wrong?

Link to comment
Share on other sites

You open the file with

<?php
$handle = fopen($filename, 'w')
?>

so you need to use the variable $handle when ever you reference the opened file. Change

<?php
    while (!feof($filename)) { 
        $content .= fread($fp, 1024);

    }
    fclose($fp);
?>

to

<?php
    while (!feof($handle)) { 
        $content .= fread($handle, 1024);

    }
    fclose($handle);
?>

 

Ken

Link to comment
Share on other sites

You open the file with

<?php
$handle = fopen($filename, 'w')
?>

so you need to use the variable $handle when ever you reference the opened file. Change

<?php
    while (!feof($filename)) { 
        $content .= fread($fp, 1024);

    }
    fclose($fp);
?>

to

<?php
    while (!feof($handle)) { 
        $content .= fread($handle, 1024);

    }
    fclose($handle);
?>

 

Ken

 

 

As you asked, i have changed the codes....

Here they are:

<?
$referrer = $_SERVER['HTTP_REFERER'];
$referrer ='http://www.google.com/search?hl=en&source=hp&biw=1109&bih=736&q=law+of+attraction&aq=f&aqi=&aql=&oq=&gs_rfai=';
$referrer_query = parse_url($referrer);
$referrer_host = $referrer_query['host'];
$referrer_query = $referrer_query['query'];
$q = "[q|p|qkw|key|query|searchfor|Keywords|searchterm]"; //Yahoo uses both query strings, I am using switch() for each search engine
preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword);
$keyword = urldecode($keyword[1]);

$potongnya = array('&q=','&p=','key=','query=','searchfor=','Keywords=','searchterm=');
$referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query);
$arr = explode("&", "$referrer_query");
for($k = 0, $l = count($arr); $k < $l; ++$k)
{
$ygdicari = "$arr[$k]";
$findmeyach = "|pencarian=";
$posnyaaa = strpos($ygdicari, $findmeyach);
if ($posnyaaa === false)
{}
else
{
$keyword = str_replace('|pencarian=', "", $ygdicari);
$potongkeynya = array('+','%20');
$keyword = str_replace($potongkeynya, " ", $keyword);
}
}
if ($keyword=="")
{
$potongnya = array('q=','p=');
$referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query);
$arr = explode("&", "$referrer_query");
for($k = 0, $l = count($arr); $k < $l; ++$k)
{
$ygdicari = "$arr[$k]";
$findmeyach = "|pencarian=";
$posnyaaa = strpos($ygdicari, $findmeyach);
if ($posnyaaa === false)
{}
else
{
$keyword = str_replace('|pencarian=', "", $ygdicari);
$potongkeynya = array('+','%20');
$keyword = str_replace($potongkeynya, " ", $keyword);
}
}

}
$keyword = strtolower($keyword);
$urikeyword = array('sex ','porn ','porno',', ','penis ','penis ','http://www.','http://','http','gamble','"',"'");
$keyword = str_replace($urikeyword, "", "$keyword");



$filename = 'file.txt';


// Let's make sure the file exists and is writable first.


   
    if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $kewyword) === FALSE) {
        //echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($keyword) to file ($filename)";
    
     while (!feof($handle)) { 
        $content .= fread($handle, 1024);

    }
    fclose($handle);
foreach ($content as $kw)
{
echo $kw;
} ?>

 

 

Output:

Success, wrote (law of attraction) to file (file.txt)

 

1- Its does not write anything to File.txt, i opend and confirmed.

- File is already 755 CHMOD and does exist

 

 

On another Note, there is an Infinite loop Again.. without any eror message this time....  :-\

 

Link to comment
Share on other sites

Try using 777 Permissions  ;)

 

Dear DarkFresk,

 

Am really struggling my way through to make it work.  :'(

 

Here is the code am suing..

 

<?
$referrer = $_SERVER['HTTP_REFERER'];
$referrer ='http://www.google.com/search?hl=en&source=hp&biw=1109&bih=736&q=law+of+attraction&aq=f&aqi=&aql=&oq=&gs_rfai=';
$referrer_query = parse_url($referrer);
$referrer_host = $referrer_query['host'];
$referrer_query = $referrer_query['query'];
$q = "[q|p|qkw|key|query|searchfor|Keywords|searchterm]"; //Yahoo uses both query strings, I am using switch() for each search engine
preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword);
$keyword = urldecode($keyword[1]);

$potongnya = array('&q=','&p=','key=','query=','searchfor=','Keywords=','searchterm=');
$referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query);
$arr = explode("&", "$referrer_query");
for($k = 0, $l = count($arr); $k < $l; ++$k)
{
$ygdicari = "$arr[$k]";
$findmeyach = "|pencarian=";
$posnyaaa = strpos($ygdicari, $findmeyach);
if ($posnyaaa === false)
{}
else
{
$keyword = str_replace('|pencarian=', "", $ygdicari);
$potongkeynya = array('+','%20');
$keyword = str_replace($potongkeynya, " ", $keyword);
}
}
if ($keyword=="")
{
$potongnya = array('q=','p=');
$referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query);
$arr = explode("&", "$referrer_query");
for($k = 0, $l = count($arr); $k < $l; ++$k)
{
$ygdicari = "$arr[$k]";
$findmeyach = "|pencarian=";
$posnyaaa = strpos($ygdicari, $findmeyach);
if ($posnyaaa === false)
{}
else
{
$keyword = str_replace('|pencarian=', "", $ygdicari);
$potongkeynya = array('+','%20');
$keyword = str_replace($potongkeynya, " ", $keyword);
}
}

}
$keyword = strtolower($keyword);
$urikeyword = array('sex ','porn ','porno',', ','penis ','penis ','http://www.','http://','http','gamble','"',"'");
$keyword = str_replace($urikeyword, "", "$keyword");



$filename = 'file.txt';


// Let's make sure the file exists and is writable first.


   
    if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $kewyword) === FALSE) {
        //echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($keyword) to file ($filename)";
    
     while (!feof($handle)) { 
        $content .= fread($handle, 1024);

    }
    fclose($handle);
foreach ($content as $kw)
{
echo $kw;
} ?>

 

Output:

 

Success, wrote (law of attraction) to file (file.txt)

 

But,

There is an Infinite Loop going once it echos the Above Message.

Nothing is Written in file.txt, i have tried 755, 777 both.

 

 

May you plz help me with This?

 

Cheers

Natasha T

Link to comment
Share on other sites

You misspelled the variable name "$keyword" in this line:

<?php
if (fwrite($handle, $kewyword) === FALSE) {
?>

so nothing is being written to your file.  I would also add a end-of-line character after the data being written:

<?php
if (fwrite($handle, $keyword . "\n") === FALSE) {
?>

The reason for the apparent infinite loop is that you're trying to read the file before you close it and the code is blocking the read. Change

<?php

     while (!feof($handle)) { 
        $content .= fread($handle, 1024);

    }
    fclose($handle);
?>

to

<?php
fclose($handle);
$content = file('file.txt');
?>

 

Ken

Link to comment
Share on other sites

You misspelled the variable name "$keyword" in this line:

<?php
if (fwrite($handle, $kewyword) === FALSE) {
?>

so nothing is being written to your file.  I would also add a end-of-line character after the data being written:

<?php
if (fwrite($handle, $keyword . "\n") === FALSE) {
?>

The reason for the apparent infinite loop is that you're trying to read the file before you close it and the code is blocking the read. Change

<?php

     while (!feof($handle)) { 
        $content .= fread($handle, 1024);

    }
    fclose($handle);
?>

to

<?php
fclose($handle);
$content = file('file.txt');
?>

 

Ken

 

Based on what all you said, here is my New Code

<?
$referrer = $_SERVER['HTTP_REFERER'];
$referrer ='http://www.google.com/search?hl=en&source=hp&biw=1109&bih=736&q=hehheh&aq=f&aqi=&aql=&oq=&gs_rfai=';
$referrer_query = parse_url($referrer);
$referrer_host = $referrer_query['host'];
$referrer_query = $referrer_query['query'];
$q = "[q|p|qkw|key|query|searchfor|Keywords|searchterm]"; //Yahoo uses both query strings, I am using switch() for each search engine
preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword);
$keyword = urldecode($keyword[1]);

$potongnya = array('&q=','&p=','key=','query=','searchfor=','Keywords=','searchterm=');
$referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query);
$arr = explode("&", "$referrer_query");
for($k = 0, $l = count($arr); $k < $l; ++$k)
{
$ygdicari = "$arr[$k]";
$findmeyach = "|pencarian=";
$posnyaaa = strpos($ygdicari, $findmeyach);
if ($posnyaaa === false)
{}
else
{
$keyword = str_replace('|pencarian=', "", $ygdicari);
$potongkeynya = array('+','%20');
$keyword = str_replace($potongkeynya, " ", $keyword);
}
}
if ($keyword=="")
{
$potongnya = array('q=','p=');
$referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query);
$arr = explode("&", "$referrer_query");
for($k = 0, $l = count($arr); $k < $l; ++$k)
{
$ygdicari = "$arr[$k]";
$findmeyach = "|pencarian=";
$posnyaaa = strpos($ygdicari, $findmeyach);
if ($posnyaaa === false)
{}
else
{
$keyword = str_replace('|pencarian=', "", $ygdicari);
$potongkeynya = array('+','%20');
$keyword = str_replace($potongkeynya, " ", $keyword);
}
}

}
$keyword = strtolower($keyword);
$urikeyword = array('sex ','porn ','porno',', ','penis ','penis ','http://www.','http://','http','gamble','"',"'");
$keyword = str_replace($urikeyword, "", "$keyword");



$filename = 'file.txt';


// Let's make sure the file exists and is writable first.


   
    if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
if (fwrite($handle, $keyword . "\n") === FALSE) {
        //echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($keyword) to file ($filename)";
    

fclose($handle);
$content = file('file.txt');

foreach ($content as $kw)
{
echo $kw;
} ?>

 

Observations:

Its Adding Keyword to text file but its Overwriting any existing  value in Text file, it should rather Append, so i can build up a list of keywords in text file.

 

What changes we need?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.