...

View Full Version : newbie help if/else statement, maybe?



akmagnolia
01-09-2010, 09:02 PM
I have tried many variations on this and even tried a switch instead of the if/elseif/else, somehow, only the first header(location:url) is read no matter what I try. I have used if{} if{} if{}, if{}elseif{}else{}, if{if{}else{}}else{}, and anything else I could think of! I'm new to PHP, but have been programming for years.

What's up??


if (is_null($_SERVER["HTTP_REFERER"]) or !array_key_exists("HTTP_REFERER",$_SERVER)) {
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
} else {
if ($_SERVER["HTTP_REFERER"]="http://www.naturalchoicemarket.com") {
header("Location: http://www.moontimefarms.com/ncm/index.php");
} else {
if ($_SERVER["HTTP_REFERER"]="http://www.blueoats.com") {
header("Location: http://www.moontimefarms.com/bo/test.php");
} else {
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
}
}
}

Thanks for the help!
Nicki

JAY6390
01-09-2010, 09:09 PM
You should echo out the referrer so that you know exactly what it is that you are trying to match aginst. do this BEFORE the If statement

Also I would agree that you would be better off using a switch for this than if elses

akmagnolia
01-09-2010, 09:16 PM
I changed to this and now the variable doesn't seem to be read at all. How do I "echo out the referrer"?


if (array_key_exists("HTTP_REFERER",$_SERVER)) {
switch($_SERVER["HTTP_REFERER"])
{
case "http://www.naturalchoicemarket.com":
header("Location: http://www.moontimefarms.com/ncm/index.php");
break;
case "http://www.blueoats.com":
header("Location: http://www.moontimefarms.com/bo/test.php");
break;
case "":
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
break;
}
} else {
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
}

tomws
01-10-2010, 12:18 AM
How do I "echo out the referrer"?


echo $_SERVER["HTTP_REFERER"];

akmagnolia
01-10-2010, 02:03 AM
thanks,
as it is now, it still isn't working. here's the code

if (array_key_exists("HTTP_REFERER",$_SERVER)) {
echo $_SERVER["HTTP_REFERER"];
if ($_SERVER["HTTP_REFERER"]="http://www.naturalchoicemarket.com") {
header("Location: http://www.moontimefarms.com/ncm/index.php");
} elseif ($_SERVER["HTTP_REFERER"]="http://www.blueoats.com") {
header("Location: http://www.moontimefarms.com/bo/test.php");
} else {
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
}
} else {
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
}

:confused: so stuck

tomws
01-10-2010, 02:26 AM
if ($_SERVER["HTTP_REFERER"]="http://www.naturalchoicemarket.com") {
Oops. A single "=" is an assignment. A test for equality is two.

PHP comparison operators (http://www.php.net/manual/en/language.operators.comparison.php)

akmagnolia
01-13-2010, 01:12 AM
OK, so I changed the = to ==. It appears to be reading the variable since it does output to the screen, but there is still something wrong with the redirect part.


if (array_key_exists("HTTP_REFERER",$_SERVER)) {
echo $_SERVER["HTTP_REFERER"];
$my_place=$_SERVER["HTTP_REFERER"];
echo $my_place;
if ($my_place=="http://www.naturalchoicemarket.com") {
header("Location: http://www.moontimefarms.com/ncm/index.php");
} elseif ($my_place=="http://www.blueoats.com") {
header("Location: http://www.moontimefarms.com/bo/test.php");
} else {
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
}
} else {
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
}

The situation is that I have 3 domains I want to share a hosting account. The 2 domains I want to redirect are "forwarded with masking" to the main domain (perhaps this is the problem?) so the HTTP_REFERER variable should always exist for them.

so completely frustrated - seems like this should be much simpler!
:confused:

tomws
01-13-2010, 02:51 AM
I'm really not sure what to tell you there. The only webhost I use handles the redirection and masking stuff through the web panel, but I don't use any of it. I don't have any idea what happens to the referer variables or anything in those cases.

oesxyl
01-13-2010, 07:51 AM
try this:


$my_place = $_SERVER["HTTP_REFERER"];
if($my_place == "http://www.naturalchoicemarket.com"){
header("Location: http://www.moontimefarms.com/ncm/index.php");
}elseif($my_place == "http://www.blueoats.com"){
header("Location: http://www.moontimefarms.com/bo/test.php");
}else{
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
}

don't echo anything before. I would prefere to doit with mod_rewrite instead of this.

best regards

akmagnolia
01-13-2010, 10:18 PM
thanks.
the solution I found uses a string function and somehow this works...not sure why


if (array_key_exists("HTTP_REFERER",$_SERVER)) {
$my_place=$_SERVER["HTTP_REFERER"];
$url=substr_count($my_place,"naturalchoicemarket");
$url2=substr_count($my_place,"blueoats");
if ($url>0) {
header("Location: http://www.moontimefarms.com/ncm/index.php");
} elseif ($url2>0) {
header("Location: http://www.moontimefarms.com/bo/index.html");
} else {
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
}
} else {
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
}

oesxyl
01-13-2010, 10:39 PM
thanks.
the solution I found uses a string function and somehow this works...not sure why


if (array_key_exists("HTTP_REFERER",$_SERVER)) {
$my_place=$_SERVER["HTTP_REFERER"];
$url=substr_count($my_place,"naturalchoicemarket");
$url2=substr_count($my_place,"blueoats");
if ($url>0) {
header("Location: http://www.moontimefarms.com/ncm/index.php");
} elseif ($url2>0) {
header("Location: http://www.moontimefarms.com/bo/index.html");
} else {
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
}
} else {
include ('includes/inc_header.php');
var_dump($_SERVER["HTTP_REFERER"]);
}

array_key_exists("HTTP_REFERER",$_SERVER) is always true you don't need to check it and second else is useless..
about comperison using substr_count, will return how many times the second string is find inside the first and will work without problem.

http://www.php.net/manual/en/function.substr-count.php

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum