Norkevicius

September 12, 2008

anonymous, dynamic, variable classes and methods

Filed under: Side Notes, php — Tags: — aurimas.norkevicius @ 2:53 pm

To call a method by dynamic name you can do this:

$methodName="MyMethod";
$result=$methodName();

To create a class by dynamic name you can do this:

$className="MyClass";
$classVar=new $className;

To call any method by dynamic name in previously initialized class you can do this:

$methodName="MyMethod";
$result=$classVar->$methodName();

November 26, 2007

Php Script To Detect And Verify Googlebot

Filed under: php — aurimas.norkevicius @ 10:37 pm
function IsGooglebot(){
// check if user agent contains googlebt
if(eregi("Googlebot",$_SERVER['HTTP_USER_AGENT'])){
$ip = $_SERVER['REMOTE_ADDR'];
//server name e.g. crawl-66-249-66-1.googlebot.com

$name = gethostbyaddr($ip);
//check if name ciontains googlebot
if(eregi("Googlebot",$name)){
//list of IP's

$hosts = gethostbynamel($name);
foreach($hosts as $host){
if ($host == $ip){
return true;
}
}
return false; // Pretender, take some action if needed
}else{
return false; // Pretender, take some action if needed
}
}else{
// Not googlebot, take some action if needed
}
return false;
}

July 20, 2007

global or $_GLOBAL[] Versus local variables

Filed under: Side Notes, php — aurimas.norkevicius @ 10:29 pm

Did some tests and found out that scripts of type

$a=5;
function foo(){
global $a;
return $a +5;
}
foo();

have relatively bad performance ….

try to use something like

$a=5;
function foo($a){
return $a +5;
}
foo($a);

instead …

Powered by WordPress