Archive for category Php Coding
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.
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.
PHP mysql_query vs mysql_unbuffered_query
Posted by mcgelligot in Php Coding on December 1, 2011
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.
Php Two Ways to Write to a File
Posted by mcgelligot in Php Coding on November 9, 2011
In Php you can write to a file the old fashioned way using the standard file-handling functions, fopen(), fwrite(), and fclose(). The first function opens the file and specifies the manner in which it is written to. It can specify that the function can only be read, completely rewriting the entire page, appending to the end of the page and even only writing if it is a new page. The fwrite() function simply does what the fopen() function allows it to do, and fclose() frees up the resource. Here is a basic example:
$file_to_change = “test.htm”;
$fh = fopen($file_to_change, ‘a’); //$fh stands for file handle. “a” tells us the mode = append.
if($fh === 0)
{
exit(“Could not open $file_to_change”);// Just in case. This the fopen() function will return 0 if there is a problem.
}
$info_to_write = “This is what I really want to say”;
fwrite($fh, $info_to_write);
fclose($fh);
Yes, it is three steps. Nevertheless it has some nice functionality. But there is another way. Instead of three functions, this same thing could be done with only one function:
$file_to_update=”test.htm”;
$info_to_write = “I wanted to say this too.”
file_put_contents($file_to_update, $info_to_write, FILE_APPEND | LOCK_EX);
In the above code, the FILE_APPEND serves the obvious need. LOCK_EX puts a lock on the file not allowing anyother program to write to it while this script has it open.
So why would you want to choose the first script over the second script? Well, the first script allows you to keep the script open for an extended period. Also in a very long loop the second file_put_contents is going to open and close the file unecessarily thousands perhaps millions of times.