PHP: Extract just the domain name from an URL

July 30th, 2009 by Richy B. No comments »

Although I posted this on Stack Overflow, but I thought for reference purposes I’ll keep a copy here as well on how to extract the domain name from a URL in PHP:

If you just want to handle three character top level domains – then this code works:

<?php
// let's test the code works: these should all return
// example.com , example.net or example.org
$domains=Array('here.example.com',
'example.com',
'example.org',
'here.example.org',
'example.com/ignorethis',
'example.net/',
'http://here.example.org/longtest?string=here');
foreach ($domains as $domain) {
testdomain($domain);
}
function testdomain($url) {
if (preg_match('/^((.+)\.)?([A-Za-z][0-9A-Za-z\-]{1,63})\.([A-Za-z]{3})(\/.*)?$/',$url,$matches)) {
print 'Domain is: '.$matches[3].'.'.$matches[4].'
'."\n";
} else {
print 'Domain not found in '.$url.'
'."\n";
}
}
?>

$matches[1]/$matches[2] will contain any subdomain and/or protocol, $matches[3] contains the domain name, $matches[4] the top level domain and $matches[5] contains any other URL path information.

To match most common top level domains you could try changing it to:

if (preg_match('/^((.+)\.)?([A-Za-z][0-9A-Za-z\-]{1,63})\.([A-Za-z]{2,6})(\/.*)?$/',$url,$matches)) {

Or to get it coping with everything:

if (preg_match('/^((.+)\.)?([A-Za-z][0-9A-Za-z\-]{1,63})\.(co\.uk|me\.uk|org\.uk|com|org|net|int|eu)(\/.*)?$/',$url,$matches)) {

etc etc. If you want a list of top level domains, you may find Mozilla’s TLD List useful and DKIM Reputation Detected Registered Domains code handy.

Techy: Resetting a Windows Vista Password

July 14th, 2009 by Richy B. 4 comments »

Due to staff changes at work, we had two Windows Vista PCs (Business Edition, but these instructions should work all all versions of Windows XP and Vista) which had installed/purchased software on them, but we did not know the user passwords and/or administrator passwords. We also did not have a “Password reset disc”. We therefore needed to “break into” them and it’s a lot easier than you expect.

  • First of all, from a machine which you do have access to, download the Trinity Rescue Kit (TRK) from http://trinityhome.org.
  • Now burn the ISO image to a CD/DVD (instructions will vary depending on which platform you downloaded the ISO from and what software you are using). Make sure you burn it as a “Disc Image (ISO)” (aka a “Bootable CD/DVD”) and not just storing the file “as is”.
  • Insert the TRK CD into the machine you need recovering and get the machine to boot from the CD drive (on Dell machines, just press F12 as it is booting for the “Boot Menu” and select “Boot from onboard or USB CD/DVD drive”).
  • Wait for TRK to load.
  • Select the first option on the TRK menu as you don’t need any fancy extras
  • Once booted, type “winpass -l” (that’s all in lower case and finishes with a hypen and then a lower case L for lima)
  • If you get a message about Windows already being mounted, just select the “Force” option.
  • You may then be asked to select a Windows installation – select one and see if it lists the username you are trying to reset. If it doesn’t, repeat the previous step and select another Windows installation
  • Make an exact note of the username as the spelling, spacing and capitalisation will matter
  • Type “winpass -u 'User Name'” where ‘User Name’ is the exact username you are changing. Select the same Windows installation as you did previously.
  • Select option 1 to reset the password for that account to “blank” and, if necessary, unlock the account with option 4 (if there have been too many resets previously).
  • When finished, type “q” and press return to exit winpass.
  • Eject the CD/DVD and reboot.
  • You should now be able to log into that user account without using a password (if it is the only Windows account on the machine, it should auto-boot into that account)

Needless to say, neither myself nor the makers of Trinity Rescue Kit can take any responsibility for any damage/problems caused by following these instructions and I can only say “it worked for me!” (twice).

PHP: Zend Framework and Browser Content-types

July 8th, 2009 by Richy B. 2 comments »

I’ve just added a bit more security on to the system I’m working on and for that, I needed to check what the Content-type of the content the browser was sending (as I’m looking at checking that it’s an “application/x-amf” content type requested via Flash). Since I’m using Zend_Framework and wanted to make the check in the controller, it seemed sensible to use:
if (!($this->getRequest()->getHeader('Content-type')=='application/x-amf')) {
throw new Exception('Not a valid request');
}

which worked ok in Firefox. However, when testing in Internet Explorer, I always got “Not a valid request”… Why? Well, IE sends a “Content-Type” header instead of “Content-type”: note the different capitalisation!

It gets worse! Google Chrome (based on Apple WebKit), sends “content-type”.

Here’s the summary

Browser Content type field Content length field
Google Chrome 2.0.172.33 content-type Content-Length
Firefox 3.0.11 Content-type Content-Length
Microsoft Internet Explorer 8.0 Content-Type Content-Length
Safari 4.0 Content-Type Content-Length

Hope it helps somebody else!

YouTube: Japanese Queen

June 9th, 2009 by Richy B. No comments »

In another shameless Japanese-orientated post stealing from Accordion Guy, here’s a Queen Medley (entitled “Joousama Monogatari” by the Japanese group Joousama):

In order, the songs (if you don’t recognise them) are:

  • Bohemian Rhapsody
  • Bicycle Race
  • Killer Queen
  • Flash Gordon
  • We Will Rock You
  • Another One Bites the Dust
  • A Crazy Little Thing Called Love
  • We are the Champions

Tech: Apple iPhone 3Gs in the UK

June 8th, 2009 by Richy B. No comments »

Well, the Apple iPhone 3GS (3G Speed) has been announced on at the Apple WWDC (Worldwide Developer Conference) and thanks to O2 on Twitter, here’s the key points for UK people wanting the iPhone:

  • The 3GS be available on the O2 Mobile Phone Network in the UK on the 19th of June
  • The 16Gb version will cost up to £184.98 and the 32Gb version up to £274.23 depending on the contract you chose: an 18month contract will be £29.38 for the 16Gb and £34.26 per month for the 32Gb (and these are the highest prices). In the US, the 16Gb version costs $199 and the 32Gb costs $299.
  • You will be able to upgrade to an iPhone 3GS from another O2 contract phone when you are eligible for an upgrade. You will be able to leave your current contract early if you want it the new iPhone asap: however, you’ll need to speak to O2 and they’ll explain to you how much “early termination” would cost.
  • O2 will be supporting MMS (out of your normal text message allowance: one Multimedia Message will cost four SMS from your allowance)
  • O2 will also be supporting tethering (allowing your computer to use the iPhone as a 3G modem). However, this will be an extra Internet Tethering Bolt One costing £14.68 per month for 3G and £29.36 per month for 10Gb. Both include access to The Cloud’s Wifi points.
  • More information will be available on o2’s website at o2.co.uk/iphone as soon as possible
gamy-dance
%d bloggers like this: