If there is a ref id # in the session, record it in an invisible field on the form.
Looks like that isn't done, but why do you need it?????
You are concentrating on the referrer, why? he is not on your site nor is he doing anything on your site at present. It's the current user you need to track. Especially if you have things she can or should do on the site.
When the new user fills out the form he enters or does not enter a referrer's number. You should write all form info to the file immediately, including the referrer number. That's all you need.
P.S. This is just for your information:
Learn how PHP talks to MySQL (a database) or one of your choosing. It's simple and you can get info from it much easier then a cvs file.