Networking is the act of connecting one or more computers together using wired or wireless technology.

Subnet Calculator in 9 Lines of PHP

I’ve been spending a lot of time studying for my Microsoft 70-642 exam, an important part of which is subnetting.As an intellectual exercise I wrote a subnet calculator. Enjoy:

1
2
3
4
5
6
7
8
9
 function subnet($hosts)
 {
	$bits       = decbin($hosts) + 2; // Add 2 for Network ID and Broadcast
	$hostBits   = strlen($bits); // find how many bits it takes to represent it
	$cidr       = 32 - $hostBits; // Find slash-notation
	$binaryMask = str_repeat('1', $cidr) . str_repeat('0', $hostBits); // Subnet in binary
	$subnet     = implode('.', array_map('bindec', str_split($binaryMask, 8))); // Subnet in dotted-decimal
	return array('hosts' => $hosts, 'hostBits' => $hostBits, 'cidr' => '/' . $cidr, 'binaryMask' => $binaryMask, 'subnet' => $subnet);
 }

PHP On Windows 7

Last week I decided to take the plunge and install the release candidate of Windows 7 on my office workstation where I happen to do a lot of web development and testing.  My development stack consists of Netbeans IDE, PHP 5.2.8, and IIS 7.  There are plenty of other quality tutorials out there explaining how to get PHP working on IIS 7 so I won’t bother beating that particular dead horse.  However there was one quirky difference between installing PHP on Vista and installing PHP on IIS 7 that developers may want to be aware of before they follow my lead.

The Problem : Editing php.ini as a Standard User

During the process of installing PHP on Windows 7 you will be required to copy php.ini into the C:\Windows directory, and  later point you’ll most likely need to edit this file to suit your needs.

On Windows Vista I simply wasn’t allowed to edit files under the \Windows directory (specifically php.ini) when logged in as a Standard User. Windows 7 was more than happy to allow me to modify and save this file in the same location, logged in with the same credentials. But even after restarting IIS my configuration changes never took effect.

The problem was caused by something called the Virtual Store, an application compatibility feature that existed in Windows Vista but works a little bit differently in Windows 7.  Where Vista wouldn’t let me edit this file at all, Windows 7 happily lets me modify it but transparently saves the changes in a different location. The updated php.ini was stored in %UserProfile%\AppData\Local\VirtualStore\Windows\php.ini.  The service account which IIS runs under can’t see this updated file

The Solution: Run As Administrator

When editing php.ini, don’t simply double-click it and make changes.  You must run your text editor as an administrator to edit the file, otherwise the changes will be saved to the Virtual Store.