Gaining access to a visitor’s IP Address in PHP

PHP‘s HTTP_X_FORWARDED_FOR and REMOTE_ADDR server variables store the IP Addresses of a visitor’s connection and any proxy server that their connection was forwarded through.

To use these details in your code, write a script similar to this:

< ?php
 
  $addressForwarded = $_SERVER['HTTP_X_FORWARDED_FOR'];
  $addressRemote = $_SERVER['REMOTE_ADDR'];
 
  echo "Connection forwarded for: {$addressForwarded} " .
       "and originated from {$addressRemote}.";
 
? >
Read full storyComments { 3 }

Video: Merlin Mann – Inbox Zero

Open this post to watch a video of Merlin Mann, a well-known productivity guru and creator of the popular 43 Folders website, talk about the importance of getting your inbox to zero as well as strategies for dealing with high volume email.

Read full storyComments { 0 }

‘Variable variables’ and array syntax ambiguity in PHP

In PHP it’s possible to define variable variables, variables with names that are set and used dynamically.

Normal variables are set with statements like:

<?php
 
  $fruit = 'apple';
 
?>

Variable variables are set by using two dollar signs in front of a normal variable‘s name:

<?php
 
  $fruit = 'apple';
  $$fruit = 'red';
 
?>

This results in two variables being defined: $fruit with the value ‘apple‘, and $apple with the value ‘red‘, which means that these two echo statements will produce the same results:

<php
 
  $fruit = 'apple';
  $$fruit = 'red';
 
  echo "$fruit ${$fruit}";
  echo "$fruit $apple";
 
?>

Associative arrays and foreach loops

Multiple variable variables can be created from associative arrays using a foreach loop:

<?php
 
  $fruit = array('lime'   => 'green',
                 'apple'  => 'red',
                 'banana' => 'yellow');
 
  foreach ($fruit as $key=>$value)
  {
    $$key = $value;
  }
 
  print_r($fruit);
 
  echo "\nLime: {$lime}\n";
  echo "\nApple: {$apple}\n";
  echo "\nBanana: {$banana}\n";
 
?>

… resulting in:

Array
(
    [lime] => green
    [apple] => red
    [banana] => yellow
)
 
Lime: green
 
Apple: red
 
Banana: yellow

Ambiguity

Keep in mind that when variable variables are used with arrays, there is an inherent ambiguity problem (see the PHP code examples below). If you write $$fruit[1], the parser needs to know if you meant:

Use ‘$fruit[1]‘ as the variable name, hence creating ‘$apple’ with a value of ‘red‘:

<?php
 
  $fruit = array('lime',
                 'apple',
                 'banana');
 
  $$fruit[1] = 'red';
 
?>

… or…

Assign ‘red‘ to the ‘[1]‘ index on ‘$$fruit’, hence creating an array ‘$apple’ with ‘red‘ in position ‘[1]‘:

<?php
 
  $fruit = 'apple';
 
  $$fruit[1] = 'red';
 
?>

The syntax for resolving this ambiguity is ${$fruit[1]} for the first case and ${$fruit}[1] for the second:

<?php
 
  $fruit = array('lime',
                 'apple',
                 'banana');
 
  ${$fruit[1]} = 'red';
 
 
  $fruit = 'apple';
 
  ${$fruit}[1] = 'red';
 
?>
Read full storyComments { 0 }

How to remove and replace a Microsoft Office package serial

On Windows machines, registry corruption could necessitate that you remove and reinstall a Microsoft Office package serial number.

The instructions below explain how to do this, but involve modifying the Windows Registry. If done incorrectly you could end up with a corrupt registry, so making a backup of the registry before you follow the instructions below is strongly recommended.

Please note that the instructions below are intended to be used on Microsoft Windows 7 Professional installations.

  1. Close all Microsoft Office Applications.
     
  2. Click on the Start button and move to the search programs and files.
     
  3. Type “regedit” and press Enter.
     
  4. This puts you in registry editor, Locate the following subkey:

     
    HKEY_LOCAL_MACHINE  >> Software >> Microsoft >> Office >> 12.0 >> Registration You will find another subkey insde:

     HKEY_LOCAL_MACHINESOFTWAREMicrosoftOffice12.0Registration

     {30120000-0011-0000-0000-0000000FF1CE}

  5. Under the subkey there are Globall Unique Identifiers (GUID) that contain a combination of alphanumeric characters. Each GUID is specific to a program that is installed on your PC. click to open each GUID subkey to view and identify the Microsoft Office product.
     
  6. After you find the GUID subkey that contains your Microsoft Office product. Delete the registry entries by right clicking on the registry entry and clicking Delete.
     
  7. Exit Registry Editor.
     
  8. Open a Microsoft Office application such as Word or Outlook. Office 2007 will prompt you to enter a new character product key.
     
  9. Type in the product key and then click Continue.
     
Read full storyComments { 1 }

Bash script to generate MD5 hashes

The simple Bash script below can be used to generate random MD5 hashes. To change the amount of hashes generated, you only need to change the value of the “limit” variable:

#!/bin/bash
 
counter=0
limit=10
 
while [ "$counter" -lt "$limit" ]
do
 
md5 -s "$counter $RANDOM `date`" | grep -o "[a-z0-9]\{32\}"
let "counter += 1"
 
done
 
echo
 
exit 0

To run this script from the command line (in my examples below the script is saved to “md5.sh”), you would use the command line to navigate to its directory and enter:

sh md5.sh

To run this script from the command line and save the output to a file, you would use the command line to navigate to its directory and enter:

sh md5.sh > output.txt
Read full storyComments { 0 }