Saturday, November 21, 2009

Setting up a local Virtual Host using WAMP/Apache

Setting up a virtual host will allow you to essentially run an exact copy of your site locally.  You'll need to decide on a local domain name.  My standard is to only maintain the primary name.  So for instance if the site is then I will use http://domain/ for the local copy.  Some people decide to use http://domain.loc/.

Once you've decided, open C:\Windows\System32\drivers\etc and open the hosts file.

It should look like this when opened:       localhost
::1             localhost

Add a new line       localhost       domain
::1             localhost

Replace "domain" with "domain.loc" if you want.

Next you're going to need to locate your Apache installation. In WAMP, you're going to need to open C:\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf and add the line:

    ServerAdmin webmaster@localhost
    DocumentRoot "c:/wamp/www/domain"
    ServerName domain
    ErrorLog "logs/domain.log"
    CustomLog "logs/domain.log" common
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Allow,Deny
        Allow from all
        Allow from


NameVirtualHost *:80

    DocumentRoot "c:/wamp/www"
    ServerName localhost

If you're using .loc, all you should have to do is add .loc to the ServerName, NOT to the paths defining the location to load.

Friday, November 20, 2009

Regex Library

I was doing a search for some regex that I could modify to meet my specific needs for a project (I haven't learned it yet). I stumbled on this site that has tons and tons of regex examples. I figured it was a link I definitely wanted to keep so I'd post it here. Who knows, maybe you'll find it useful too.

Wednesday, November 18, 2009

PHP Force File Download

When setting up a file download on a web site you never want to link to the file directly.  By linking to the file directly you're letting the browser determine the best action to take.  If the file is capable of being opened in the browser (.txt, .jpg, .pdf) then that's what the browser will do.  However you can easily force the browser to give a download prompt.

$file = '/home/user/public_html/';

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file));
header('Connection: close');


Run PHP via Cron

It's simple, run the following command using an absolute path to the file.

php -q /home/user/public_html/file.php