I don't think you need a coockie to do this.
what i think you can do is ,
catch the ref number in a variable ($ref) then
<INPUT TYPE='TEXT' NAME='ref' VALUE='<?php echo"$ref"; ?>'></TD>
This way it will allways be the correct number.
( i'm a newb myself but i think this could work)