Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Corbin's answer will only block explict matches meaning you must input every domain/subdomain combination you want to block. This isn't a problem if you have only two domains such as "www.domain.com" &amp; "domain.com". If you have dozens, hundreds or thousands then that would be a real headache. This function is a bit more robust as it checks if the banned domain string is in any part of the domain name, it's slower too :).</p> <pre><code>$bannedDomains = array('banned.com') // blocks top level &amp; subdomains. function bannedDomain($url, $bannedDomains) { $domain = parse_url($url, PHP_HOST_URL); foreach($bannedDomains as $bannedDomain) { if (strpos($domain, $bannedDomain) !== false) { return true; } } return false; } </code></pre> <p>There is a caveat here as well, since it's blind string matching there is the off chance you could have someone do something like "www.banned.com.notbanned.com" as a domain name, since a banned domain string is within that domain, then you'd end up blocking a valid domain name. Domain matching &amp; validation is actually pretty difficult given the variety of TLDs &amp; subdomains that are out there.</p> <p>Additionally, if you're okay with explicit matches only, it's more efficient to use your domains as keys in an associative array &amp; then use isset() to detect if they are present in your banned list.</p> <pre><code>$bannedDomains = array( 'www.banned.com' =&gt; true, 'banned.com' =&gt; true ); function bannedDomain($url, $bannedDomains) { $domain = parse_url($url, PHP_HOST_URL); return isset($bannedDomains[$domain]); } </code></pre> <p>If you really need a super robust system then you're better off using a lookup inside a database or using your HTTP server's features.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload