mercredi 28 mars 2018

php IF / ELSEIF inside IF / ELSEIF

Hi there I have the following php that I have been developing

<?php

$URI = $_SERVER['REQUEST_URI'];
$activepage = $_SERVER['SERVER_NAME'];
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

if (
    strpos($_SERVER["HTTP_USER_AGENT"], "facebookexternalhit/") !== false ||          
    strpos($_SERVER["HTTP_USER_AGENT"], "Facebot") !== false
) {
    // it is probably Facebook's bot
    if ($activepage=="www.example.co.uk") {
    $link = 'http://www.example.co.uk' . $URI;
    header("location:$link");
    exit;
    }
    elseif ($activepage=="test.example.co.uk") {
    $link = 'http://test.example.co.uk' . $URI;
    header("location:$link");
    exit;
    }
    elseif ($activepage=="www.example.us") {
    $link = 'http://www.example.us' . $URI;
    header("location:$link");
    exit;
    }

}
elseif {
    // that is not Facebook
    if ($activepage=="test.example.co.uk" & $country_code=="CA") {
    $link = 'http://www.example.us' . $URI;
    header("location:$link");
    exit;
    }
    elseif ($activepage=="test.example.co.uk" & $country_code=="US") {
    $link = 'http://www.example.us' . $URI;
    header("location:$link");
    exit;
    }
    elseif ($activepage=="test.example.us" & $country_code=="GB") {
    $link = 'http://www.example.co.uk' . $URI;
    header("location:$link");
    exit;
    }
    elseif ($activepage=="test.example.us" & $country_code=="UK") {
    $link = 'http://www.example.co.uk' . $URI;
    header("location:$link");
    exit;
    }

}



?>

the second if / elseif works fine which by itself looks like this

<?php

$URI = $_SERVER['REQUEST_URI'];
$activepage = $_SERVER['SERVER_NAME'];
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

if ($activepage=="www.example.co.uk" & $country_code=="CA") {
$link = 'http://www.example.us' . $URI;
header("location:$link");
exit;
}
elseif ($activepage=="www.example.co.uk" & $country_code=="US") {
$link = 'http://www.example.us' . $URI;
header("location:$link");
exit;
}
elseif ($activepage=="www.example.us" & $country_code=="GB") {
$link = 'http://www.example.co.uk' . $URI;
header("location:$link");
exit;
}
elseif ($activepage=="www.example.us" & $country_code=="UK") {
$link = 'http://www.example.co.uk' . $URI;
header("location:$link");
exit;
}

?>

However when I pop it inside this

if (
    strpos($_SERVER["HTTP_USER_AGENT"], "facebookexternalhit/") !== false ||          
    strpos($_SERVER["HTTP_USER_AGENT"], "Facebot") !== false
) {
    // it is probably Facebook's bot
}
else {
    // that is not Facebook
}

it stops working all together.

what I want to do is have it so facebook bot goes to the site which the link is used however failing this if it is a normal user, then if a user from US hits the UK site they're redirected and if a user from the UK gets the US site they get redirected - using cloudflares HTTP headers.

Any thoughts on where I might be going wrong here?

Thanks

Aucun commentaire:

Enregistrer un commentaire