Forum Moderators: coopster

Message Too Old, No Replies

Split Array to redirect two types of traffic?

         

JAB Creations

9:34 pm on Nov 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to figure out how to modify the following script so that I can redirect two or more types of traffic to different websites.

The example below has two (not sure about the term) (sub?) arrays, fire and water. What this script does is detect strings in the referrer and if any part of the referrer contains a string in the array the visitor is immediately redirected by the server. The redirect.php simply handles the HTTP query and works just fine (allows me to monitor outbound traffic if this script is triggered). I want to modify the script so that any strings in the water sub-array ultimately redirected to redirect.php?lookingfor=water and of course visitors with referrers that match the fire sub-array strings to be redirected to redirect.php?lookingfor=fire.

I had more then just a little help getting this working in the first place so I'm not sure how to even approach this right now. Is there a specific or general term for what I'm trying to do?

- John

<?php
$referer = $_SERVER['HTTP_REFERER'];

$searchstrings = array
(
'fire' => array // $searchstrings['fire']
(
'fire',
'hot',
'lava',
),
'water' => array // $searchstrings['water']
(
'water',
'liquid',
'h20',
)
);

if ($referer == '') {
$result = FALSE;
}else{
$result = FALSE;
foreach ($searchstrings as $group) {
foreach ($group as $ua){
if (stristr($referer,$ua)) {
$result = TRUE;
break 2;
}
}
}
}

// Redirect
// if ($result == true) {header("Location: http://www.example.com/redirect.php?lookingfor=fire");}
// if ($result == true) {header("Location: http://www.example.com/redirect.php?lookingfor=water");}
?>

d40sithui

11:06 pm on Nov 26, 2007 (gmt 0)

10+ Year Member



i think i understand your issue.
you can try the in_array() function, which simply searchs an array for a variable. the only thing is, its case sensitive so you will need to strlolower() all your search keys and array elements.

<?

$keywords = array("fire", "lava");

if(in_array($keywords, $searchstrings['fire']){
//redirect to fire page
}

elseif(in_array($keywords, $searchstrings['water']){
//redirect to water page
}

else{
//default
}
?>

PHP_Chimp

11:12 pm on Nov 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about -
$referer = $_SERVER['HTTP_REFERER'];
$searchstrings = array
(
'fire' => array // $searchstrings['fire']
(
'fire',
'hot',
'lava',
),
'water' => array // $searchstrings['water']
(
'water',
'liquid',
'h20',
)
);
$result = false;
foreach ($searchstrings as $group) {
foreach ($group as $ua){
switch ($ua) {
case 'fire':
$result = 'fire';
break;
case 'water':
$result = 'water';
break;
default:
// dont know if you want something in here
break;
}
}
}
//////////////////////// or even - ///////////////////////////
$result = $ua;
// Redirect
switch ($result) {
case 'fire':
header("Location: http://www.example.com/redirect.php?lookingfor=fire");
break;
case 'water':
header("Location: http://www.example.com/redirect.php?lookingfor=water");
break;
default:
// go back
break;
}
////////////////////// or even - /////////////////////////////
header("Location: http://www.example.com/redirect.php?lookingfor=$result");
?>

You may want to look at trying to remove the referer part, as this is not always sent or correct...so using it may give you some funny results. Hence the switch statements and you could use the default setting to redirect people to a form where they could enter there choice.

whoisgregg

11:19 pm on Nov 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The trick is to pass the array key into the foreach loop. That way you can just set a variable for later reference.

foreach ($searchstrings as $groupname => $group) { 

Complete code:

<?php 
$referer = $_SERVER['HTTP_REFERER'];
$searchstrings = array
(
'fire' => array // $searchstrings['fire']
( 'fire', 'hot', 'lava', ),
'water' => array // $searchstrings['water']
( 'water', 'liquid', 'h20', )
);
$lookingfor = false;
$foundwith = false;
if ($referer!= '') {
foreach ($searchstrings as $groupname => $group) {
foreach ($group as $ua){
if (stristr($referer,$ua)) {
$lookingfor = $groupname;
$foundwith = $ua;
break 2;
} } } }
// Redirect
// if ($lookingfor!= false) { header("Location: http://www.example.com/redirect.php?lookingfor=".$lookingfor); }
?>