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

Asked

Viewed 66 times

3

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

That is if the visit comes from domain 01.com, it displays: WITH REFERENCE if it does not display: UNREFERENCED

Code:

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

But how to place more than one domain ?

Example, when the references are domain 01.com or domain 02.com or domain 03.com or domain 03.com, display: WITH REFERENCE, when you don’t have any of these references display: UNREFERENCED ?

  • I tried doing '/01.com/', '/02.com/' but it didn’t work..

2 answers

2


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 }?>

0

I believe you have a problem with the @dvd and @Eduardo Bona response, because:

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

All these are valid for dominio01.com. That is, a subdomain of my website will be validated as a reference. If I have possession of aaa.com I can create dominio01.com.aaa.com. Even though it’s not the dominio01.com I will be considered the dominio01.com, due to strpos. This ignoring the fact of Referer be manipulated in other ways, such as any HTTP header


I believe this should be replaced by ===, thus 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: Not if not how reliable is the parse_url.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.