not writing the refid # to the csv file?
I never did that.
Add this line in form under the first hidden input:
<input type="hidden" id="refid" name="refid" value="<?php echo $value; ?>">
At the top of the code add
right after the line
$_SESSION["rewards"] = '';
Should do it.