Nettuts+ recently published a very nice article summarising “30+ Best Practices for PHP Beginners” . The article is a must read for all PHP beginners. Check out the article here :
http://net.tutsplus.com/tutorials/php/30-php-best-practices-for-beginners/
Archive for the ‘php’ Category
30+ Best Practices for PHP Beginners : Nettuts+
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 :
/* 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.
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 :
{
//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
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.
$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 ****************/
Wordpress – How to display featured posts in your theme
“Featured Posts” are a key feature of any news/journal wordpress site. It is pretty easy to develop a wordpress theme to display a fixed number of featured posts from a specific category on the homepage. It can be done using the query_posts() function of wordpress. Here are few lines of code that displays latest 10 posts from the news category , you can edit it to tailor to your needs.
/* Name of your category you want to show posts from */
$cat = "news";
/* No. of posts to display*/
$num = "10";
?>
<!– Show x Posts from $cat category–>
<?php query_posts(’showposts=’.$num.‘&category_name=’.$cat.”);
while (have_posts()) : the_post();
?>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content(‘Continue…’); ?>
<p class="postmetadata">Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’, ”, ‘ | ‘); ?> <?php comments_popup_link(‘No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?></p>
<a href="<?php the_permalink(); ?>" class="permalink">Link to this article</a>
</div>
<?php endwhile; ?>
The function query_posts(); can be used to control which posts are displayed in the loop. More information on using query_posts(); can be found here : http://codex.wordpress.org/Template_Tags/query_posts .
