Quantcast 2008 September | Victor's Programming Aid

An array of C#, PHP, and HTML programming articles, tutorials, and resources

Archive for September, 2008

The Firefox browser (as of version 3.0.2 and likely earlier versions), there is a search bar at the top right. Custom search engine plugins are available to allow easier searching. For this example, I will demonstrate how to create search engine for the search engine Scour. We will begin with a template which should be copy and pasted into an xml file (in our case, it will be scour.xml).

<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
   xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName>engineName</ShortName>
<Description>engineDescription</Description>
<InputEncoding>inputEncoding</InputEncoding>
<Image width="16" height="16" type="image/x-icon">engineImage</Image>
<Url type="text/html" method="method" template="searchUR" />
</OpenSearchDescription>

Now that we have our template, we need to make custom changes for our search engine. We will go through each of the colored changes:

engineName
This simply shows the search engine name that appears on the browser in gray before a search term is entered. In our example, the search engine name is Scour.

engineDescription
This one is a search engine description. Scour’s search engine description is, “Search Socially”.

inputEncoding
This is the encoding for the search engine. In most cases, this should be “UTF-8″.

engineImage
This is the image that will represent the search engine. Note that generating this portion will most likely require an online tool. The image should be a 16×16 or 32×32 image converted to type image/x-icon. The template above is cofigured for a 16×16 images. Be sure to adjust the width and height to 32×32 if you decide to use a 32×32 sized icon. An example of a suitable image is the search engine’s default shortcut icon found in the the meta information of the website. To generate the text representation of the image, go to The data: URI kitchen. Below is a quick tutorial on how to use use URI kitchen:

  1. Insure that the “base64″ checkbox is checked.
  2. Either insert an online http link to the image or browse to a local copy on your computer in the correct input field.
  3. Click the Generate button. Note that the result required for engineImage field will be outputted to your browser’s Location Bar (URL bar).
  4. Copy and paste the result into your xml document.

searchURL
This is the most important field and will determine whether your search plugin correctly performs a search. Because each search engine is different, we will go through a step by step process of how to obtain this information for the Scour search engine.

  1. Go to their main website at Scour.com.
  2. Input the following search term without the quotes: “findMe”. Click Search.
  3. On the search results page, we will examine the URL. As of the writing of this document, the resulting URL was: http://www.scour.com/search/web/findMe.
  4. We then want to replace the string “findMe” with “{searchTerms}”. The result of this step (and the searchURL data) should be: http://www.scour.com/search/web/{searchTerms}.

We have now completed the xml file for our search engine. We will now go through the process of testing to ensure that our Firefox Search Plugin works:

  1. Make sure your XML file is free of errors by opening the file in Firefox (should be as easy as double clicking on the xml file in windows explorer or your windows desktop). If you have problems at this step, the most common errors is a tag might be overwritten.
  2. Assuming Firefox 3.0.2 was installed into it’s default directory, the local directory to place this xml file is: C:\Program Files\Mozilla Firefox\searchplugins.
  3. Restart the Firefox browser.
  4. On the fresh Firefox browser load, check the search engine dropdown to see if your newly created search engine is there. If the search engine image is blank, verify that the information you used for engineImage is correct. Recreate another copy if you have to!

This just about sums up how to create a Firefox Search Engine plugin. Let me know how this works!

Verify Email Address in PHP

by Victor | September 24, 2008 in PHP | 4 Comments

Nearly every interactive web application requires at some point a user to register. So generally, there exists a registration page where site visitors input basic information like username, password, and email address. Aside from the username and password, what is the easiest way for a PHP web developer to verify an email address is at the very least properly formatted (whether this is an active account is a whole separate matter)?

The approach that come to mind first is to do a simple email string check:

  1. Make sure the email string contains at most one ‘at’ symbol (@)
  2. Make sure the email string contains at least one ‘period’ symbol (.) after the ‘at’ symbol (@)
  3. Make sure the email string contains at least one character before the at symbol (@)
  4. Make sure the email string contains at least one character between the ‘at’ symbol (@) and the ‘period’ symbol (.)
  5. Make sure the email string contains at least one character after the ‘period’ symbol (.)

For starters, this is no easy task. Note that the five suggested steps above is not even complete. Many might suggest a complex regex verification process. I have personally attempted to write a regex and even PHP based checker. During this process, I found a great PHP Email Address Validation function available under BSD License which, among others things, allows commercial use. [More]

So you have successfully created a PHP class and want access to the classes variables and functions. As a reminder to programming beginners, you can only access public variables and public functions. That is a function or variable that was declared with the public keyword or no keyword (as PHP functions and variables are by default public). Below is a quick refresher example where functionNumberOne and functionNumberTwo are public and functionNumberThree is private.

class Common {
   public $secondVariable = "2";
   private $thirdVariable = "3";

   function functionNumberOne() {
      echo "This is my first function";
   }

   public function functionNumberTwo() {
      echo "This is my second function";
   }

   private function functionNumberThree() {
      echo "This is my third function";
   }
}

If the class is declared in another a separate file, remember to first include the file before your first attempt to access the class. Generally, the best place to include files is at the very top of the file.

Assuming our Common class function above, our first step to accessing our PHP variables and functions is to create an object. In programming terms, this is known as initializing an object instance. To create the class instance:

$myInstance = new Common();

How was the previous code snippet generated? You can choose any name to replace the $myInstance variable (so long as it is not a keyword). Next, new is a PHP keyword that in this situation signifies the creation of a new object. Finally, Common() is constructed by using the class name (in this case Common) followed by an open parenthesis and close parenthesis ‘()‘. If you have a specified a constructor, make sure you include the correct number of parameters.

Finally, to access the public variables we use the $myInstance object followed by ‘->’ and the public variable or function name. When accessing the variable name, you do not need to start with $. Again, we have a code example below.

echo $myInstance->secondVariable;
$myInstance->functionNumberOne();
$myInstance->functionNumberTwo();

And there we have it. You now have access to public PHP functions and PHP variables. Note that the use of classes is a new feature to PHP 5.