Do this with preg_match
wouldn’t be the best option. I suggest you create a list of domains in string form, convert to array and check if any value of the array is present in the REFERER with strpos()
.
Create a list of comma-separated domains:
$dominios = "dominio01.com, dominio02.com, dominio03.com";
Convert to array:
$doms_array = explode(',', $dominios);
With a foreach
you will check if any item of the array is present in the REFERER. I have also created a $flag
initial value false
. If you encounter an occurrence, you will switch to true
:
$flag = false;
foreach($doms_array as $keys){
if(strpos($_SERVER['HTTP_REFERER'], trim($keys))){
$flag = true;
break;
}
}
And the if
would look like this:
<?php if (isset($_SERVER['HTTP_REFERER']) && $flag) { ?>
COM REFERENCIA
<?php } else{ ?>
SEM REFERENCIA
<?php }?>
Complete code:
<?php
$dominios = "dominio01.com, dominio02.com, dominio03.com";
$doms_array = explode(',', $dominios);
$flag = false;
foreach($doms_array as $keys){
if(strpos($_SERVER['HTTP_REFERER'], trim($keys))){
$flag = true;
break;
}
}
?>
<?php if (isset($_SERVER['HTTP_REFERER']) && $flag) { ?>
COM REFERENCIA
<?php } else{ ?>
SEM REFERENCIA
<?php }?>
I tried doing '/01.com/', '/02.com/' but it didn’t work..
– Matheus Vitor