Redirect to another url depending on the referer

2

I have this code in the index.php of a domain:

    <?php           

    if (isset($_POST["redirect"])) {    

        $hash = $_POST["redirect"];

        if ($hash !== "") {     

            $origen = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "";
            $destino = "";

                 if($origen == 'http://www.example.net/test') $destino = 'https://www.test.com/ad';     
            else if($origen == 'http://www.example.net/test2') $destino = 'https://test.com/ad2';                

            if($destino != "")
            {

             echo "<script>window.location.href = '".$destino."';</script>";
            } 
        }
    }

    ?>

So far everything is correct, because if the user enters from one of the referers specified, it goes to the URL specified according to the entry referer, but if the user enters from any other referer that is not any of the mentioned ones ( or that the referer is blank) the user stays in that same URL (of this index.php) blank. How can I make the user go to a specific URL (and not stay in the current index) for all other cases?

    
asked by David Matas 18.05.2018 в 23:56
source

2 answers

1
<?php           

if (isset($_POST["redirect"])) {    

    $hash = $_POST["redirect"];

    if ($hash !== "") {     

        $origen = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "";
        $destino = "";

        if($origen == 'http://www.example.net/test') $destino = 'https://www.test.com/ad';     
        else if($origen == 'http://www.example.net/test2') $destino = 'https://test.com/ad2';  
        else{
             $destino = "otra url"
        }              

        if($destino != "")
        {

           header("Location: $destino")
        } 
    }
}

?>
    
answered by 19.05.2018 / 00:40
source
1

Just add a new else to your conditional block with the URL you'll use by default.

    ...

    if($origen == 'http://www.example.net/test') { 
        $destino = 'https://www.test.com/ad';
    } else if($origen == 'http://www.example.net/test2') { 
        $destino = 'https://test.com/ad2';                
    } else { 
        $destino = 'http://la-otra-url.com';                
    }

    ...
    
answered by 19.05.2018 в 00:49