PHP Extract Function

The extract function is quite handy in PHP.  I can do this:

t1.php

<?php
$a = 'this is variable A';
 
extract(array('b' => $a));
?>

t2.php

I can call variable A using a different variable thanks to extract:  <?php echo $b; ?>

More info about this function can be found here.

Local Development on iPhone with OSX and Charles Web Proxy

I recently had to do some development on the iPhone. Although the emulator is great, I wanted to test my website on an actual website. However, I did not want to do my testing on a world/company accessible location. Turns out, you can accomplish this task quite easily with Charles and OSX. Here’s how:

Step 1: Download Charles web proxy

I am typically against paying for tools, but Charles is pretty sweet. You can download it here.

Step 2: Create a personal wifi hotspot with OSX

In System Preferences go to Sharing. Within Sharing click on Internet Sharing. Select AirPort Options...:

You’ll probably want to set a password for your own wifi network. I use 40bit WEP encryption (password must be exactly 5 characters long). Click Ok when you finish.

Now select the checkbox next to internet sharing. You’ll be prompted with a warning (don’t worry about it). Click Start.

Step 3: Setup Charles Proxy Setting

You will most likely have the same install information as me, so this step is to confirm that assumption. In Charles goto Proxy > Proxy Settings...

And verify that the proxy port is 8888. Hit OK when you are done.

Step 4: Setup Proxy on iPhone

We are now ready to configure your iPhone to use the proxy. Before we can get started, you must install the Charles Proxy certificate on your iPhone. The certificate is located at:

http://charlesproxy.com/charles.crt

You will be prompted to install the certificate. Go ahead and do that! When you are done, go to Settings > Wi-Fi. Connect to the wireless network you just created (mine is called deadpool).

Once you are connected, click on the circle blue arrow to go into network settings. We need to setup our proxy information here. On the top part of the screen, get the IP address of the router. Mine is 10.0.2.1 (you will likely have the same one as me).

At the bottom of the screen under HTTP Proxy you will need to set it to manual. Then, enter the IP address you just collected and set the port to the Charles Proxy port (should be 8888).

Alternative Method: Use your assigned IP as the Proxy

I realize that it is actually a lot easier to use my existing network IP address instead of creating a hotspot. In System Preferences > Sharing > Web Sharing you will see your IP address. Enter that as the IP address as your HTTP Proxy on your iPhone (other method uses 10.0.2.1 as your HTTP Proxy).

Step 5: Go!

Visit the page in your browser. Done!

Happy debugging!

Regex Find and Replace on Textmate

I usually do the old school way of find replace of simple strings. Turns out you can be even more elegant by doing a regex find and replace.

For example, lets say that I have a text file that looks like this:

$CONF['SWEET'];
print_r("This is a " . $CONF['hey'] . "test for us to use.");

I want to replace the array lookup with a function call. So I will search for this regex:

\$CONF\[(.*)\]

And replace with this regex:

$config->get($1)

A simple Command + F will bring up a window for you to enter this:

After a replace all, the text file will now look like this:

$config->get('SWEET');
print_r("This is a " . $config->get('hey') . "test for us to use.");

Simple, but it just might save you a bunch of time.

Fallback Javascript Loading

I love the idea of using a CDN to serve up jQuery and other javascript libraries. It seems faster and ultimately better for caching amongst various sites that share the same code (especially true of javascript libraries like prototype or jQuery). However, when things hit the fan it’s best to be able to load from a local copy of the code:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.4.2.js"%3E%3C/script%3E'))</script>

Also, the lack of the protocol in the loading operation is called “protocol relative url”–see the RFC!

Setting up Apache on Snow Leopard (Easy!)

Setting up Apache on Mac OSX 10.6 is a snap.  Sure, there’s tools out there like MAMP, but why bother with the download when everything is already setup on your machine? Now to get started:

Where will I store my code?

Instead of storing all of my development code in the default public_html or sites folder on my machine, I’d like to store it in my code folder and just serve it from there. To be even cooler I want to setup some custom urls that are mapped to different sites on my local machine. For example I want http://app1.local to display the php site called app1. This requires me to do 2 things: map custom urls to my local machine and tell apache what the heck to do.

Override IP Mapping with the HOSTS file

Every time you make a request to a website like facebook.com, your computer must translate it to an IP address. Once your computer has the IP address of the said site, it is able to start communicating with the server. This process of translation is now performed automatically without any need for additional computer configuration. In the old days however, computers had to download a file containing all name to ip address mappings (this scaled very poorly since more websites means a larger file that must not only be maintained, but is constantly being downloaded by lots of computers). Anyways, short story long–this file still exists and allows you to manually override certain mappings (more info here). I’m going to tell my computer that the IP address 127.0.0.1 not only points to localhost, but also one called app1.local:

# open the hosts file (this file is owned by the super user, so you must open it using sudo)
$ cat /etc/hosts
 
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost 
fe80::1%lo0	localhost
#

To add an entry for app1.local I’ll add a mapping at the bottom of the file

$ cat /etc/hosts
 
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost 
fe80::1%lo0	localhost
 
# my development sites
127.0.0.1 app1.local

To be safe you’ll want to flush your cache when you update the hosts file. If you havent tried going to http://app1.local then it is unlikely that there will be anything in the cache, but then again why risk it. Flush the cache!

$ dscacheutil -flushcache

Apache Virtual Hosts

Plainly put, virtual hosts allow you to serve multiple different sites on the same computer (instead of 1 site per instance of apache running). For example, I can run both http://app1.local and http://app2.local on my machine. When setting up these virtual hosts I will update the apache configuration file located at /etc/apache2/httpd.conf:

$ head /etc/apache2/httpd.conf 
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.2> for detailed information.
# In particular, see 
# <URL:http://httpd.apache.org/docs/2.2/mod/directives.html>
# for a discussion of each configuration directive.
...
# Include any configuration details for my sites
Include /usr/local/etc/sites/*.conf

The only addition I made to this big monolithic file is telling it to include any conf file from a directory of my sites. Although it is possible to just add configuration stuff to this file, it seems much more manageable to keep it small and just include configuration files as necessary (personal preference though).

I went ahead and created a file on /usr/local/etc/sites called httpd.vhost.conf and added in the following:

# we will be listening on port 80 already so no need to tell apache to listen on port 80
# otherwise, we would specify it using the listen parameter
#Listen 80
 
# we will be using several different named addresses pointing to this ip
# so we need to let apache know about that
NameVirtualHost 127.0.0.1
 
# by default apache is super restrictive, so you need to allow access to the directories
# before apache will server things up.  if not, you'll get a permission denied error
<Directory /Users/USERNAME/Code>
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
 
# the virtual host ip address must match exactly to the NameVirtualHost specific above
<VirtualHost 127.0.0.1>
	# files will be served from this location
	DocumentRoot /Users/USERNAME/Code
 
	# the requested server name was app1.local
	ServerName app1.local
</VirtualHost>

Enabling Apache

Now fire up terminal and get apache running!

# start apache as the root user
$ sudo apachectl start
 
 
# check to see that it's running
$ ps -e | grep httpd
18066 ??         0:00.23 /usr/sbin/httpd -D FOREGROUND
18067 ??         0:00.00 /usr/sbin/httpd -D FOREGROUND

That’s it. Now get back to developing! Gist available.