Score:0

php multiple reffering domains not working

vg flag

with single domain as reffering domain code works fine

<?php $referrer = $_SERVER['HTTP_REFERER']; if (preg_match('~site1\\.com$~', parse_url($referrer, PHP_URL_HOST)))

but when i add more sites as reffered domain nothing happens only blank page

<?php $referrer = $_SERVER['HTTP_REFERER']; if (preg_match('~site1\\.com$~'| '~site2\\.com$~' | '~site3\\.com$~', parse_url($referrer, PHP_URL_HOST)))

how can i add more sites as reffering domains

Score:1
in flag

I would use the | operator inside one and only one regex (I am not sure multiple regex using pipe are allowed in php) :

<?php  $referrer = $_SERVER['HTTP_REFERER']; if (preg_match('~(site1\.com|site2\.com|site3\.com)$~', parse_url($referrer, PHP_URL_HOST)))

additionally, I don't understand why you escape twice the dot character.

johnsow avatar
vg flag
it worked with your suggestion but when if reffering domain use www in the link, it'll not work
johnsow avatar
vg flag
it worked with your suggestion but when reffering domain contains www it not works but gives 404 error
Fabien Auréjac avatar
in flag
it's generally a bad idea (especially for search engines) to let domain be accessible with AND without www You should redirect between one to the other and adapt your code regarding what you choose.
Fabien Auréjac avatar
in flag
You can redirect from server side or dns, or if you can't either the first or second way there is php's function header("Status: 301 Moved Permanently", false, 301); and header('Location: YourDestinationLocation'); that can be used.
johnsow avatar
vg flag
you are right to move www to non www through 301, but still i am confiused is there a way to add multiple domains into this <?php $referrer = $_SERVER['HTTP_REFERER']; if (preg_match('~site1\\.com$~', parse_url($referrer, PHP_URL_HOST))) because with single domain with or without www works fine only wana add more domains
johnsow avatar
vg flag
there is also another alternative solution <?php if(strstr($_SERVER['HTTP_REFERER'],'example.com')) { echo '<div id="main">Title</div>'; } ?> this also works fine but can you add more domains to this code? <?php if(strstr($_SERVER['HTTP_REFERER'],'example.com'| 'example2.com')) { echo '<div id="main">Title</div>'; } ?>
Fabien Auréjac avatar
in flag
I don't think either strstr or preg_match were made to accept pipes and several strings in php. The solution I showed you with | inside one and only one regex should be satisfying for every list of domains, including www or not.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.