ways to make your website easier to maintain using php

 

This is nowhere near an all inclusive list of things you can do to make your life easier when having to maintain a website.  These are just a few tips and tricks worth knowing.

1) Never use absolute paths.  Use relative paths instead.

For example, this is a no-no:

include('/home/user/public_html/some/file');

Instead, try this:

include($_SERVER['DOCUMENT_ROOT'] . '/some/file');

Or even this:

include('../../some/file');

Then if (when!) you migrate to a new server, and your base web dir changes, you won't have to update your file paths throughout your code.

2) Use a config file.

Don't hard code your email address throughout your site.  define() it in a config file that you include() everywhere.  Same goes for mysql credentials, or any other text which may need to change at some point.  Putting everything in an external config makes a migration much easier.

Example:

define('EMAIL','my@email.com');
define('DB_USER','mydbusername');
define('DB_PASS','mydbpassword'); 

Or, use global variables:

$GLOBALS['EMAIL'] = 'my@email.com';
$GLOBALS['DB_USER'] = 'mydbusername';
$GLOBALS['DB_PASS'] = 'mydbpassword'; 

3) Place re-used web page code into external files. include() them.

Got a navigation menu that appears on every page?  Place it into a file (e.g. navigation.php) and include() it on every page.

Google Analytics?  Same deal--- include() it.

Doing so makes site maintenance MUCH easier.


October 10, 2011 at 08:22:47 AM / 1 comments / General Web Programming, Perl, PHP

Protecting mailto link email addresses from spam

 

The best way to keep a spammer from getting your email address is to not put your email address on your website.  So best case scenario --- use a contact form.  And use one that doesn't require you to put your email address in a hidden input field.  And even more important, make sure it has protection from email header injection.

If you absolutely must use mailto: addresses, here's a little trick;  write it out to the page using Javascript.  I wrote a Perl script a few years back which will do just that.  Type in your email address and it will spit out a few lines of document.write which will obfuscate your address.  You can then place this into an external .js file and reference it anywhere on the page that you want your mailto: to appear.  It isn't fancy, but it does the trick.

http://projects.ajmconsulting.net/mailto/

December 21, 2008 at 03:21:22 PM / 0 comments / Email, Javascript, Perl, Projects, Spam