Search
Archives
- January 2012 (2)
- June 2011 (2)
- May 2011 (1)
- November 2010 (1)
- October 2010 (1)
- May 2010 (2)
- April 2010 (1)
- December 2009 (1)
- November 2009 (1)
- August 2009 (1)
- June 2009 (1)
- April 2009 (1)
- January 2009 (5)
- December 2008 (1)
- November 2008 (1)
- August 2008 (3)
- July 2008 (3)
- June 2008 (1)
- February 2008 (2)
- January 2008 (1)
- November 2007 (2)
- September 2007 (1)
- July 2007 (1)
- May 2007 (2)
Categories
Meta
Category Archive: Tips
Subcategories: No categories
Easy php output compression
Below is what I regard as a reliable method of doing gzip output compression.
One thing that normally breaks gzip compression is something printing/echoing before the ob_start (usually the result of an error, debug output, errant space somewhere etc…), this is where the ob_get_length and ob_flush come in.
The problem is that headers_sent is not a reliable way to tell if any thing has been output because it may still be in the output buffer, so if we flush it before hand problem solved.
<?php
function doPage()
{
echo 'Hello World';
}
if (ob_get_length() > 0)
@ob_flush();
if (!headers_sent() && @ob_start("ob_gzhandler"))
{
doPage();
ob_end_flush();
//Probably a good idea to exit here as more output would break it
exit;
}
else
{
doPage();
}
lighttpd, php and clean urls
Here’s a situation, You have php running on a lighttpd server via fastcgi and you want clean urls (ie. /post/123/postname).
The typical way to-do this with lighttpd is to set the error-handler to your script.
$HTTP["host"] == "my.com" {
server.document-root = "/var/www/my.com"
server.error-handler-404 = "/index.php"
}
We use an error-handler here rather than rewriting the url as with apache because there’s no way to detect if the url is actually a file (ie. your css/js files).
Problem here is that this will break query strings for anything other than the root url (query strings are what’s after the ?). ie. when accessing /post/123/postname/?confirm=true the confirm will not appear in the $_GET or $_REQUEST arrays while /?confirm=true will.
So how do we fix this? The method I’ve used is to manually parse the query string and put its values into the get and request arrays, as below.
function fix_query_string()
{
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$query = parse_url($url,PHP_URL_QUERY);
if ($query == False || empty($query))
return;
$_SERVER['QUERY_STRING'] = $query;
parse_str($query,$query);
foreach($query as $key=>$value)
{
$_GET[$key] = $_REQUEST[$key] = $value;
}
}
if (stripos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false)
fix_query_string();
Hope this helps!
Maximizing a print preview dialog in c#
Here’s a way to get a print preview dialog (or perhaps any resizeable dialog) maximised.
dlg.Show(); //Make full screen //cant use WindowState as its not a form //dlg.DesktopBounds = Screen.FromControl(dlg).WorkingArea; ((Form)dlg).WindowState = FormWindowState.Maximized; dlg.BringToFront();
Although some times it disappears behind whatever form had focus last.
Update
Looks like the dialog can be cast to a form allowing WindowState to be set.
PrintPreviewDialog dlg = new PrintPreviewDialog(); dlg.Document = doc; ((Form)dlg).WindowState = FormWindowState.Maximized; dlg.ShowDialog();
Detecting drive insertion and removal in C#
Here is some C# code to detect when a logical volume (e.g. USB Memory Stick) is inserted or removed via the WM_DEVICECHANGE message with WndProc.
However this doesn’t tell you what has been inserted/removed, you will have to poll the drives manually to find out.
If you need that you will have to register to receive events from windows which involves P/Invoking RegisterDeviceNotification (See Drive Detector at Code Project).
[...]
using System.Runtime.InteropServices;
using System.Security.Permissions;
public class MyForm : Form
{
const int WM_DEVICECHANGE = 0x0219;
const int DBT_DEVICEARRIVAL = 0x8000; // system detected a new device
const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //device was removed
const int DBT_DEVNODES_CHANGED = 0x0007; //device changed
const int DBT_DEVTYP_VOLUME = 0x00000002; // logical volume
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_DEVICECHANGE
&& m.WParam.ToInt32() == DBT_DEVICEARRIVAL
|| m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE
|| m.WParam.ToInt32() == DBT_DEVNODES_CHANGED))
{
if (m.WParam.ToInt32() != DBT_DEVNODES_CHANGED)
{
int devType = Marshal.ReadInt32(m.LParam, 4);
if (devType == DBT_DEVTYP_VOLUME)
{
//Poll drives
}
}
else
{
//Poll drives
}
}
base.WndProc(ref m);
}
}
Converting Videos for the Creative Zen
I have a Creative Zen, its a very nice little mp3/video player, and while it plays xVid/DivX/WMV its a bit picky about what it will play.
So ive created this script to convert videos to a format it will play (320×240, xVid, with auto black bars to maintiain aspect ratio & 128KB ABR MP3 audio).
Its a Bash script using Mplayer / Mencoder.
Files
Making better use of mod_deflate
Output compression using Gzip and Deflate is a common feature of modern webservers. Webpages can be compressed by the server and then decompressed by the client seamlessly.
By default (at least on debian/ubuntu) Apache has a module installed and enabled called mod_deflate. While great, here is the default configuration:
AddOutputFilterByType DEFLATE text/html text/plain text/xml
Now at a glance this is fine, But modern webpages consist of more than just html, we have CSS, Javascript, RSS and even JSON, all of which can benefit from compression but aren’t enabled by default.
Here’s a modified config file that will compress these files:
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/javascript text/css application/rss+xml application/json
This config is usually located at /etc/apache2/mods-enabled/deflate.conf.
With this done jQuery (minified) goes down from 54KB to just 16KB of data send to the client
CSS Columns and the Box Model
One of the greatest frustrations for me when designing a website has been using css to layout a multi-column format (like this one).
I’ve found that it is important to understand the box model. more importantly knowing that when you set the width of a block element (ie div) it sets the width of the content. The padding, border and margin is wrapped arond it.

SSH Passwordless Login
Here is a script that adds your SSH public key to a remote hosts authorized_keys file. Thus enabling you to login without a password. (more…)
Using a Linux Box to share Internet Access
So recently i had to setup my linux server to be able to dialup to the internet and act as a internet sharing box for the other pc’s on the network. This is how i did it. (more…)