check the IP input with IP array

0

I want to check the IP input if it is inside any of the 3 or 4 array list I put the message, I had it like that but it does not work.

$laptop=(1.1.1.1,2.2.2.2,3.3.3.3)
$pcescritorio=(1.1.1.2,2.2.2.3,3.3.3.4)
switch ($_SERVER['REMOTE_ADDR']) {
case "laptop":
    readfile("laptop.pdf");
    break;
case "pcescritorio":
    readfile("work.pdf");
    break;
default:
    echo "No Encontrado";
}

The idea is that between the check the IP with the IP list above and send you automatically the type of file according to the type of PC you have, the IP may vary and be different that I will put them ponerselos , but I can not get it to work for me

    
asked by Pedro Rafael Santiesteban 13.05.2018 в 22:52
source

1 answer

0

If I understood correctly, you would have two arrays with IP addresses, and you want to verify if the IP obtained with $_SERVER['REMOTE_ADDR'] is in one of those two arrays.

You can try it like this:

/*Creamos dos arrays correctos con las IP*/
$laptop=array("1.1.1.1","2.2.2.2","3.3.3.3");
$pcescritorio=array("1.1.1.2","2.2.2.3","3.3.3.4");

/*Una variable para la IP actual, así el código queda más claro*/
$ipActual=$_SERVER['REMOTE_ADDR'];    

/*
  *Nos vamos a valer de dos operadores ternarios    
  *para determinar si $ipActual está en alguno de los arrays
  *cuando se cumpla alguno de ellos llamaremos a readfile con el nombre
  *si ninguno se cumple, imprimiremos el mensaje 
  *That's all!
 */

in_array($ipActual, $laptop) ? readfile("laptop.pdf") : 
    (
        in_array($ipActual, $pcescritorio) ? readfile("work.pdf"): print ("No encontrado")
    );

This option makes use of the Ternary Operators, to simplify the code, if it becomes illegible or difficult, I can propose a more classic solution too.

    
answered by 14.05.2018 в 00:21