Reputation: 12512
I need to execute a PHP script only is a user is redirected from another page that starts with specific name. For example: domain.com/abcXXXXXX
I've tried this but it doesn't seem to work:
if (preg_match("/abc", $_SERVER['HTTP_REFERER'])) {
}
What am I missing?
Upvotes: 1
Views: 76
Reputation: 6864
I would suggest using parse_url in conjunction with a look up table of URLs.
You can do the following:
$allowedReferals = [
'www.google.com/maps',
'www.google.co.uk/maps',
'www.google.in/maps',
];
$referer = !isset($_SERVER['HTTP_REFERER']) ? null : parse_url($_SERVER['HTTP_REFERER']);
if (!is_null($referer)) {
$host = !isset($referer['host']) ? null : $referer['host'];
$path = !isset($referer['path']) ? null : $referer['path'];
$referingDomain = $host . $path;
if (in_array($referingDomain, $allowedReferals)) {
// The referer matches one of the allowed referers in the lookup table
// Do something...
}
if (preg_match('/^maps/', $path)) {
// The referer's path begins with maps
// Do something...
}
}
Upvotes: 1
Reputation: 2464
Fix the regex pattern like this:
if (preg_match("/^domain\.com\/abc/", $_SERVER['HTTP_REFERER'])) {
}
Another version to check with/without www:
if (preg_match("/[w{3}\.]?domain\.com\/abc/",'www.domain.com/abcXXXXXX')) {
}
Upvotes: 0