Forum Moderators: coopster

Message Too Old, No Replies

Retrieve/Redirect Page based on Zip Code

Need a simple php script that can redirect to a page based on zip code

         

pointloma

11:05 pm on Dec 5, 2008 (gmt 0)

10+ Year Member



Hey PHP Geniuses...

I've got a small problem. I have 8 people who are advisors for 2200 zip codes split between them.

Essentially, I need a simple php script that when a zip code is entered, it forwards the user to the page of their advisors.

IMPORTANT: BECAUSE OF A CMS LIMITATION, I DO NOT HAVE SQL FUNCTIONALITY.

Thanks for all your help!

coopster

11:25 pm on Dec 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com], pointloma.

If you don't have a database then the next thing would be either a flat file or even just an array of the zipcodes pertaining to each advisor. Do a lookup and use the information to redirect your user to the appropriate page.

pointloma

11:34 pm on Dec 5, 2008 (gmt 0)

10+ Year Member



I guess I need a snippet to start with...I'm pretty new to php

coopster

11:46 pm on Dec 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, if it were a flat file I would likely have something like a delimited set of the zip codes in a text file for each user. But that wouldn't be quite as efficient because you would have to do a disk i/o for each set of zip codes. Therefore, I would likely put them all in the same text file. But if we are going to go that far, we might as well just put them in the PHP handler script and go from there. Something like this ...
$advisors = array( 
'john' => array(12345, 54321, 98765),
'jane' => array(65432, 23456, 56789)
);

... except 1 index entry for each advisor and all of their corresponding zip codes. Then I would loop through the zip codes to determine who their advisor might be and redirect accordingly.

pointloma

11:50 pm on Dec 5, 2008 (gmt 0)

10+ Year Member



But how would it associate with the actual page?

I need them to:

Enter Zip Code
Click Submit

And then be forwarded to their page.

My brain's just hurting from looking at code too long today...

coopster

12:05 am on Dec 6, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, you have your HTML form and request their zip code. Your PHP processing script that is in the "action" attribute of the <form> element would grab that zip code from the POST data, assuming your form is of "method" type "post", and look for the zip code in the array you have defined in that PHP script.
$zip = isset [php.net]($_POST['zip']) ? trim [php.net]($_POST['zip']) : 0; 
$found = false; // initialize a variable
foreach ($advisors as $advisor => $zips) {
if (in_array [php.net]($zip, $zips)) {
$found = true;
break;
}
}
if ($found) {
//redirect your user
} else {
// error of some sort
}

pointloma

3:11 am on Dec 6, 2008 (gmt 0)

10+ Year Member



I like this script and it will work for my purposes with a flat file. Is there a way to get rid of the username field?

<?php

####################################################################
# Password Protect Avanced :: Login Form - v.1.2
####################################################################

// load settings
include_once('settings.php');

// list of users
$users = @file(USERS_LIST_FILE);
if (!$users) die('Cannot find users list!');
// remove php "die" statement (hackers protection)
unset($users[0]);

// prepare users list and redirects
$LOGIN_INFORMATION = array();
$REDIRECTS = array();
foreach ($users as $user) {
$u = explode(',',$user);
if (USE_USERNAME) {
$LOGIN_INFORMATION[trim($u[0])] = trim($u[1]);
$REDIRECTS[trim($u[0])] = isset($u[3]) ? trim($u[3]) : '';
}
else {
$LOGIN_INFORMATION[] = trim($u[0]);
}
}

// timeout in seconds
$timeout = (TIMEOUT_MINUTES == 0 ? 0 : time() + TIMEOUT_MINUTES * 60);

// logout?
if(isset($_GET['logout'])) {
setcookie("verify", '', $timeout, '/'); // clear password;
header('Location: ' . LOGOUT_URL);
exit();
}

if(!function_exists('showLoginPasswordProtect')) {

// show login form
function showLoginPasswordProtect($error_msg) {
include('login_header.php');
include('login_form.php');
include('login_footer.php');

// stop at this point
die();
}
}

// user provided password
if (isset($_POST['access_password'])) {

$login = isset($_POST['access_login']) ? $_POST['access_login'] : '';
$pass = $_POST['access_password'];
if (!USE_USERNAME && !in_array($pass, $LOGIN_INFORMATION)
¦¦ (USE_USERNAME && ( !array_key_exists($login, $LOGIN_INFORMATION) ¦¦ $LOGIN_INFORMATION[$login] != $pass ) )
) {
showLoginPasswordProtect("Incorrect password.");
}
else {
// set cookie if password was validated
setcookie("verify", md5($login.'%'.$pass), $timeout, '/');

// Some programs (like Form1 Bilder) check $_POST array to see if parameters passed
// So need to clear password protector variables
unset($_POST['access_login']);
unset($_POST['access_password']);
unset($_POST['Submit']);

// need to be redirected?
if (isset($REDIRECTS[$login]) && !empty($REDIRECTS[$login])) {
header('Location: '
. ((REDIRECT_PREFIX != '') && (strpos($REDIRECTS[$login], 'http') !== false) ? '' : REDIRECT_PREFIX)
. $REDIRECTS[$login]);
exit();
}
}

}

else {

// check if password cookie is set
if (!isset($_COOKIE['verify'])) {
showLoginPasswordProtect("");
}

// check if cookie is good
$found = false;
foreach($LOGIN_INFORMATION as $key=>$val) {
$lp = (USE_USERNAME ? $key : '') .'%'.$val;
if ($_COOKIE['verify'] == md5($lp)) {
$found = true;
// prolong timeout
if (TIMEOUT_CHECK_ACTIVITY) {
setcookie("verify", md5($lp), $timeout, '/');
}
break;
}
}
if (!$found) {
showLoginPasswordProtect("");
}

}

?>

[edited by: eelixduppy at 9:14 pm (utc) on Dec. 6, 2008]
[edit reason] specifics [/edit]