Ahh cracked it. The problem (for once) wasnt my code. Or not this piece anyway. See this piece of code appears on the login page. Preceding it is the code that sets the cookies, and says that config.php should be included. config.php says that the var
$mysite_username = $HTTP_COOKIE_VARS["mysite_username"];
For some unknown reason, this page, and this page alone cant seem to recognise $mysite_username. So when I define $mysite_username as $_POST[username] then it works. Odd... but it works.
Thank you for all of your help anyway.