A quick and easy wrapper for HTML5 IndexedDB

March 3rd, 2013

After some trail and error I stumbled across https://github.com/jensarps/IDBWrapper. It is a quick and easy to use wrapper for the HTML5 IndexedDB wrapper. There is a two part tutorial (part 1, part 2) that will get you up and running in a few minutes.

NuSoap version 0.9.5 Attempt to modify property of non-object in nusoap.php

December 1st, 2012

I am working with NuSoap and a particular WSDL file was causing the following error for me:

PHP Warning: Attempts to modify property of non-object in lib/nusoap.php on line 4694.

To fix this problem I modified line 4694 in nusoap.php from:

$this->schemas[$ns]->imports[$ns2][$ii]['loaded'] = true;

To this:

$this->schemas[$ns][$ns2]->imports[$ns2][$ii]['loaded'] = true;

And that fixed the problem.  I found the solution in the NuSoap forums, but I though I would share the information as a post in the hopes it make it easier for someone else to find, and my line number was different than the one mentioned.  It looks like it is an old problem that has not been patched in the official release.

Bitwise Operators

April 27th, 2011

An integer stored in computer memory is comprised of bits. The number of bits in an integer varies by computer platform and possibly programming language. Integers are stored in computer memory using the binary number system. Let’s suppose a hypothetical integer has 8 bits, as shown below.

Starting on the right (the least significant bit), is 20 which equals 1. Each succeeding bit increments the exponent, 21,22,23,… .  To convert a binary number to decimal add the values associated with every bit that has the value of 1.  In the example above add 20+22+24 which evaluates to  1 + 4 + 16  which equals 21.

Two in binary is simply ‘10’. As shown below, the second bit, which represents 2, is set to one.  To store a number such as five, one must set both the first and third bits, 1 and 4 respectively which sums to the number 5.

These bits in a binary number could be used to represent a number of different types of data. One might be a set of Boolean variables.  We could have one bit indicate that spell check should be enabled, another bit could represent that the export file function should be enabled, and so on.

We could set the settings variable to 2 if we wanted to indicate that the file export feature should be enabled.  If we set the settings variable to 5 we could indicate that both the spell check and advertisements should be enabled.  If we wanted to set or clear a certain bit we could set the value of the variable as done above or we could use the bitwise OR operator to set a bit.

Above we bitwise OR 5 and 2 together, with a result of 7.  Bitwise OR works in a way similar to logical OR, in that if either bit is set then the bit in the result will be set.  If we wanted to enable file exporting, without impacting the other bits in the variable we could bitwise OR settings and 2.  The bitwise OR operator in most languages is the pipe character, not to be confused with the double pipe ‘||’ logical OR.

$settings = $settings | 2;  //Enable file exporting

To make life easier we might define constants so that we do not have to remember the number and meaning associated with each bit.

define(“SPELLCHECK”,1);
define(“EXPORT”, 2);
define(“ADS”, 4);
define(“IMPORT”,16);
$settings = $settings | EXPORT;

To determine if a bit is set we can use bitwise AND.  When we use bitwise AND the result will either be zero, meaning that the bit is not set or the result will be non-zero meaning that the bit is set.

Above, we AND 7 and 2 we get a result of two because both bits must be set in order for the corresponding bit in the result to be set.  That means that the file export feature is enabled.

In another example, we AND 7 and 8, notice how the result is 0, this is because the 4th bit, file import is not enabled.  The bitwise AND operating is the single ‘&’.

if($settings & IMPORT == 0){
     //This bit is not set.
}else{
     //The bit is set.
}

Easy HTML Form Emailer Written in PHP

February 13th, 2010

PHP Script

This easy to use script makes it easy to have an HTML form send its contents to you via email.  The PHP script is just a few lines of code.  Just customize four lines of code so that the script will send the email to the right address.  The lines of code in the easymailer.php file to modify are:

define("EMAIL","enteremail@here.com",false);
define("DESTINATION","success.htm", false);
define("SUBJECT","An email from your website.",false);
define("FROM","webmaster@yousite.com",false);

The first line configures who will receive the emailed form.  The second line determines what page the visitor will be sent to after the script has emailed you.  The third line is the determines what you will see in the subject line of the email you receive.  Line four determines who the sender will be in the email you receive.

The HTML Form

The only requires to use the PHP script is to set the form’s method attribute to post and the action attribute to ‘easymailer.php’.  See the example form below:

<form action="easymailer.php" method="post">
<input name="name" type="text" />
<input name="email" type="text" />
<input name="action" type="submit" value="Submit" />
</form>

When the visitor clicks submit the form data will be redirected to the easymailer.php script which will send the email and then redirect them to the page you configured it to.

You can download an example below just remember to change the four lines  in the easymailer.php file so it is properly customized for your needs. Then, upload easymail.php to your PHP enabled web server.

Download easy mailer script

PHP5 Singleton Design Pattern

August 12th, 2009

The singleton design pattern allows you to insure that only one instance of an object exist at one time.  It is useful tool to prevent multiple connections to a database.  The skelton looks like this:

class MyClass{
     static private $instance = false;
 
     #This prevents the user of the class from creating an
     #instance of the object
     private function __construct(){}
     #This prevents the user from making a copy of the
     #instance
     private function __clone(){}
 
     #This is how the user gets an a single instance
     #of the class
     public function getInstance(){
           #if the static instance attribute is false
           #then create the first and only instance of
           #the class
           if(!MyClass::$instance){
                MyClass::$instance = new MyClass();
           }
           return MyClass::$instance;
     }
 
     #Here you can put all of your members and attributes
     #that your class needs.

}

To get the instance of the the class you will need to invoke the getInstance method using the double-colon notation.

$myVar = MyClass::getInstance();