In PHP, look at $_Server['HTTP_Referrer']. i.e.
PHP Code:
<?php echo $_Server['HTTP_Referrer']; ?> is for sale
Don't rely on this being a simple URL. Some software blocks referrers, search querys will probably include the search URL and string, so do some checking to see if it is what you're expecting, and either fix it or have some default code if it is not.
Referrers can also be spoofed, so you should also
clean up the string to remove anything malicious before using it.