
June 28th, 2007 by

Pete
PHP monitor server log
Occasionally you'll be working on a webserver and not have SSH access to it. Below is a simple script I've written that reads a server log (in this case the error log) and outputs a portion of it to a webpage.
PHP:
-
$no_lines = 25;
-
-
$filename = $_SERVER['DOCUMENT_ROOT']."/../logs/error.log";
-
$handle =
fopen($filename,
"r");
-
-
-
-
$contents =
trim($contents,
"\n");
-
$contentsArr =
explode("\n",
$contents);
-
-
$n = $size - $no_lines;
-
-
-
<h1>monitor log</h1>
-
";
-
-
<ol start=\"$n\">";
-
-
for ($n; $n<$size; $n++)
-
{
-
-
<li>".$contentsArr[$n]."</li>
-
";
-
}
-
-
-
";
Obviously in a production environment you'd want to add some sort of password protection to such a script.
/* Pete Graham */
Posted in Uncategorized, Website Development, php |
4 Comments »

June 28th, 2007 by

Pete
Everyone hates spam, I hate it even more than most people. If you put an email address on a website, as a mailto hyperlink, then it's likely that spam bots will find this address and start sending annoying emails.
One way to get around this is to use a clever technique which I discovered over at SpamSpan.com. Instead of entering your email like a normal hyperlink use the code below:
HTML:
-
-
<span class="u">pete.graham
</span> [at]
<span class="d">domain-name.com
</span>
-
(
<span class="t">Pete's Email Address
</span>)
-
</span>
A nifty piece of Javascript is then used to convert this html into a normal mailto hyperlink. Spam bots don't have Javascript enabled so wont register the link. If a visitor to your site have Javascript disabled they will see the link in the form below:
petegraham [at] domain-name.com (Pete's Email Address)

# Pete Graham
Posted in Uncategorized, Website Development, spam |
No Comments »

June 5th, 2007 by

Pete
One of the things I always found awkward about Vim is stretching for the ESC key to get out of insert mode.
Here's a little trick that I like to add into my .vimrc file:
CODE:
-
map <space> i
-
imap <S-space> <esc>
This will let you escape insert mode by pressing shift and space and enter insert mode by pressing space.
/* Pete Graham */
Posted in vim |
No Comments »

June 5th, 2007 by

Pete
I'm not a fan of applications that use sounds effects, one of the first things I do when configuring a new PC is to set the Windows Sound scheme to 'No Sounds'. IE7 has the hugely annoying feature where a click sound is made when you click a link with the middle mouse button (this opens the link in a new tab).
My preferred browser of choice is Firefox but working in Web Development I often need to test website across a range of different browser, hence the need to use IE7. So far I have found no way of disabling this infuriating sound! Tortoise SVN is another offending program, there's nothing worst than having listening to some music interrupted with the 'Conflicting Files' sound!
If anyone finds any way of disabling the IE7 noise let me know and you will be my Hero for a whole day!
# Pete Graham
Posted in Uncategorized, Internet Explorer, Website Development |
3 Comments »

May 16th, 2007 by

Pete
In PHP you can provide file downloads by using a PHP page to “pass through” the file. Of course you could just link to the actual file, however this technique means your file can kept outside of your website root. Another advantage is you can integrate this system into a PHP based user/password system easily.
Now this technique is fairly straightforward, but you need to make sure you get the HTTP Headers correct for it to run smoothly. Here’s the code:
PHP:
-
$file = "/path/to/file.txt";
-
-
-
//This will set the Content-Type to the appropriate setting for the file
-
switch ($file_extension)
-
{
-
case "htm":
-
case "xhtml";
-
case "txt";
-
case "html": $ctype="text/html"; break;
-
case "pdf": $ctype="application/pdf"; break;
-
case "doc": $ctype="application/msword"; break;
-
case "exe": $ctype="application/octet-stream"; break;
-
case "zip": $ctype="application/zip"; break;
-
case "xls": $ctype="application/vnd.ms-excel"; break;
-
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
-
case "gif": $ctype="image/gif"; break;
-
case "png": $ctype="image/png"; break;
-
case "jpeg":
-
case "jpg": $ctype="image/jpeg"; break;
-
case "mp3": $ctype="audio/mpeg"; break;
-
case "wav": $ctype="audio/x-wav"; break;
-
case "mpeg":
-
case "mpg":
-
case "mpe": $ctype="video/mpeg"; break;
-
case "mov": $ctype="video/quicktime"; break;
-
case "avi": $ctype="video/x-msvideo"; break;
-
default: $ctype="application/force-download";
-
}
-
-
//Begin writing headers
-
-
-
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
-
header("Cache-Control: public");
-
header("Content-Description: File Transfer");
-
//Use the switch-generated Content-Type
-
header("Content-Type: $ctype");
-
-
-
// regex stops download prompt for browser associated types
-
if (!
preg_match('/(htm|xhtml|txt|html|gif|png|jpeg|jpg)/',
$file_extension,
$matches))
-
header("Content-Disposition: attachment; filename=\"".
basename($filename).
"\";" );
-
-
// output the file
-
-
I will be writing a follow up article in the future that shows how this technique can be improvement using Apache's mod-rewrite to provide cleaner URLs for file downloads.
Note: The regular expression on line 42 will make files normally associated with the browser (images, webpages, etc) open inside the browser. If you want to get the “open/save” download prompt for all types of files then comment out this line.
Note: Make sure you use: “Content-Type: image/jpeg” for JPGs, IE doesn’t seems to like “Content-Type: image/jpg”
# Pete Graham
Posted in Website Development, php, regular expressions, programming |
2 Comments »

May 15th, 2007 by

Pete
There is a great talk that went up on Google Videos earlier this month called Away with Applications: The Death of Deskop. In it the speaker, Aza Raskin points out that no work is carried out actually using the computers desktop. He also points out the failing of desktop applications, that you frequently need to switch between applications, moving content, to achieve even relatively simple tasks.
I think his points are particularly relevant as we move into an age where many traditional desktop apps are being converted to web applications. I see an increasing trend in web applications of people trying to recreate desktop applications like interfaces in a browser-based setting. Aza urges us to rethink what we know about computer interfaces and to come up with exciting and innovative alternatives.
Traditional applications have had more and more functionality added to them to an extent that they have become “a portmanteau of all possible features”, again it is worrying that many of today’s successful web apps could suffer the same fate. The speaker encourages open APIs and Mash-ups as an alternative and a may to combine applications without having to switch between multiple programs/services.
One of the most interesting parts of the talk for me is when the speaker demos some systems that he's been developing himself, one is a command-line interface for launching applications that uses almost real language commands and auto-complete. The other is an interesting zoomable desktop, which is much more work orientated than the traditional model, it also centres around the concept of “content as content”.
At nearly an hour and a half the video is quite long but I found it extremely interesting, the speaker also looks a bit like Rick Moranis of “Honey I shrunk the kids” fame, which has got to be a good thing.
# Pete Graham
Posted in Tech, usability, videos |
No Comments »

May 15th, 2007 by

Pete
The other day I was programming in PHP and needed to get the first index in an associative array. There doesn’t appear to be a built in function to deal with this so I wrote my own.
PHP:
-
function getArrayFirstIndex($arr)
-
{
-
foreach ($arr as $key => $value)
-
return $key;
-
}
Here's a bit of code you can test it on:
PHP:
-
$arr['banana'] = "yellow";
-
$arr['apple'] = "apple";
-
$arr['orange'] = "orange";
-
-
$first_index = getArrayFirstIndex($arr);
-
-
echo "first index =".
$first_index;
The above code will output 'first index = banana'.
/* Pete Graham */
Posted in Website Development, php, programming |
4 Comments »

May 14th, 2007 by

Pete
Recently I’ve been making an effort to improve my JavaScript skills, I’m not a JavaScript-Jedi yet but I have been picking up a few, tips, tricks and techniques.
One thing that I find extremely useful is using an init function to call other functions when the page has loaded. I have been using the nifty function below:
JAVASCRIPT:
-
function WindowOnload(f)
-
{
-
var prev=window.onload;
-
window.onload=function(){ if(prev)prev(); f(); }
-
}
I save this function in a file called init.js which is then included in my HTML pages using:
HTML:
-
<script xsrc="/js/init.js" type="text/javascript"></script>
Functions can then be initialised by including the following in the HTML:
JAVASCRIPT:
-
<script language="JavaScript">
-
<!--
-
-
WindowOnload(function()
-
{
-
a_function('Sdelete_date');
-
});
-
-
WindowOnload(function()
-
{
-
another_function('a_parameter');
-
});
-
-
//-->
-
</script>
The above code is inline JavaScript which is often seen as being naughty, obviously you can save the same code to a separate file and include that instead.
I originally saw this technique here. Alternative JS initialisation scripts are available but so far this one has worked well for me, so I’ll be sticking with it.
# Pete Graham
Posted in javascript, Website Development, programming |
No Comments »

May 3rd, 2007 by

Pete
Vim has some really useful commands, especially search and replace ones. Unfortunately I'm always forgetting the finer details of how to use them so here's an example:
CODE:
-
:%s/$/\rthis is a new line
To break down whats happening above:
- : switches Vim into command mode
- % means we're going to apply this command to every line in our file
- s means we're using the substitute command. The format of the command is s/search/replacement, 'search' can be a regular expression
- $ tells vim to search for the end of the line
- \rthis is a new line tells vim to replace with a new line character, and to put the text "this is a new line" on that line.
NOTE: \r is used as the new line character when vim is using DOS file format, to find out which file format you are using type :set ff? You can find out more about Vim file formats here.
#Pete Graham
Posted in regex, regular expressions, vim |
9 Comments »

April 30th, 2007 by

Pete
When I was doing my Computer Science degree our lectures used to make a big deal about making sure you spell Internet with a capital ‘I’, because it’s important and it’s the name of a thing. Apparently it’s now been decided that it should be spelt with a lowercase ‘i’, I wish bloody linguists would make their minds up!
# Pete Graham
Posted in Tech |
2 Comments »