Yes, you can do this.
How best to track depends on what you're trying to do with the information. If you need to know the referrer for the current visit on every pageview in that visit, then setting a cookie on the first (aka referred) pageview in that visit is a good idea. That way every page can have access to the referral domain.
If you only need the info for logging purposes (e.g. to double-check billing info from a PPC ad vendor), then you don't need to store in a cookie, simply detect that you have a referrer and log that fact. It's probably easier to do this in server code rather than client script.
You didn't note which server platform you were running on, so it's hard to give specific code samples or advice, but in javascript, here's a quick code sample I had from a while ago, which creates a tracking image (aka "web bug") to record a pageview, including referral information in an optional "r=" query string parameter. Adapting this to do what you want to do shouldn't be hard.
<script type="text/javascript">
document.write("<img src=\"http://yoursite.com/logviews/?TYPE=PV"
+ ((document.referrer == "" || document.referrer == null) ? "" : "&r=" + encodeURIComponent(document.referrer))
+ "\" width=\"0\" height=\"0\" alt=\"Page view tracker\" />");
</script>