How to know what keywords visitors Googled to reach your site?

Here is a simple php script that will detect if your visitor is coming from Google and then log the keyword used to reach your site to a file. The script logs the visitors ip address along with the keyword and exact date and time to a file in the form x.x.x.x googled for “key word here” on Month Date, Year, at Hours:minutes am/pm. To use the script, just paste the following code into your php pages , and set the variable $log_file appropriately.
Here is the code :

'
<?php

/* JUST A FEW SETTINGS FIRST
The log file name,
this is where the key words will be stored,
make sure to keep a difficult-to-guess name
for privacy reasons
*/
 
$log_file="google-keywords-log.log";

/*
 Set your timezone (just to make sure you can analyze the data better)
 Some useful time zone values :
 Asia/Karachi
 Asia/Dacca
 Asia/Qatar
 Asia/Muscat
 Asia/Tokyo
 for a complete list, refer to http://www.php.net/manual/en/timezones.php
*/
date_default_timezone_set("Asia/Calcutta");


// THE CODE
if (strpos($_SERVER['HTTP_REFERER'],'http://google') OR strpos($_SERVER['HTTP_REFERER'],'http://www.google')) {
$referer=$_SERVER['HTTP_REFERER'];
$url=parse_url($referer);
parse_str($url["query"],$query);
$data=$_SERVER['REMOTE_ADDR']." googled for "".$query["q"]."" on ".date("F j, Y,")." at ".date("g:i a")."\r\n\r\n";
$fp=fopen($log_file,"a+");
fwrite($fp,$data);
fclose($fp);

}
?>
'

Code Explained

We first check to see if our visitor has been referred to our site by google by checking the predefined variable $_SERVER[“HTTP_REFERRER”] for occurrences of the string http://www.google or http://google using the strpos() function. Then we parse the referrer url using parse_url() function. Then we go on to parse the “query” component of the array returned by parse_url() using the parse_str() function which parses the strings as if it were the query string passed via a URL and sets variables in the current scope. Once the string is parsed into the array $query , we can access our keyword term at $query[“q”] .Finally we open our log file (set in $log_file ) in the append mode (a+) and append the visitors IP address, the keyword and date and time to the file.

 

Sniffing Googlebot using php

Ever wanted to know when Googlebot (google’s search engine spider) visits your site? . Googlebot can be sniffed (detected) easily using php. It is identifiable by the “Googlebot” string in the HTTP_USER_AGENT field it sends with the headers. Here is a simple function called is_google() which returns TRUE if the visitor is Googlebot, else FALSE.

'
//The function
function is_google() {
if (strpos($_SERVER['HTTP_USER_AGENT'],"Googlebot")) 
 return true
else
 return false
}
'

Usage

To use the function, just call it using an if statement, here is how :

'
if (is_google()) 
{
 //do something here if the visitor is Googlebot
//like log the date and time to a file/database
}
'


Some common uses of this function can be :

  • Analyzing the visit of Googlebot to your sites
  • Spamdexing,Cloaking ( deceiving )
    serving different content (like bulk of links,high ranking keywords etc…) to Googlebot for better rankings
NOTE
Using this function for cloaking and spamdexing is a black hat SEO technique and can cause your site to be blacklisted by Google
 

Parsing “ini” configuration files using php


Many times during development involving php, one would want to store some variables as settings. The most common way developers do this is by defining those variables at the beginning of the script. For example, while writing a contact form script, I would like to define a few handy variables first, including admin email, contact log file name/path and etc… The most common way to do this would be to write something similar in your php script.

'
/******* S E T T I N G S *********/
$admin_email="you@yourdomain.com";
$file="logs/contact.log";
$success_msg="Thanks! your submission was recieved, we will get back to you soon!";
/****** E N D ****************/
'

Read the rest of the article on Devils Workshop

 

jQuery : How to highlight alternate rows of a table

While displaying large chunks of data in a table with lots of rows, it is really desireable to have a different background color for alternating rows. While there is no easy way you can do that using css alone, it is highly impractical to manually add a alt class to each alternate tr (table row). But thanks to the powerful selector engine of jQuery, you can very easily select an alternate tr . Here is how :

'

$("#mytable tr:odd").addClass("alt");

'

The above code selects every odd table row of the table with id mytable and adds a class alt to each of theme. Next you can style the alt class rows (tr) using css to make them stand out and add a nice zebra strip effect to your table.

 
2019 All rights reserved | Powered by WordPress
Back to top
Theme by dynamicguru.com