jeudi 20 octobre 2016

Redirect if User's IP Address is in .txt file

I have tried REMOTE_ADDR and also HTTP_X_FORWARDED_FOR but im still having issues getting this to work.

Basically, if the users IP is found in the text file I want the user to be redirected to cnn.com, if no IP then redirected to google.com

Here is the code that isn't working with my IP in the file:

<?php

$iplist = @file("ip.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$iparray = in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $iplist);
//$iparray = in_array($_SERVER["REMOTE_ADDR"], $iplist);

//###if ?test= is not empty
if (!empty($_GET["test"])){
        if($iparray){
            header("Location: http://cnn.com");die;
        }
}
header("Location: http://google.com");

?>

Note: I do have ?test=1 at the end of my URL when testing

I would like to keep the format of just keeping the if statement + stopping the rest of code from executing if possible so header("Location: http://google.com"); does not execute.

Aucun commentaire:

Enregistrer un commentaire