htaccess to deny access, except for includes

It's usually a bad idea to allow php include files to be directly accessed. My general method is to either declare a constant in the index.php and check for that constant in the included file or make sure that the included file doesn't match $_SERVER['REQUEST_URI'].

Sure, either of those work. But I came across a much easier way using .htaccess and am now kicking myself for not using it earlier.

Just put the following in an .htaccess file in the folder with your include files.

CODE:
  1. # Block direct requests for files in this folder
  2. deny from all

The first line is just a comment, so you could even take that off.

Trying to directly access one of the PHP files will result in a forbidden error, but the files can still be included through another script.

Bookmark and Share

Dynamically link to CSS with JavaScript

Needed a way to add a CSS file with JavaScript, so I went out a-Googling like I normally do.  Problem is everything I found either worked in Internet Explorer and not in FireFox or the other way around.  Nothing I found worked in both.  So I hacked together a function of my own.  Should have just started with it since it took less time to write than the time I spent searching.

JavaScript:
  1. function addCSS(link) {
  2.     var styleEl = document.createElement('link');
  3.     styleEl.type = 'text/css';
  4.     styleEl.href = link;
  5.     styleEl.rel = 'stylesheet';
  6.     document.getElementsByTagName('head')[0].appendChild(styleEl);   
  7. }

Bookmark and Share

phpMailer – Could not instantiate mail function

Had a client getting this error when trying to send emails last week and have now spent several hours on Google trying to find a solution.

Biggest catch is that other scripts were able to send mail, so it seemed unlikely that it was the host. So I took the phpMailer class and uploaded it to a test folder and wrote a script that did pretty much exactly what ProofBuddy does and it worked just like it should.

After much confusion on my part, I discovered a typo in the email address that was used for sending. A character was left out - exampl.com instead of example.com. Guess 1&1 had something in place that checked if it was a valid domain before sending out.

I'm sure there's a lesson in there somewhere. Either way, I'm glad it wound up being something relatively simple even if it did take me way too long to get there.

Bookmark and Share

bit.ly and PHP

Needed a quick and easy way to create a bit.ly URL for another site of mine. Google came through for me.

David Walsh posted a short PHP function on his site that takes your bit.ly login, API key, and the URL to shorten and returns a nice, short bit.ly link.

Link: http://davidwalsh.name/bitly-php

Just put the function into your file and...

PHP:
  1. $shortURL = make_bitly_url($longURL, $bitlyLogin, $bitlyAPIKey);

And that's it. A new bit.ly URL is created and stored in $shortURL for you.

I started using this on my @DailyFont Twitter feed today to post fonts as they're added to the site and after a couple of test Tweets it seems to be working without a hitch.

Bookmark and Share

Missing httpd.conf in WHM / cPanel

Came across an error after mistakenly clicking a link in my hosting control panel.

CODE:
  1. Unable to locate httpd.conf at /usr/local/cpanel/Cpanel/ConfigFiles.pm line 27.
  2. Cpanel::ConfigFiles::find_httpconf(undef) called at /usr/local/cpanel/Cpanel/ApacheConf.pm line 206
  3. Cpanel::ApacheConf::loadhttpdconf() called at whostmgr/bin/whostmgr4 line 197
  4. main::listaccts() called at whostmgr/bin/whostmgr4 line 157

At the time, WHM was working but none of the sites on the server would load. Trying to restart httpd manually from PuTTY resulted in an error that httpd.conf couldn't be found.

Fortunately, there was an easy fix.

CODE:
  1. /scripts/rebuildhttpdconf

It's a script included with WHM that rebuilds the httpd.conf file. Ran it, waited a couple of minutes while the server churned, started Apache, and everything was back to normal.

Bookmark and Share

Quick PHP wrapper for print_r

Working on web apps I find myself using the print_r command a lot, and I mean a lot. It helps trace out what data is going where, and more often what's not going where it's supposed to.

A few months ago I realized that I type this same bit of code way too often.

PHP:
  1. echo '<pre>';
  2. print_r($variable);
  3. echo '</pre>';
  4. die();

Does a nice job of wrapping the output in<pre> tags making it easier to trace through.  But too much typing for as many times as I use it.  So I spent 2 minutes kicking out a function named pre_r that does it for me.

PHP:
  1. /**
  2. * Wrapper for print_r with pre tags
  3. * @param mixed $data   What to display
  4. * @param boolean $die  Whether the function should die() at the end
  5. */
  6. function pre_r($data, $die=false) {
  7.      echo '<pre>';
  8.      print_r($data);
  9.      echo '</pre>';
  10.      if ($die) {
  11.          die();
  12.      }
  13. }

Bookmark and Share

Mario on a sidewalk curb

Definitely cool for those of y'all that grew up playing Mario.

Bookmark and Share

Netbeans with multiple monitors

How did I not try this before today?

My normal method of needing two files open was to have the one I'm working on in Netbeans and the other in Notepad++ on separate monitors.  Turns out you can drag the tab of an open file outside of Netbeans and it becomes its own window.

We're going to have to file that one under the wish I had thought of that a long time ago category.

Bookmark and Share

Java vs. .Net – Movie Trailer

Bookmark and Share

Alka-Seltzer in space

What happens when you put an Alka-Seltzer in water with no gravity?

Bookmark and Share