Press "Enter" to skip to content

Month: July 2009

PHP: Extract just the domain name from an URL

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:

// let's test the code works: these should all return
// , or
foreach ($domains as $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].'
} else {
print 'Domain not found in '.$url.'

$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

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
  • 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

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 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!