yes i love technology

PHP monitor server log

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:
  1. $no_lines = 25;
  2.  
  3. $filename = $_SERVER['DOCUMENT_ROOT']."/../logs/error.log";
  4. $handle = fopen($filename, "r");
  5. $contents = fread($handle, filesize($filename));
  6. fclose($handle);
  7.  
  8. $contents = trim($contents, "\n");
  9. $contentsArr = explode("\n", $contents);
  10. $size = sizeof($contentsArr);
  11. $n = $size - $no_lines;
  12.  
  13. <h1>monitor log</h1>
  14. ";
  15. <ol start=\"$n\">";
  16.  
  17. for ($n; $n<$size; $n++)
  18. {
  19.     <li>".$contentsArr[$n]."</li>
  20. ";
  21. }
  22.  
  23. echo "</ol>
  24. ";

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 | 2 Comments »

Mailto Hyperlinks Stop Spam Bots!

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:
  1. <span class="spamspan">
  2. <span class="u">pete.graham</span> [at] <span class="d">domain-name.com</span>
  3. (<span class="t">Pete's Email Address</span>)
  4. </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)

Email Spam Bot

# Pete Graham

Posted in Uncategorized, Website Development, spam | No Comments »

IE7 Clicky Sounds Do my Nut in!

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 Internet Explorer, Uncategorized, Website Development | 3 Comments »

Death to Spammers!

April 12th, 2007 by Pete

I've starting getting lots of Spam comments on my blogs recently. This was becoming increasing annoying because each time someone comments on my blog I get sent an email. Not only were the comments sections, on some of my articles, full of viagra advertisements but my inbox was also full of notification about these comments.

I decided to install the Spam Karma plug-in in an effort to thwart the irritating bots that were making these comments. Since installing it (about 2 weeks ago) I think it has successfully caught each spam post to this site, and set it for moderation. It rarely seems to identify real comments as spam (let me know if one of your comments goes missing). As a Spam prevention plugin it works very well.

Spam Karma is incredibly configurable, the downside of this is its interface isn't very intuitive. It does appear to have some options for moderating your existing comments, but after having very limited success with this I decided to delete the offending comments manually. This is just a small complaint thought and if you do start getting Spammed on your Wordpress blog then I would suggest installing this plug-in ASAP.

Can of Lovely Spam

# Pete Graham

Posted in Uncategorized, plug-ins, spam, wordpress | No Comments »

G(rrrrrhhhh)mail: Slow Google Mail Attachment Downloads

April 10th, 2007 by Pete

I’m a big fan of Gmail but they're a few things about it I find annoying. My Friend Kenny Lee, of Hidden Messages fame, sent me the band’s album, which they had recently finished recording, earlier this week. He attached each track as an mp3 to an email (I believe Gmail has a total attachments limit of 10MBs per email).

So I had 14 emails from Kenny each with an mp3 attached. Gmail saw all these emails and grouped them into a 'conversation'; this was actually very convenient since I could access them all together. My problems started when I went to download the first track, Gmail insists on running a virus check of files before you are offered a download link (again a nice feature). I noticed Gmail was running extremely slowly, this is because it was attempting to virus check every single attachment in the conversation.

Doubly annoying Gmail doesn’t remember which emails in my inbox it’s virus checked, so each time I reload it the whole process kicks of again. Now Kenny had originally sent me the emails from his Gmail account therefore they would have been virus checked when they were uploaded, this just all seems a bit inefficient.

At the end of the day these are minor grumbles and Gmail, in my opinion, is still pretty ace. I guess it’s probably because of things like this that Gmail is still stuck in Beta mode.

# Pete Graham

Posted in Gmail, Uncategorized, hidden messages, rants | No Comments »

PHP: XOR swap

March 26th, 2007 by Pete

The XOR swap lets you swap the value of integer variables without using a temporary variable, it's also slightly faster.

PHP:
  1. $x ^= $y;
  2. $y ^= $x;
  3. $x ^= $y;

I can't actually think of a good reason Why you'd want to use this in PHP since memory isn't normally a big issue, however it might be good for showing off ;-) .

// Pete Graham xXx

Posted in Uncategorized, Website Development, computer science, php | 2 Comments »

MySQL tips: Grants and Changing Passwords

February 23rd, 2007 by Pete

I was doing some work administering MySQL databases earlier in the week. I haven’t done much with MySQL in over half a year since we use Postgres as the backend for phuser.com, needless to say I’d forgotten a lot of the commands! Here are a few MySQL tips which I’m putting up here for my own reference but if anyone else finds them useful then that’s great!

First thing I had to do was create a database, for this example we’ll call the DB stats.

CREATE DATABASE STATS

Normally you will want to create a new user to access your DB, here’s how to do this:

GRANT All PRIVILEGES ON stats.* TO 'statsBoy'@'localhost' IDENTIFIED BY 'rubbishPassword';

Here I have created a new user called "statsBoy" with password "rubbishPassword". I'm giving statsBoy all privileges to the DB and I’m only letting statsBoy connect from localhost so he can only to connect from the server the DB is on. Don’t forget to put the .* on the end of your DB otherwise you won’t be able to access your DB tables (this has screwed me over before!).

After doing any granting you’ve got to flush your privileges for them to take effect:

FLUSH PRIVILEGES;

Doing this will tell you “Query OK, 0 rows affected” it lies! Rows have been affected and your privileges should work now. I normally log on using the mySQL commandline interface and check.

Now my earlier password wasn’t very good, to change it do this:

SET PASSWORD FOR 'statsBoy'@'localhost' = PASSWORD('Rd1x3$k8');

No flushing of privileges is required here. Well that’s all for today MySQL maniacs, I'm sure they'll be more commandline Database Administration fun soon!

// Pete Graham

Posted in MySQL, Uncategorized, changing passwords, database, grants | No Comments »

Email Interaction with Web Applications (using PHP, Postfix and Regular Expressions)

February 9th, 2007 by Pete

One of the popular facilities on phuser.com is the ability to create polls. Once a poll has been created it can be sent to people by SMS or Email. The recipients can reply with their choice of answer and a optional comment using SMS, Email or phuser web interface, lovely!

Handling of emails replies has easy been the trickiest part of this process. Here is a very basic overview of the steps we have taken:

  1. Have postfix forward the email to a PHP command line script that saves the email to file. Details of how to do this can be found here.
  2. Cron runs a PHP script that processes the email files. It makes sure the emails are valid, strips the users reply from the body of the email, and then adds the email to a database table.
  3. Crons runs another script which queries the database table and processes the emails replies, this is where the actually interaction with the phuser system (the magic) happens!

Today I shall be describing the regular expression that we used for Step 3 as it took a bit of effort to get right. Step 2 will be covered at a later date.Warning: when I cover Step 2 I will be having a rant about my new found hatred of HTML emails!

Note: Steps 1 and 2 are separate because security precautions on our servers prevent the Postfix user from interacting with the Databases.

When someone gets a poll email from phuser the content looks like this:

pete (pete graham) wants your opinion:

Which is your favourite drink?

Vote for one of the options below:

1) Tea
2) Coffee
3) I love them both like they were my children
4) Urgh! I can’t stand either!

--------------------- YOUR REPLY BELOW THIS LINE ---------------------

--------------------- YOUR REPLY ABOVE THIS LINE ---------------------

Reply using one of the following methods:

o Reply to this email. Include the number of your vote at the start of your message. Any extra text will be added as a comment. (Please do not edit the Subject line).

Now due to the difference in the way email clients work and the ways people think here is a small selection of the possible replies we needed to be able to handle:

  • > 1 I like to drink tea since I am a English Gentleman!
  • 4 I’ll have whats going
  • 3 – I hate them all
  • > 2)
  • 1) tea because I love tea!!!
  • 1 I'm not a big drinker of either since I'm the no-caffs, but like a cup of decaff tea
  • > 1
  • 2

So what we required is a Regular Expression which would correctly grab the number if one was included at the beginning so we could log the voting option. We also needed to grab the text afterwards if any was included so it could be used as a comment. Here’s what we eventually came up with:

CODE:

  1. /^[^\w]*([0-9]+)[^\w]*(\w.*)/ms

And it works, yay! The 'm' flag is set so the Regex is multiline , the 's' flag makes the dot character match every character including newlines. Here is the page that describes all the PHP regular expression modifiers.

If you want to learn more about regular expressions (and who doesn’t) then loads of information can be found on this site. Another great site is the one that we used to testing our regular expressions, it lets you supply up to 10 different test strings at once.

If you’d like to see this system in action then go to phuser.com and request an invite we are still looking for Beta testers currently, if you sign-up now you can have a real input into the site as it develops and grows.

Ps. The regular expression is in an if statement. If it fails the entire message is put in the comments section on the poll.

# Pete Graham

Posted in Uncategorized, php, phuser, postfix, regex, regular expressions | 5 Comments »

My RSS Confession

January 31st, 2007 by Pete

Even though I work as a website developer until last week I had never bothered to set up an RSS reader. Now I’m not sure why I hadn’t bothered doing this, I was well aware of RSS feeds, I was even running 2 blogs both with RSS feeds. I think I was just stuck in my habit of reading blogs and news websites by navigating directly to the websites.

So I took the plunge and decided to try out Google Reader as Robert Scoble is always banging on about it, I thought about using the built in Firefox RSS reader but a some people were claiming its a bit rubbish.

I thought about getting some sort RSS plug-in for Thunderbird or Outlook but decided that it would be much cleaner to have a web-based solution; I get a bit fed up having to install and configure hundreds of applications everytime I want to use a new computer.

Anyway I’m very glad that I have started using an RSS reader. It helps me keep what I want to read organised, avoid adverts, and stops me opening a silly amount of tabs with articles that I’ll probably never get around to reading. I’m subscribed to about 20 blogs at the moment so am not using any of the more advanced features to keep things organised, I haven’t bothered learning any of the keyboard shortcuts yet either.

In conclusion I can’t really compare Google Reader to any other RSS readers as I haven’t used any others but I’d definitely recommend it to someone wanting to start reading RSS feeds.

Ooohh that RSS feed in the picture above looks really good (nosey people can click the image to see it full size and snoop on what I’ve been reading).

/* Pete Graham */

Posted in Google Reader, RSS, Uncategorized | No Comments »

MSN messenger has a mind of its own!

January 23rd, 2007 by Pete

I often have MSN messenger running on my PC for a number of reasons:

  1. It starts-up when the machine starts-up
  2. Its refuses to let you close it unless you shut outlook, Internet explorer and about 20 other Microsoft programs
  3. I occasionally use it to chat to people online

Friday lunchtime I was chatting to people on Messenger. Once I’d finished I set my status to away as I had work to do. When I unlocked my PC on Monday morning I found 3 people had started conversations with me on Messenger as it had decided to switch my status to online over the weekend!

While I’m ranting about Microsoft my PC Blue Screened of Deathed itself today. IRQ_LESS_OR_NOT_EQUAL, great I’ll remember not to do that in future, thanks Bill.

/* Pete Graham */

Posted in Uncategorized, microsoft, msn messenger, rants | 1 Comment »

« Previous Entries