Dunno about the first one, but for the referer one, try
$URL = parse_url($_SERVER["HTTP_REFERER"]);
$domainName = $URL["host"];
Haven't tried it, but it should work for you.
Also, just a word to the wise, unless you are using PHP3, it's generally suggested to use $_SERVER instead of $HTTP_SERVER_VARS. Same goes for $HTTP_POST_VARS, $HTTP_GET_VARS, and others.
Go here for more information: http://us4.php.net/reserved.variables