Archive for the ‘Web technology’ Category

Some floating issues

Saturday, August 18th, 2007

I have written an article that shed light on some floating issues. Take a look if it interests you.

Error reporting in PHP

Wednesday, March 21st, 2007

Some way down the road the default php.ini started to get shipped with settings that stopped the error printing. The default location of this file is /usr/local/lib/php.ini, and the most common settings that controls the error reporting behaviour is listed below:

error_reporting = E_ALL | E_ERROR | ... | E_USER_NOTICE
display_errors = On | Off
log_errors = On | Off

First of all error_reporting decides what type of errors or messages that will be taken care of in the first place. See php.ini for details. Then you have the options to:

  1. Print the error as part of the HTML by setting display_errors to On
  2. Log the error to Apaches error_log by setting log_errors to On
  3. Or both print and log the errors and messages

There is a reason why they stopped to display errors as a default thing. This is because you are risking to leak sensitive information about your server configuration. A way to achive instant information about errors, and keep the rest of your web site safe, is to alter the settings for just the scripts you are working with at run time. It can be done like this:

ini_set('display_errors','On');
error_reporting(E_ALL);

Suggested readings:

The UTF–8 character encoding, XML and PHP5

Tuesday, July 11th, 2006

In this article you will get to know a litte about how character sets, XML and PHP5 work together. We will also look a bit deeper into the most common character sets and encodings.

UTF-8

First we need to talk a little so that you will be able to grasp the two terms:

  • character set
  • character encoding

Data is stored on disk as bits. To get a character out of a bit sequence the computer:

  1. first reads a number of bits,
  2. decodes the bits to get a code point and
  3. maps the code point into a character

For instance:

  1. the fetched bit sequence 1100001
  2. could be decoded into code point 97
  3. which maps to the character a using the Unicode character set.

It’s also common to express code points in hex so for instance the arabic character ف has a 641 hex and a 1601 decimal code point.

Now it should be obvious that character encodings maps bits to code points while character sets maps code points to characters. Unicode has a lot of different encodings, but ISO–8859–1 (Latin–1) has only one. UTF–8 is one of the Unicode encodings. While Unicode and Latin–1 have an equal character map below code point 255 the UTF–8 encoding is different from Latin–1 from code point 128 and up.

XML and PHP5

Use the following XML declaration:

<?xml version="1.0" encoding="ISO-8859-1"?>

to tell your software that your XML file has a Latin–1 encoding. The default is UTF–8 so if you omit the encoding part, you are really saying that your file contains UTF–8 characters.

Opera, Firefox and Internet Explorer knows how to display Unicode characters represented in the UTF-8 encoding. The problem is that web servers like Apache has a default Latin–1 content header. Browsers then think they are reading Latin–1 and displays characters, which have different encodings like å, wrong. The following PHP code changes the header so that browsers know that it’s UTF–8 they are receiving:

header('Content-Type: text/html; charset=utf-8');

Suggested readings:

DOM, the built in template system

Tuesday, June 6th, 2006

It’s allways good to separate code and markup, and with DOM you can do such a thing. We will look at these properties of DOM in this article:

  1. Load and parse a base XHTML file
  2. Fetch elements using XPath
  3. Append some XHTML elements
  4. Print the modified document

Let’s start with this file called base.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>DOM example</title>
  </head>
  <body></body>
</html>

Tip: To get Iternet Explorer 6 into “almost strict mode” the XML declaration needs to be removed. If not IE will turn into “quirks mode”.

Next step is to load the XML and add a style sheet and some contents. After the document has been created we fetch the head and body tag. Finally we modify the document and print it.

// Creating and parsing the base.xml document...
$doc = DOMDocument::load('base.xml');
 
// Instantiating XPath object...
$xp = new DOMXPath($doc);
 
// Fetching elements...
$head = $xp->query('/html/head')->item(0);
$body = $xp->query('/html/body')->item(0);
 
// Appending contents...
$link = $head->appendChild($doc->createElement('link'));
$link->setAttribute('href','example.css');
$link->setAttribute('type','text/css');
$link->setAttribute('rel','stylesheet');
$body->appendChild($doc->createElement('h1','Contents'));
 
// Printing the modified document...
print($doc->saveXML());

Suggested readings