Wednesday, October 20, 2010

PHP to Remote Database Error: "Can't connect to MySQL server on..."

On a new server migration project I am working on I have separate web and database servers. So, I use a private network to connect them. It took me a day to get working, so I am putting up a post to help others in similar situations.

First, just to clarify, I knew it was a web server problem (and not a network or db permissions issue), because I could connect to the MySQL server just fine from the MySQL command line on the web server. But, when I pinged the PHP web page from my browser I'd get this error:

Can't connect to MySQL server on '10.1.1.1' (13)

The problem was SELinux, which was installed and crackin' down on Apache rights.

One simple fix:
setsebool -P httpd_can_network_connect=1

More information can be found in this nice article:
http://www.ehow.com/how_2090983_connect-remote-database-under-selinux.html

SELinux would allow me to connect from the command line just fine. I could even execute the PHP script from the command line and connect fine. But SELinux did not like it when Apache tried to connect to a computer on the network. Hence the policy update, as stated above.

If you have other issues or it appears that your db server is having issues, here are some other MySQL debug tips as well.

Monday, October 18, 2010

Add Text to an Existing PDF Using PHP

I am finishing up on a project right now that fills out a PDF application for a merchant account. The software helps the user through the application process by asking more targeted questions and filling in fields that are known. In order to do this I had found two PHP libraries that work quite well.

The first is FPDF. It is a free PHP library that allows you to create PDFs. It is not a module or binary code, just a straight up PHP script library. Color me impressed. You can download it at http://www.fpdf.org/

The second bit of code you need is called FPDI. This will import a PDF as a template and allow you to overlay text. It worked quite well for my project as I was taking an existing PDF and filling in fields on top of it. Just like FPDF, it is super easy to install because all it is is a collection of PHP script files. You can download it at http://www.setasign.de/products/pdf-php-solutions/fpdi/

Here is a quick demo by FPDI on how to use these two modules together.

To make it all work I created a flat, PDF version of the template PDF. I removed all of the form fields using Adobe Acrobat. The first run I got an error: "probably uses a compression technique which is not supported". This was easily fixed by saving the PDF as PDF/A using Adobe Acrobat. (Update below.)

If you have any questions, please let me know. I'd be glad to share more of what I learned.

Update (10/22/10): I've learned that saving the PDf as a PDF/A doesn't always solve the error I explained above. What has worked, however, is running Adobe Pre-Flight on the document. If you go to "Advanced" > "Print Production" > "Preflight" and then click on "Analyze and fix", Adobe will work some magic on the document and make it work with FPDI.

Update (1/27/11): FPDI, the free version of the PDF parser, requires the PDF to be version 1.4 or below. To save a PDF in this format, open the document in Adobe Acrobat, click "Save As" and then select the type "PDF, Optimized" then click the "Settings" button and select "Adobe Acrobat 5.0". This will save the PDF in version 1.4, which was last used in Adobe Acrobat 5.0!

Tuesday, October 5, 2010

Removing WordPress LINK: NEXT and PREV

In WordPress, in the HTML HEAD, there are two <link> lines that are present. One is rel="next" and the other is rel="prev". Now, if you have some pages that you don't want just anyone to find (and the private and password-protect features are not appropriate), this little feautre may reveal them to the public.

To remove the LINK data from the HEAD:
  1. First, pull up your template editor by going to Appearance >> Editor.
  2. On the right side is a list of files. Click on "Theme Functions" or "functions.php" to edit it.
  3. At the top, just after "<?php" add these lines of code:
    1. remove_action('wp_head', 'parent_post_rel_link', 10, 0);
    2. remove_action('wp_head', 'start_post_rel_link', 10, 0);
    3. remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
  4. Click "update file" at the bottom

All done! The link, rel="next" and rel="prev", as well as "start" and "parent", should be all gone from your template.

Friday, October 1, 2010

iPod Touch Battery Life Issue

I love my iPod touch. But it had a problem. After upgrading to the latest and greatest software (iOS 3), the battery life was awful, to say the least. I charged it, set it on the kitchen table in the evening and by morning it was completely dead. Something was very wrong.

The first thing I tried was to disable all push notifications and alerts. And, after charging it up, I left it plugged in for a few extra hours (just in case). Unfortunately, I saw no change in performance. The iPod would die, again, within 24 hours, even if I had not used it. This was getting ridiculous.

I found a blog post that suggested performing a restore. So, I gave this a try. I plugged in the iPod to my laptop, and clicked the restore button in iTunes. After a few warnings and some waiting, the iPod was back to its squeaky-clean state. And I can say that now, after 3 days, that the restore worked and the battery life is back to normal. I haven't charged it in days and it is still going strong.

Bottom line, if your iPod touch's battery life was cut dramatically short after upgrading to iOS 3, try a full restore. Worked for me.

Leave a comment if you've had similar experiences. I'd be interested to hear if it worked for others as well.