now with added Masterweb and IB Geek Girl

spider web Ecanus•net :: Code Snippets

My favourite code snippets

These are just some of the code snippets I couldn't live without. Okay, so maybe, I could live without them – they're not like air or food or anything, but they're the code snippets I use most often, and the ones that make life easier for me. I've tried to group them into logical groups – HTML (and XHTML), CSS, and PHP. I hope you find them useful.

These are just a few of my favourite ones. I'll be adding more as soon as I get a chance. Click the links below to see the snippets.


Compressing your web pages using PHP

mod_gzip is an Apache web server module that compresses the HTML as it sends it out. It's compatible with all browsers (old browsers that don't support gzip content will get uncompressed HTML). Using gzip can save you HUGE amounts of bandwidth, and increase the overall performance of your webserver, and speeds up loading for visitors on slow dial–up modems, BUT it can be difficult to set up and not all hosts support mod_gzip.

Stick this handy little snippet though at the top of your PHP page, and and it automatically gzips the page for browsers that support it. It works with all browsers (though v old browsers that don't support gzip just get it uncompressed), and as most webservers support PHP, it can be used on most pages, and all you have to do is change your files from mypage.html to mypage.php. If you're already using php anyway, it's just an extra line in the top of the page.

PHP: <?php ob_start("ob_gzhandler");?>

Page modified date and the copyright year
Page modified date

So I can remember myself when a page has last been updated, and show how current a page is, I like to show the date a page was modified on my pages. It's a pain trying to remember to do it manually though, so I use a bit of PHP intermingled with the HTML to do the job for me.

PHP: <? $last_modified = filemtime("index.php"); print("Last Modified "); print(date("j/m/y", $last_modified)); ?>

All that's needed to display the correct date the file was modified is the name of the file – just change that for each page, and the PHP does the rest.

Result: Last Modified 21/11/08

Copyright year

This is the PHP snippet I use combined with HTML on my pages for the copyright statement at the foot of all of my pages.

&copy;, 2005 – <?php echo date('Y'); ?>. All Rights Reserved

Result: ©, 2005 – 2017. All Rights Reserved