Archive for December, 2009

offsetLeft - offsetTop Properties

Value: integer

Compatibility: WinIE4+, MacIE4+, Moz1+, Safari1+

The offsetTop and offsetLeft properties are valuable in providing pixel coordinates of an element within the positioning context  of the parent element even when the elements are not positioned explicitly. The element used as a coordinate context for these properties is whatever element the offsetParent property returns. This means that to determine the precise position of any element you may have to add some code that iterates through the offsetParent hierarchy until that property returns null.

Note: The offsetLeft and offsetTop properties for positioned elements in MacIE do not return the same values as the style.left and style.top properties of the same element.


offsetHeight - offsetWidth Properties

Value: integer

Compatibility: WinIE4+, MacIE4+, Moz1+, Safari1+

These properties report the height and width of any element, for a normal block-level element whose height and width are not specified the offsetHeight is determined by the actual height of the content after all text flows. But the offsetWidth always extends the full width of the containing element. For example , a p element consisting of only a few words may report a offsetwidth of many of hundreds of pixels because the p block extends the full width of the body element that represents the containing parent of the p element. To find out the actual width of text within a full-width block-level element wrap the text within an inline element (span) and inspect the offsetWidth property of the span.

Related items: clientHeight, clientWidth properties.


Caching Output

High-traffic sites can often benefit from caching of pages, to save processing of the same data over and over again.

Put the first function cache_start in the beginning of php script and the second cache_end in the end of script.

Code:

function cache_start($_time, $dir)
{
  $cachefile = $dir.'/'.sha1($_SERVER['REQUEST_URI']).'.html';
  $cachetime = $_time;
  ob_start();

  if(file_exists($cachefile) && (time( )-$cachetime < filemtime($cachefile)))
  {
    include($cachefile);
    ob_end_flush();
    exit;
  }
}

//——————————————–

function cache_end($dir)
{
  $cachefile = $dir.'/'.sha1($_SERVER['REQUEST_URI']).'.html';
  $fp = fopen($cachefile, 'w');
  fwrite($fp, ob_get_contents());
  fclose($fp);
  ob_end_flush();
}

//————————————–
$_time : cache time

$dir : directory to cache files


Catching Keywords from Search Engines

With this class we try to catch some keywords from google, yahoo and bing search engines.

Code:

<?php
class keywords
{
 private $referer;
 private $_e;
 public $keywords;

 public function __construct()
 {
   if($_SERVER['HTTP_REFERER'])
   {
     if(preg_match("#\.google|search\.yahoo|\.bing#", $_SERVER['HTTP_REFERER']))
     {
       $this->referer = urldecode($_SERVER['HTTP_REFERER']);
     }
     else
     {
       return;
     }    
   }
   else
   {
     return;
   }    
 }

 private function getSeparators()
 {
   $this->_e = (preg_match("#\?q=|\?p=#", $this->referer)) ? "\?" : "&";
 }

 public function getKeywords()
 {
   if(!empty($this->referer))
   {
     $this->getSeparators();
     //google
     if(preg_match("#\.google#", $this->referer))
     {
       $m_ = preg_match("#{$this->_e}q=(.+?)&#si", $this->referer, $this->keywords);

       if($m_ == 0)
       {
         return false;
       }
     }
     //yahoo
     elseif(preg_match("#search\.yahoo#", $this->referer))
     {
       $m_ = preg_match("#{$this->_e}p=(.+?)\&#si", $this->referer, $this->keywords);

       if($m_ == 0)
       {
         return false;
       }
     }
     //bing
     elseif(preg_match("#\.bing#", $this->referer))
     {
       $m_ = preg_match("#{$this->_e}q=(.+?)\&#si", $this->referer, $this->keywords);

       if($m_ == 0)
       {
         return false;
       }
     }
     else
     {
       return false;
     }

     return $this->keywords[1];
   }
   else
   {
     return false;
   }
  }
}
?>

Save this script as keywords_class.php

Now let’s try to print these keywords.

Code:

<?php
require_once('keywords_class.php');
$keywordsObj = new keywords();
$keys = $keywordsObj->getKeywords();

if($keys)
{
 print $keys;
}
else
{
 print "ooops";
}
?>

Save this code as index.php