Don't rely on HTTP_REFERER, instead using a ?next query argument.