PHP mail() function and From Header
Posted by mcgelligot in Php Coding on January 5, 2012
Yes, I knew you wanted to read about another development on the PHP mail function front. So here it is: you can use the “from” header on some servers and not on others. Some mailservers will reject an email with a “from” header (with out of state plates) out of hand, on the possibility it is spam. Others will allow you to send a message with it.
Why do I need to include the “From” header in a mail message? As previously explained, I am developing a universal contact module that can be included on any website. Not all that complicated…you would think. But as my dear old granddad used to say between spits of tobacco, “It’s always sumpin’.”
I wanted to make it easy to respond to messages sent by the contact form, so I included the “From” and “reply to” headers. It worked fine on one server, and I thought, great, now I will just start installing this module on all my sites. The first one liked the “From” module, so did the second, but when I hit a different server, it did not like it.
As it turns out, you don’t really need the ”From” header in the email to allow a one click response form to show up on your email program. So, I have simply removed it. The “reply to” still works fine.
PHP Mail Function and the New Line Character
Posted by mcgelligot in Php Coding on January 2, 2012
Lately I have come to the notion that I can modularize my PHP and inject it like a serum into some of my older websites to update and upgrade their performance and useability. In pursuit of this goal, I rewrote a mail utility to make it universal, a kind of plugin. Simply FTP some files to the server, install a link and wham.
I put the utility on Drywall How To Manual. It works great…now. There was only one tiny problem. When I rewrote the utility, I changed the $message variable to look something like this:
$message = “Name: $cname \n Email: $cemail \n Ref Page: $ref_url \n Message: $cmessage \n”;
It feeds the following:
$mail_success = mail($email, $subject, $message);
Do you see anything wrong with it? Neither did I. It took me an hour of testing and breaking down those lines of code to find out what the problem was. I will give you a hint if you haven’t already got an inkling from the title of this post. The problem is in the new line character.
Yes, you can have a new line character in the variable of the mail function in PHP. Only thing is you cannot have it at the end JUST BEFORE the quote mark.
The funny thing is that this does not throw an error. In fact, the email is simply not sent, but the function returns “true”, indicating the message was sent.
To test to make sure that the “backslant n” was not a general no no in PHP I tried a little test which looked like this:
//test in variable:
$test = “test \n”;
$test2 = “test2 \n”;
print “test = $test”;
print “test2 = $test2″;
print “test3″;
print “\n”;//test in function:
$length = strlen($test);
print “length = $length”;
The results make me believe this is a little bug in the mail() function:
test = test
test2 = test2
test3
length = 6
Interesting development. No matter how long you are in the game, you always are learning something.
PHP isset() and Form Input
Posted by mcgelligot in Php Coding on December 27, 2011
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.
SSI: Include Virtual in Subdomain
Posted by mcgelligot in Domains, Php Coding on December 26, 2011
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.
rel=me: Claiming Authorship in Subdomains
Posted by mcgelligot in Miscellaneous on December 17, 2011
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:
CSS: Animated Gif Behind a Static Image
Posted by mcgelligot in css on December 9, 2011
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.
Levenshtein Distance
Posted by mcgelligot in Php Coding on December 6, 2011
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.

