Archive for December, 2011

PHP isset() and Form Input

It is easy to fall prey to simple errors when writing PHP. Usually when a script spits out an error it is because there is no semicolon or a quote mark is in the wrong spot. Even so, PHP is fairly forgiving and also explicit in warning of mistakes, but sometimes simple errors can also be deceptively hard to find.

The isset function is frequently used in if statements to test for a condition. Yet it can also fool the programmer when dealing with form input. For example input from a form looks something like this:

<input type=”text” name=”book” value=”<?php print $_SESSION[book]; >” />

However, the $_SESSION[book] variable happens to be empty, because say a book has not yet been chosen. What is more when the user submits the form, he does not fill in the “book” data.

Now if the script receiving this data looks like this:

if(isset($_SESSION[book]))
{
$x = “Eat chopped liver.”
}

Then someone is going to be eating chopped liver because even though there is nothing in the variable $_SESSION[book], the variable is still SET.

No Comments

SSI: Include Virtual in Subdomain

SHTML is not the rage that it once was. It was a huge advancement over HTML because it could be used to more easilly control the formatting of a website. I personally created quite a few websites using the technology.

Now, when I am converting some of those sites to use PHP and access a data base, I find an interesting thing happens when attempting to include a file from a directory within the  main domain. Of course, different servers will set this up differently, but usually the subdomain is relative to the main domain in the same way a directory would be. You would think, then, that you could use an include in a file in a subdomain to directly access a file higher up the directory tree in the same domain, but not within the subdomain. Well, you can’t….unless you don’t do it directly.

A work-around to this is create a .php file in the subdomain that includes the  file up the directory tree that you want included in the .shtml file and access that file using a virtual include:

<!–#include virtual=”include.php” –>

You may think that you could just use your .htaccess file to make .shtml parse like PHP. This did not work in my server, perhaps because I wanted to continue to use some of the SHTML capabilities.

You may wonder why you would even want to do this. Why not just change the name of the files to .php or even create a perma page that is handles through the .htaccess? The problem here is that you lose some of the benefit of good standing in the search engines. So, with a bit of a hack, you can keep your .shtml file extension and its capabilities in a subdomain while accessing files within the domain.

No Comments

rel=me: Claiming Authorship in Subdomains

Well, I am finally getting around to claiming authorship for all of my various writings. I have been creating web pages and content for thirteen years now, so there is quite a pile of it out there. In the course of that, I have experimented with various website structures. One thing I tried was setting up subdomains within a domain.

I have always gone back and forth on the efficacy of domains as opposed to directories. Well, I suddenly discovered one way in which directories are superior to subdomains. In order to claim authorship through Google using the rel=”me” parameter in an anchor tag, the page linking to the author page must be within the same domain (not sub-domain) as the page linking to it and that is linked to from the author’s Google profile.

Re-reading the above paragraph, I realize this sounds a little confusing. It becomes clearer when we understand the system whereby the claim takes place.

1. There must be a link to an about or author page from an article page. That anchor tag must contain rel=”author”.
2. There must be a link from the author page to the Google profile page of the author containing rel=”me”. (This must be within the same domain as the article – not a subdomain.)
3. There must be a link in the contributor to column to the author page.

To check to see if you have done this correctly you can look at the Rich Snippet Testing Tool. If you screw up the result will look something like this:

No Comments

CSS: Animated Gif Behind a Static Image

Rock Paper Scissors

You can use HTML and CSS to create a neat little effect where you have a static image in front and action going on behind. It is pretty simple to do.

First create a png file for your foreground that is transparent where you want to see the action going on behind a scene. Then create an animated gif with the action. So far so good.

Now using html with css use the following outline:

<div id=”container”>
<div id=”action”>
<img src=”action-image.gif” />
</div>
<div id=”static-foreground”>
<img src=”static.png” />
</div>
</div>
div#container
{
position: relative;
}
div#action
{
position: absolute;
top: 0;
left: 0;
}
div#static-foreground
{
position: absolute;
top: 0;
left: 0;
}

The foreground must come before the animated gif or it will be totally masked by it. If you try to put the images in the background of the container using CSS the div will simply collapse and you will not see the image at all. The images must be in the html structure.

I discovered this while working on a Rock Paper Scissors app. The image above is a simple animated gif.

 

No Comments

Levenshtein Distance

In all of my days writing PHP I have never before run accross the Levenshtein Distance. I don’t suppose that this is strictly a PHP idea. Nevertheless, I was intrigued to find out that there is a specific function in the library that will calculate the Levenshtein Distance between two strings. It looks like this:

levenshtein(string1,string2,insert,replace,delete)

The required input is the two strings, and then optionally you can give weight to certain distance perameters. So just what is this function measuring? Quite simply how many changes would have to be made between two strings in order to make them equal.

I have been pondering as to why we should ever want to quantify such a thing. But a little cogitation would show that this distance would work very well for spell-checkers in trying to determine what a person meant to write when using a particular spelling. For example, if you wrote “recieve” the Levenshtein Distance to “receive” would be in the neighborhood of two with only two replaces. A person writing “recieve” would probably not be trying to write “rectify”, which would be a distance of three. But that would be a whole lot more likely that “transmogrify”, which would be a much greater distance.

While I don’t think this distance says much about entomology, I can see how it could be a useful tool in scripts dealing in word processing.

A program dealing with the Levenshtein Distance between certain names might also prove entertaining. I will ponder that one.

No Comments

PHP mysql_query vs mysql_unbuffered_query

Using php there are two ways to get info from a database. The first is a standard query in the form of “mysql_query()”. When selecting info in this manner it is placed into a buffer on the server. To work with it, it must be fetched from the buffer using “mysql_fetch_array()” or some such function.

The other way to get info is via “mysql_unbuffered_query()”. Info downloaded in this format is worked with line by line as it comes down the pike. This eliminates the need to call the info from a buffer.

So, why is it that the standard query is more often used when an unbuffered query eliminates a step in the process. Very simply, by buffering you know before you begin how many rows are the result of the query. There may also be performance aspects involved especially with regard to the SQL server.

So, then, why use a unbuffered query? Besides eliminating a step, it can also speed up the processing server’s processing time for very large queries.

No Comments