Skip to content
PHP Logo

What are the best practices for removing 'http://' and 'www.' from the output of $_SERVER['HTTP_REFERER'] in PHP?

When retrieving the value of $_SERVER['HTTP_REFERER'] in PHP, it may include 'http://' and 'www.' which are not always needed. To remove these parts and get a clean URL, you can use PHP string manipulation functions like str_replace or preg_replace. This will ensure that the output is a clean and concise URL without unnecessary prefixes.

php
$referer = $_SERVER['HTTP_REFERER'];
$referer = str_replace(array('http://', 'https://', 'www.'), '', $referer);
echo $referer;