Posts Tagged ‘PHP’

Custom Logging in PHP

Saturday, April 24th, 2010

I've been writing more and more cron jobs lately and, although most of the time they run smoothly, there are moments where everything seems to break at once. It can be tough tracking down these problems since you're not seeing any output while they're running. So, I've started logging all sorts of information. I'm not talking about the system logs here, but instead about custom logs. I'm sure some of it is overkill, but I've been going with the store it and if you don't need it later, you can delete it approach. Here's a quick, easy example:

$logfile = '/export/logs/test-log_'.date('Ymd').'.txt' ;
$logtext = "\n".date('n/j/y g:i a')."\n" ;
if (testFunction()) { $logtext .= "success.\n" ; } else { $logtext .= "FAIL!\n" ; }
$fp = fopen($logfile,"a");
fwrite($fp, $logtext);
fclose($fp);

Quick Security Alert Regarding Bluefish Editor

Sunday, July 12th, 2009

This will be a quick post because I've been slammed with work lately, but I just wanted to mention something alarming that I discovered the other day. As I started using Bluefish Editor, I started noticing all sorts of extra files on my server with a ~ at the end of the filename (index.php~ as an example). So, I tried typing one of those into my browser and, to my alarm, found that my server was displaying the raw PHP code! Of course, this is a huge security risk. So, here's how I fixed it:

  1. I disabled the auto-backup feature in the editor. In Bluefish, this was just an option in the preferences menu. Note: I have no idea why they would have this as a default setting. It makes no sense to me. Anyway...
  2. I deleted all of those extra files I could find.
  3. I modified the .htaccess file to prevent any of them that I missed in the cleanup from being displayed. By adding this code to your highest level .htaccess, you'll now get a forbidden 403 when attempting to access:
    <Files ~ "~$">
      Order allow,deny
      Deny from all
    </Files>

Hope that helps someone out there.

Connecting to an IMAP Mailbox with PHP

Sunday, December 7th, 2008

There are a number of cool things that you can do after opening up an IMAP connection in PHP (monitoring bounce-back rates, processing incoming attachments, etc.) but before you do, you have get connected. Normally, this is a pretty straight-forward process, but sometimes different mail server settings can throw you a curveball. Here's a couple quick tips to help you get started:

  1. Open up a phpinfo() page and make sure that you have the IMAP extensions installed. If you don't, here are the instructions on how to get them.
  2. Copy our PHP IMAP Connection Tester onto your server and configure it with your account information.
  3. That's it. I told you that was easy!

A note: I ran into the following error today: "Certificate failure for mail.example.com: Server name does not match certificate." It took me a while to find the solution but finally came up with this -- add /novalidate-cert after the port number. So, for example, if you're using our tester, set $mail_port = 143/novalidate-cert; Hope that helps!

If you have any other tips, help the world out and share them below!

Image Upload Form, Cron Job Tester, & MySQL Table Copier

Sunday, November 30th, 2008

Over the weekend, I posted three new code snippets:

The Image Upload Form has everything you need to create a simple HTML form allowing users to upload their own JPEG images. After it verifies that the image is valid, it saves it and generates a thumbnail using the PHP GD library of image functions. The code is clean, so it's pretty easy to manipulate it to suit your needs (other file formats, different resizing, etc.)

The Cron Job Tester is the result of an epic battle I had with the server hosting one of my client's sites. I couldn't figure out why these cron jobs weren't running and so constructed this little tester to simply log an entry in a text file every minute. From there, I was eventually able to find out that the host's "Easy Cron" function was overwriting my crontab file. Arrrgh...

Finally, I posted the PHP code to Copy All the Rows in a MySQL Table to another, identical MySQL table. You can, of course, do this easily in phpMyAdmin, but I needed to have the function automated for this particular project. Hopefully you can get some use out of it too.

Slow Transitions

Thursday, November 6th, 2008

I've been trying to juggle a million different projects lately and haven't had much of a chance to write up here. I wish I could just pause time for a month or two and get caught up on this gigantic checklist of tasks that I want to get done on my sites.

One of the bigger tasks on the list is switching all the Lantenengo Industries stuff over to Ink Plant (I'm going to be officially changing my business name in the near future for branding reasons). Tonight, I got a decent-sized chunk of that switch taken care of: I closed down my old blog (redirecting it to here, of course) and pulled out the code snippets that I had published there, creating the new Code section of this site.

I also published a new code snippet that generates the PHP to display a MySQL table automatically. I've probably written this out by hand a couple hundred times, so it'll be nice to finally have this little shortcut. Hopefully some of you will benefit from it too. Enjoy!


Copyright © 2010, Ink Plant. All rights reserved.