PHP cloaking script

Sometimes you need to show another text to crawlers :)
This script check serfer User-Agent and if this UA is listed in variable $bot_lst - so, it’s bot…

  1. // List of bots:
  2. $bot_lst=array(
  3.              'google',
  4.              'msn',
  5.              'yahoo'
  6.            );
  7.  
  8. $is_bot=0;
  9. for($i=0; $i<sizeof($bot_lst); $i++)
  10.   if(strstr(strtolower($HTTP_SERVER_VARS['HTTP_USER_AGENT']), strtolower($bot_lst[$i])))
  11.     $is_bot=1;
  12.  
  13. if($is_bot) echo 'Hello bot :)';
  14. else echo 'Hi user :]';
  15.  
  16. echo '<p>Your UserAgent: '.$HTTP_SERVER_VARS['HTTP_USER_AGENT'].'</p>';

Tags: , , , , , ,

:) A job for ukrainian programmer

Test-task from http://www.dp76.com/php-test/

I wrote this script in three minutes… Maybe I can get a cool php-programmer job :)

  1. // http://www.dp76.com/php-test/
  2.  
  3. $s='1|0|Electronics
  4. 2|0|Video
  5. 3|0|Photo
  6. 4|1|MP3 player
  7. 5|1|TV
  8. 6|4|iPod
  9. 7|6|Shuffle
  10. 8|3|SLR
  11. 9|8|DSLR
  12. 10|9|Nikon
  13. 11|9|Canon
  14. 12|11|20D';
  15.  
  16. $s=explode("\n", $s);
  17. $items=array();
  18. foreach($s as $item){
  19.   $s1=array();
  20.   $s1=explode('|', trim($item));
  21.   $items[$s1[1]][$s1[0]]=$s1[2];
  22. }
  23.  
  24. echo '<pre>'.CatLst(0).'</pre>';
  25.  
  26.  
  27. function CatLst($parent_id, $indent=''){
  28. global $items;
  29.   if(isset($items[$parent_id])){
  30.     $out='';
  31.     while(list($item_key, $item_val)=each($items[$parent_id])){
  32.       $out.=$indent.$item_val.' (id: '.$item_key.', parent_id: '.$parent_id.")\r\n";
  33.       $out.=CatLst($item_key, $indent."\t");
  34.     }
  35.   }
  36.  
  37.   return $out;
  38. }

Tags: , ,