Archive for the ‘Uncategorized’ Category

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);

Custom Sorting MySQL Data

Wednesday, December 16th, 2009

Sorting data in MySQL can be really easy if you're just trying to sort alphabetically or numerically. All you have to do is slap in the ORDER BY clause and list out the columns in order of importance. For example:

SELECT * FROM `animals` ORDER BY `color` DESC,`name`

name animal_type color
Clifford Dog light blue
Jingles Cat light blue
Lucy Cat light blue
Sebastian Hamster light blue
Cincy Tiger dark blue
Odysseus Hamster dark blue
Steve Bear dark blue
Vito Dog dark blue
Billy Bear blue
Rex Lion blue
Rover Dog blue
Simba Lion blue

But, what if you want to do something more complicated? Like sorting the animals by the lightness of their color? That's where ORDER BY FIELD can come in handy. You can use it to specify the exact order the values should be sorted by. Check this out:

SELECT * FROM `animals` ORDER BY FIELD(`color`,'light blue','blue','dark blue'),`name`

name animal_type color
Clifford Dog light blue
Jingles Cat light blue
Lucy Cat light blue
Sebastian Hamster light blue
Billy Bear blue
Rex Lion blue
Rover Dog blue
Simba Lion blue
Cincy Tiger dark blue
Odysseus Hamster dark blue
Steve Bear dark blue
Vito Dog dark blue

Still, that doesn't quite cover all the bases. What if you want to show all the light blue and blue animals first (sorted by name) followed by all the dark blue animals? You could do two separate queries and then UNION them together, but that creates it's own set of issues... Or you can try this cool trick involving CASE that I figured out the other day:

SELECT *,CASE WHEN `color` IN ('light blue','blue') THEN '1' WHEN `color` ='dark blue' THEN '2' END AS `customsort` FROM `animals` ORDER BY `customsort`,`name`

name animal_type color
Billy Bear blue
Clifford Dog light blue
Jingles Cat light blue
Lucy Cat light blue
Rex Lion blue
Rover Dog blue
Sebastian Hamster light blue
Simba Lion blue
Cincy Tiger dark blue
Odysseus Hamster dark blue
Steve Bear dark blue
Vito Dog dark blue

Using the CASE statement, you create a temporary column that you can then sort by. Of course, this example is really simple but you can get much more complex with the queries. Hope this helps save you some time. And, if you have any similar tricks, please paste them in the comments below.

Explode Each Character in a String to an Array

Sunday, November 8th, 2009

I added a couple new code snippets to the Code section today, including a function that act's like PHP's explode(), but separates every character instead of requiring a delimeter.  You can achieve the same thing using PHP's split() and RegEx, but this is simple and works...

PHP Explode Each Character Function

How to Run a Shell Script as Superuser

Sunday, October 4th, 2009

If you've ever tried to run a shell script as superuser in Linux by using the syntax sudo ./example-shell-script, you'll know that doesn't work. Instead, this is how you need to do it:

  1. Create your script
    vi example-shell-script
  2. Change the permissions
    chmod 755 example-shell-script
  3. Switch into superuser mode
    sudo su -
  4. Run the script
    ./example-shell-script
  5. Log out of superuser mode
    exit

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.


Copyright © 2010, Ink Plant. All rights reserved.