#!/usr/bin/perl ############################################################################## # By BumbleBeeWare.com 2006 # zipcode lookup # ziplookup.cgi # reads database and finds city and state for zipcode ############################################################################## # ############################################################################## print "Content-type: text/html\n\n"; # if direct access print form for zipcode lookup fields if($ENV{"REQUEST_METHOD"} ne "POST") { print '

To find the city or state for a particular zipcode specify the zipcode.
(US zipcodes only)

Zipcode:
'; exit; } # otherwise lookup city and state based on form inputs and datafile # parse incoming form data read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'}); @pairs = split(/&/,$buffer); foreach $pair (@pairs){ ($name,$value) = split(/=/,$pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $form{$name} = $value; } # open the datafile and get the latatude and logitude for each zipcode open(FILE,"./zipcodes.txt"); @zipdata = ; close(FILE); foreach $zipdata (@zipdata){ $zipcode = ""; $state = ""; $city = ""; chomp $zipdata; ($zipcode, $state, $city) =split(/\|/,$zipdata); if ($form{'zip'} eq "$zipcode"){ $zipstate = $state; $zipcity = $city;} } if ($zipstate eq ""){print "No Match Found";} else {print "The zipcode $form{'zip'} is for $zipcity $zipstate.\n";} exit;