How to create a redirection file with different destinations in PHP?

0

I mean, I use advertising on my website. but I have 2 different providers for each advertisement, so what I want is that when a user clicks on the barner, it takes it to a PHP file that contains the 2 destinations, if the user clicks the first time, it takes it to the First, if you do it for a second time, take it to the second time, if you do it for a third time, then start again with the first time. let it be a cycle. or failing that, that is for seconds, that is to say that the PHP, has a sequence that every so many seconds, changes between 2 different destinations. I have this until now:

            if(){ 
                header ('Location: http://www.misitio1.com'); 

            }else{ 

                header ('Location: http://www.misitio2.com'); 
            }
    
asked by Luis Cesar 08.06.2018 в 18:58
source

2 answers

1

As I explained to you in the comment, for example you can check if the second or minute is even or odd, or use any other formula with pseudo-random numbers for example.

Examples:

You can choose a variable number in one of the following ways:

// minutos
$numero = date('i');
// segundos
$numero = date('s');
// numeros pseudo-aleatorios
$numero = mt_rand();

Then you simply check if it's even or odd

if($numero%2==0){
    // PAR
    header ('Location: http://www.misitio1.com'); 
}else{ 
    // IMPAR
    header ('Location: http://www.misitio2.com'); 
}

Following the same formula we can create a range of seconds, from 0 to 30 to a site, from 31 to 60, example:

if(date('s') <= 30){
    // segundo menor o igual a 30
    header ('Location: http://www.misitio1.com'); 
}else{ 
    // segundo mayor a 30
    header ('Location: http://www.misitio2.com'); 
}
    
answered by 11.06.2018 / 22:44
source
1

Following the idea of Xerif, only that it would do it with a counter stored in the session, since over time the redirection can be repeated.

session_start();

if (!isset($_SESSION['clicks'])) { 
    $_SESSION['clicks'] = 0;
}
$clicks = $_SESSION['clicks'];

$_SESSION['clicks'] = $clicks + 1;

if ($clicks % 2 == 0) {
    header ('Location: http://www.misitio1.com'); 
}else{
     header ('Location: http://www.misitio2.com');  
}
    
answered by 11.06.2018 в 23:08