Firstly HTTP_REFERER probably isnt a reliable way doing it.
Still...
Basically, you want to check if HTTP_REFERER is of the form:
http://www.domain.com/page.html
or
http://www.subdomain.domain.com/page.html
and if domain.com matches, it is expected.
What you can do is this:
function matchDomain($domain, $url) {
//get an array containing the parts of the parsed URL
$url_array = parse_url($url);
$host = $url_array['host'];
//periods are special chars in regex, so we escape them
$domain = str_replace('.', '\.', $domain);
//compare $host with the domain name provided
if (preg_match('/' . $domain . '$/i', $host) == 1)
return true;
else
return false;
}
Then you would use:
<?php
//check that HTTP_REFERER is set
if (isset($_SERVER['HTTP_REFERER'])) {
if (matchDomain("site.com", $_SERVER['HTTP_REFERER'])) {
//ok
}
else {
//incorrect
}
}
else {
//incorrect
}