How to put + of a domain in this code, different references

3

Hello, I have a code that works as a reference.

In other words, if the visit comes from domain01.com, it displays: WITH REFERENCE if it does not display: NO REFERENCE

Code:

<?php if (isset($_SERVER['HTTP_REFERER']) && preg_match('/dominio01.com/', $_SERVER['HTTP_REFERER'])) { ?>
 COM REFERENCIA
<?php } else{ ?>
  SEM REFERENCIA
<?php }?>

But how to put more than one domain?

Example, when references are from domain01.com or domain02.com or domain03.com or domain03.com, display: WITH REFERENCE , when you do not have any of these references display: NO REFERENCE ?

    
asked by anonymous 16.06.2018 / 22:30

2 answers

2

Doing this with preg_match would not be the best option. I suggest you create a list of domains in the form of a string, convert it to array, and check if any array value is present in REFERER with strpos() .

Creates a list of domains separated by commas:

$dominios = "dominio01.com, dominio02.com, dominio03.com";

Convert to array:

$doms_array = explode(',', $dominios);

With foreach you will check if any items in the array are present in REFERER. I also created a $flag with initial value false . If it finds any occurrences, it will change to true :

$flag = false;
foreach($doms_array as $keys){
   if(strpos($_SERVER['HTTP_REFERER'], trim($keys))){
      $flag = true;
      break;
   }
}

And if would look like this:

<?php if (isset($_SERVER['HTTP_REFERER']) && $flag) { ?>
 COM REFERENCIA
<?php } else{ ?>
  SEM REFERENCIA
<?php }?>

Full 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 }?>
    
17.06.2018 / 00:14
0

I think you have a problem with @dvd and @Buéda Bonita's answer because:

  • https://dominio01.com
  • https://dominio01.com.meusite.com
  • https://meusite.dominio01.com

All of these are valid for dominio01.com . That is, a subdomain of my site will be validated as a reference. If I have aaa.com ownership I can create dominio01.com.aaa.com . Even not being dominio01.com will be considered dominio01.com , due to strpos . This ignoring the fact that Referer is manipulated in other ways, like any HTTP header

I believe this should be replaced by === , so it should be equal to the domain, invalidating the "false" subdomain. In this case the in_array will do the job.

$autorizados = ['dominio01.com', 'dominio02.com', 'dominio03.com'];
$atual = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);

$isReferencia = in_array($atual, $autorizados, true);

Then to show:

<?php if($isReferencia){ ?>
 COM REFERENCIA
<?php } else{ ?>
  SEM REFERENCIA
<?php }?>
  

PS: If not, how confident is parse_url .

    
17.06.2018 / 04:30