Showing posts with label new ideas. Show all posts
Showing posts with label new ideas. Show all posts

Sunday, November 16, 2008

PHP Robot Check

I've seen quite a few methods for determining if a request was made by a robot or search engine spider using PHP over the years, and they always have something to do with checking the User Agent. That works, but you have to keep up with User Agent strings which can get to be annoying.

I had an idea how to check for robots using PHP and htaccess today that doesn't involve a User Agent string at all. Instead it uses sessions and exploits the fact that all well behaved robots are going to request robots.txt before they request anything else.

I start by making sure my usual robots.txt file is in place. Then I upload a robots.php file to the same location as robots.txt with a tiny bit of code in it.

<?php
session_start();
$_SESSION['robot'] = 1;
echo file_get_contents('robots.txt');
exit;
?>


All that does is start a session, set a robot $_SESSION variable that I can check in subsequent scripts, then return the contents of robots.txt.

In htaccess I have the following line which transparently redirects requests to robots.txt made by visitors/spiders to robots.php, which in turn returns the contents of robots.txt

RewriteEngine on
RewriteRule robots\.txt robots.php


Now in my applications I can easily check for robots and drop things like advertisement banners to speed up page loads since spiders don't look at advertisements anyway.

<?php
session_start();
echo isset($_SESSION['robot']) ? 'ROBOT !!' : 'Not a robot';
?>


Here are some of the benefits of doing it this way.

  1. I can continue to modify robots.txt as I normally would
  2. I don't need to keep up with changing User Agent strings
  3. Checking for the existance of a session variable is quicker than performing pattern matching on a variable

Friday, June 20, 2008

Random Color Generation With Javascript

I'm sure there's quite a few ways to get a random color using JS, I can think of a few such as returning a random index from an array of CSS color names, or generating three random numbers between zero and 255 and formatting from there.

The following method is my favorite though because the single randomly generated integer can be easily converted into pretty much any other format or quickly returned as-is.
To illustrate this I've written the following function which will return a hexidecimal, RGB, or integer format random color depending on the argument passed to the function.

// @format (hex|rgb|null) : Format to return, default is integer
function random_color(format)
{
var rint = Math.round(0xffffff * Math.random());
switch(format)
{
case 'hex':
return ('#0' + rint.toString(16)).replace(/^#0([0-9a-f]{6})$/i, '#$1');
break;

case 'rgb':
return 'rgb(' + (rint >> 16) + ',' + (rint >> 8 & 255) + ',' + (rint & 255) + ')';
break;

default:
return rint;
break;
}
}

Tuesday, June 17, 2008

50 Random PHP5 Facts

Here's a list of 50 49 random facts about PHP.

  1. You MUST load PDO before loading any PDO drivers
  2. An iterator cannot be used with foreach by reference
  3. Cannot create references to elements of a temporary array expression
  4. Cannot create references to/from string offsets nor overloaded objects
  5. Cannot assign by reference to overloaded objects
  6. Only variable references should be returned by reference
  7. Only variables can be passed by reference
  8. Cannot return string offsets by reference
  9. Cannot use string offset as an array
  10. Cannot use object as array -- See Dans comment
  11. Cannot use a NULL value as an array
  12. Cannot use a scalar value as an array
  13. foreach() can not iterate over objects without PHP class
  14. Cannot use assign-op operators with overloaded objects nor string offsets
  15. __HALT_COMPILER() can only be used from the outermost scope
  16. Assigning the return value of new by reference is deprecated
  17. Variables passed to each() must be an array or object
  18. Constants may only evaluate to scalar values
  19. is_a() is deprecated. The "instanceof" operator should be used instead This has changed as of PHP 5.3, see Gregs comment
  20. Cannot add internal functions to return value from get_defined_functions()
  21. Cannot add user functions to return value from get_defined_functions()
  22. Static functions cannot be abstract
  23. Exceptions must be derived from the Exception base class
  24. Need to supply an object when throwing an exception
  25. Cannot declare self-referencing constants
  26. Cannot use call_user_function on objects without a class entry
  27. Cannot access self:: when no class scope is active
  28. Cannot access parent:: when no class scope is active
  29. Cannot access parent:: when current class scope has no parent
  30. add_assoc_function() is no longer supported
  31. Usage of {} to access string offsets is deprecated and will be removed in PHP 6
  32. Cannot re-assign $this
  33. __toString() must not throw an exception
  34. Can't use method return value in write context
  35. Can't use function return value in write context
  36. Multiple access type modifiers are not allowed
  37. Cannot use the final modifier on an abstract class member
  38. Default value for parameters with a class type hint can only be NULL
  39. Default value for parameters with array type hint can only be an array or NULL
  40. Cannot call __clone() method on objects - 'clone $obj' should be used instead
  41. Clone method does not require arguments
  42. Interfaces cannot not implement themselves
  43. Classes may not inherit from classes declared final
  44. Classes cannot inherit previously-inherited constants from interfaces
  45. Classes cannot implement previously-implemented interfaces
  46. Class declarations may not be nested
  47. Cannot use reserved words as class or interface names
  48. Interfaces may not include member variables
  49. Properties cannot be declared abstract
  50. Cannot declare properties final, the final modifier is allowed only for methods

Tuesday, September 25, 2007

Silky Directory Indexes

I really like the Silk Icons at famfamfam, alot more than the default FancyIndex icons used by Apache2 in fact.
So, now seems like a good time to patch those icons into my directory indexes for FancyIndexing.

First I get the zipfile fill of icons, which is actually a zipfile that has two readme files & a directory called icons in it.

Next I navigate to the directory Apache is already using for icons
Alias /icons/ "/usr/share/apache2/icons/"

In my case it's /usr/share/... look for "Alias /icons" in your *.conf file for Apache to find out where it is for you.

Now, I extract the icons folder from the zipfile into Apaches' icons directory & rename the extracted directory to "silk" so that I have the following.
/usr/share/apache2/icons/silk/


Now to change the icons in the Virtual Host container for the site in question, *.htaccess files could also be used to use different icons per-directory.
I'll start with the ones that have listings already, making the following changes to the AddIcon lines.

AddIconByEncoding (CMP,/icons/silk/page_white_zip.png) x-compress x-gzip

AddIconByType (TXT,/icons/silk/page_white_text.png) text/*
AddIconByType (IMG,/icons/silk/picture.png) image/*
AddIconByType (SND,/icons/silk/sound.png) audio/*
AddIconByType (VID,/icons/silk/film.png) video/*

AddIcon /icons/silk/application_double.png .exe
AddIcon /icons/silk/application_xp_terminal.png .bin
AddIcon /icons/silk/page_white_zip.png .Z .z .tgz .gz .zip
AddIcon /icons/silk/page_white_code.png .shtml
AddIcon /icons/silk/page_white_acrobat.png .pdf
AddIcon /icons/silk/page_white_zip.png .txt
AddIcon /icons/silk/page_white_c.png .c
AddIcon /icons/silk/page_white_cplusplus.png .cpp
AddIcon /icons/silk/page_white_dvd.png .dvi
AddIcon /icons/silk/page_white_gear.png .ini .conf .sh .shar .csh .ksh .tcl
AddIcon /icons/silk/page_white_actionscript.png .as
AddIcon /icons/silk/page_white_flash.png .fla .swf
AddIcon /icons/silk/page_white_freehand.png .fh3 .fh4 .fh5 .fh7 .ft7 .fh8 .ft8 .fh9 .ft9 .fh10 .ft10 .fhx
AddIcon /icons/silk/page_white_h.png .h
AddIcon /icons/silk/page_white_php.png .php .php3 .php4 .php5
AddIcon /icons/silk/page_white_office.png .docx .docm .dotx .dotm
AddIcon /icons/silk/page_white_powerpoint.png .pptx .pptm .potx .potm .ppam .ppsx .ppsm
AddIcon /icons/silk/page_excel.png .xlsx .xlsm .xltx .xltm .xlsb .xlam
AddIcon /icons/silk/page_white_ruby.png .rb
AddIcon /icons/silk/page_white_word.png .doc
AddIcon /icons/silk/css.png .css
AddIcon /icons/silk/html.png .html .htm
AddIcon /icons/silk/xhtml.png .xhtml
AddIcon /icons/silk/script_code.png .js
AddIcon /icons/silk/database_table.png .sql
AddIcon /icons/silk/script_gear.png .inc .pl .py
AddIcon /icons/silk/help.png .chm
AddIcon /icons/silk/music.png .wav .mp3 .ogg

AddIcon /icons/silk/folder_delete.png ..
AddIcon /icons/silk/asterisk_yellow.png README
AddIcon /icons/silk/folder_add.png ^^DIRECTORY^^
AddIcon /icons/blank.gif ^^BLANKICON^^


DefaultIcon /icons/silk/page_white.png


Followed by a restart of Apache for good measure.
sudo /etc/init.d/apache2 restart


I now have silky icons on my directory indexes, but apparently it's going to need a little tweeking to tell the difference between "text/text", "text/html", & "text/css" somewhere.