Ive realised where ive gone wrong, but its given me another problem.
I am redirecting after i am creating a cookie. Since you can only write to a cookie once, does anyone know how i can write to a cookie and then immediately afterwards redirect to another page?