Category Archives: Programming

lessons of the month

This month has flown right by, but not without some learning experiences. Here are some things I learned:

  • Never give Schr?die flea medicine while wearing a decent shirt; she still has back claws.
  • There is a limit as to how much milk one can ingest in the span of three minutes.
  • High school kids that play in pit bands are really cool.
  • The Comfort Inn is not that comfortable.
  • It can take 50 minutes to drive 5 miles when there is night construction on I-95.
  • Always make a backup of a database table before you do anything to it.
  • Agatha Christie is still a good read.
  • Some vampire cable shows should never have been made.

build readable strings recursively out of a form array in PHP

Here’s a great way to turn an array into readable strings in PHP. For example, I want an error notice to be sent to me automatically when a database action in a function fails. The problem is, I cannot possibly know what is going to be in every form that is used in the function that generates the database query. While I *can* easily get the keys and values using

foreach ($in_array as $key => $value)

and building a string, what if the value itself is an array? The value for any array within the array would be “Array”. Not very useful!

Fortunately, PHP functions can be recursive. Here’s a function I wrote to handle that:

function getArrayAsString($in_array)
{

$string = '<br />';
foreach ($in_array as $key => $value)
{

if (is_array($value)) $string = $string.'<br /><strong>'.$key.'</strong>: '.getArrayAsString($value).'<br />';
else $string = $string.'<strong>'.$key.'</strong>: '.$value.'<br />';

}
return $string;

}

Each value is checked to see if it is an array. If it is, it (the array value) is sent to this function, and it processes until all of its values have been evaluated, sending any of those array values into this function as well.

I use this function by concatenating the result to an error message by calling the function with the $_POST variable as the array parameter. This way, I have my custom-built message specific to the failed function, and I get all the information that was posted so I can evaluate the error without having to generate the array values one at a time, or worrying if any values are themselves arrays. Click here to fill out a form and see how this recursive function works.

Personally, I like my “keys” to be bold and the values to be normal, but you can change that to suit your tastes.

troubleshooting obscure php email issues

One of my current web development/programming projects is being built on a server that I have no access to. It’s quite challenging, to say the least! One of my issues, and the hardest to resolve, was email.

I use the ordinary mail() PHP function. No classes, no cgi, just the function with HTML headers. But it seemed that certain emails I sent from the server were going nowhere, and no failure was being reported either. For example, I could receive mail at my “theconnorswebsite.com” domain, but not my “whitewavedesigns.com” domain.

Well, first I learned that there was no formal MX record, and my server followed that protocol. One was set up on this particular server, but still, I could not receive email at my wwd domain.

I then tried to specify the smtp server, port and from address:

ini_set("smtp","mail.website.com");
ini_set("smtp_port","25");
ini_set("sendmail_from","info@website.com");

That didn’t work either. I also made sure that the server would allow “from” emails to be from any domain by substituting various emails at different domains as the “from”. I even double-checked my HTML headers:

MIME-Version: 1.0
Content-type: text/html; charset=iso-8859-1
From: Website <info@website.com>

Then, one of the programmers who works on this mystery server told me that their MTA (mail transfer agent) was Postfix, and a quirky thing about Postfix on a Linux box was that it would not tolerate the “r” tag when adding additional headers such as “CC” or BCC”. For example, this is how I was writing my headers:

$headers.= "rnCC:".$postformAR['Email'];
$headers.= "rnBCC:".$postformAR['Webmaster'];

(I put the carriage-return/line feed in front of the cc and bcc so it can be optional.) I had heard that Windows servers don’t especially like the “r” carriage-return tag, but a Linux server? Well, okay, I removed it.

Then, as if by miracle, some of the emails were coming through! But still not all of them… I finally sat down today and line-by-line tried to figure out why some of my code was working and some wasn’t. Eventually, I got down to the actual mail() function.

Now, according to php.net, this is the mail() function description:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Note the last “additional_parameters”. Sometimes, I use this last parameter “-finfo@website.com” for failed messages, bouncebacks, etc. I don’t tend to use it when someone submits an email to my site, but I do use it when sending out bulk emails like newsletters. As I found out today, for whatever reason, that last parameter was actually required!

So, my fellow programmers, should you ever have email that is sent from your serve simply vanish into cyber-space, I hope this gives you a few more tricks up your sleeve to resolve the issue.