Filter Access by IP Address

This is a code snippet to keep unwanted visitors out while a site is under construction or being updated. The associative array helps to keep record of the identity of the user’s IP instead of just a collection of numbers with no semantic association. The $filter_ips value makes it easy to toggle the filtering on/off.

<?php
// ---	Filter vitsitor's IP address and bounce those not in the list.
$bounce_url 	= '/maintenance.php';
$filter_ips 	= true;
$allow_ips 	= array (
	'ORG: Firstname Lastname' 	=> '00.00.00.00'
);
if ( $filter_ips && !in_array ( $_SERVER[ 'REMOTE_ADDR' ], $allow_ips ) ) {
	header ( "location: $bounce_url" );
	exit ();
}
?>

It’s also helpful to output the user’s IP address on your bounce page so user’s trying to gain access can easily copy/paste their IP to send to you:

<?php echo $_SERVER[ 'REMOTE_ADDR' ]; ?>

MySQL Trace

I moved an install of a CodeIgniter app from netbook to desktop (both running XAMPP) and notice these peculiar errors started showing up on every page:

A PHP Error was encountered

Severity: Warning

Message: Unknown: 3 result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query()

Filename: Unknown

Line Number: 0

Took me a while to figure out but this PHP setting in php.ini was causing the error:

mysql.trace_mode = Off

XAMPP php.ini

Note to self: when installing XAMMP for development on your legacy apps, be sure to set PHP short tags in php.ini:

short_open_tag = On

jQuery iFrame access

Trying to manipulate the iframe from a jQuery script running inside the iframe. Here is how to get a handle on the iframe with jQuery when you have id attribute iframe-id-attribute set on the iframe:

var ifr = $( '#iframe-id-attribue', window.parent.document );

MySQL Single Record Transfer Between Databases

I had a couple records disappear and needed to pull them from backup. The client had already made extensive changes to the database via CMS so I couldn’t copy over the whole database without overwriting all of their changes. Here is the SQL I came up with to transfer a single record:

INSERT INTO target_db.table 
SELECT backup_db.table.* 
FROM backup_db.table
WHERE backup_db.table.id = '{000}'

Replace target_db with the name of the database you are copying to.
Replace backup_db with the name of the backup database you are copying from.
Replace table with the name of the table you are handling.
Replace {000} with the id field value of the record to transfer.