I have $_SERVER['HTTP_REFERER'], pretend it is http://example.com/i/like/turtles.html .
What would i need to do to get just the "http://example.com" part out of the string and set into its own variable?
I have $_SERVER['HTTP_REFERER'], pretend it is http://example.com/i/like/turtles.html .
What would i need to do to get just the "http://example.com" part out of the string and set into its own variable?
I'd use parse_url in the following way...
if ($urlParts = parse_url($myURI))
$baseUrl = $urlParts["scheme"] . "://" . $urlParts["host"];
You could use a regular expression:
if (isset($_SERVER['HTTP_REFERER']) && preg_match('@^[^/]+://[^/]+@', $_SERVER['HTTP_REFERER'], $match)) {
var_dump($match[0]);
}
Or you could use the parse_url
function.
You should be able to use the parse_url function to achieve that
print get_domain("http://www.somesite.com/somepage/id/1");
/* outputs: somesite.com */
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
As a sidenote, just a remark : the Referer is not always sent by the client (it can be disabled, for instance), and it can be faked.
So, don't base any critical functionnality (nor security-oriented) on it !