ארכיון פוסטים מהחודש "נובמבר, 2012"

Check if user agent is GoogleBot

יום ראשון, 18 בנובמבר, 2012

In this post i will provide an example in php how to check if the request is made by Google search engine, typicily each browser have its own signature for example Mozila ,Explorer,Safari we are capable of reaing the user agent information from server variable called HTTP_USER_AGENT ,  Google search bots are identified with speicial UserAgent named Googlebot here is an example for a code that check the user Agent

<?php

if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Googlebot' ) !== false )

{

// paste your email address here

$my_email = 'xxx@Gmail.com';

// notify via email

mail($my_email,'[Notification]Googlebot Visit', 'Googlebot has just visited your website WEBSITE_NAME: '.$_SERVER['REQUEST_URI']);

}

else

{

echo ("<!– Regular user not google –>");

}

Working with Cookies with php

יום ראשון, 18 בנובמבר, 2012

Cookies are used to store data on the remote browser at the user side, this is usefull for example for saving username or userid so each time the user browse to URL the php will ask from the remote browsers its cookies then it will be able to gather the userid.

here is an example

<?php // Uniq Visitor

// Check if remote browser have visitor id cookie on the browser

if (!isset($_COOKIE['vistid']))

{

// No the user does not have visitor ID we will give him an allocate for vistor ID

$count = file_get_contents("count.txt");

$count = trim($count); $count = $count + 1;

$fl = fopen("count.txt","w+");

fwrite($fl,$count); fclose($fl);

// Sending a cookie to the remote browser

setcookie('vistid',$count,time()+3600*24*365);

echo "<!– New Visitor: id=" . $count . "–>";

}

else

{

// User have visitor ID we will just print it

echo "<!– vistor id:"  .  $_COOKIE['vistid'] . " –>";

} // End of visitors

?>