Archives for category —featured

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 refered to our site by google by checking the predefined variable $_SERVER["HTTP_REFERER"] for occurences of the string http://www.google or http://google using the strpos() function. Then we parse the referer 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 adress, 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

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.

5 very useful online web tools

Online web tools

Web Tools is a collection of online tools developed to make your task easier. Currently it features 5 tools including :

  1. md5 hash calculator
  2. sha1 hash calculator
  3. HTML to ASCII convertor
  4. HTML and php tags stripper
  5. & Base 64 encoder and decoder

The tools are developed using some pretty basic php functions , but still are of great use. The functions used are :

sha1(); //for sha1 hash calculator
md5(); //for md5 hash calculator
strip_tags(); //strips all php and html tags
htmlspecialchars(); //converts all html characters to their corresponding ASCII characters
base64_encode(); //encodes a string in base64 format
base64_decode(); //decodes a string from base64 format

Cufon – easy and fast text-image replacement solution

Cufon - easy and fast text-image replacement solutionHave you ever wanted to use your own font on your website? but bothered about it being installed on your visitors system? Cufon is the solution for you, it makes possible fast and easy replacement of text with the font of your wish. It is extremely easy to set up and supports nearly all major browsers. And also it is SEO friendly.
Here is a demo of Cufon on my portfolio site, those headings with silver gradients are actually text replaced by Cufon on the fly.

Using Cufon

To use Cufon on your site , first download this js file (http://cufon.shoqolate.com/js/cufon-yui.js) and then generate your own font’s js file by uploading your font file here : http://cufon.shoqolate.com/generate/ . Once you have both the javascript files , include it in the head section of your web page like this :

<script src="path/to/cufon-yui.js" type="text/javascript" ></script>
<script src="path/to/generated-font-file.js" type="text/javascript" ></script>

Now we are all set up to replace your text , to replace the text with cufon , add the following piece of javascript to your page :

<script type="text/javascript">
Cufon.replace(‘h1′);
</script>

The above lines of code will replace all your h1 elements with images rendered in your font.
You can take a look at a more detailed guide to replacing text with cufon here : http://wiki.github.com/sorccu/cufon/usage

Styling your rendered text

You can also add linear gradients to your text with cufon!, here is how :

Cufon.replace(‘h1′, {
        color: ‘-linear-gradient(#000, #777, #777, #000)’
});

The above code will cause the text to fade from black (#000) to grey (#777) and then back.For a comprehensive guide to styling using cufon refer to : http://wiki.github.com/sorccu/cufon/styling
So why wait? go ahead and try it out today, i bet its awesome and the easiest text-image replacement solution available right now, much simpler and better that sIFR.

© 2010 Dynamic Guru All rights reserved — About UsContactMujtaba