...and you are generally recommended not to do that sort of thing.
Moodle has a library function...
/**
* Returns the URL of the HTTP_REFERER, less the querystring portion if required.
*
* @param boolean $stripquery if true, also removes the query part of the url.
* @return string The resulting referer or empty string.
*/
function get_referer($stripquery=true) {