cookie values are only available after a refresh, its a "cookie thing", you can check that in php tuturials ("Common Pitfalls:
Cookies will not become visible until the next loading of a page that the cookie should be visible for.
Cookies must be deleted with the same parameters as they were set with. " - extracted from php tutorial........).
Try use sessions.
Good luck, good code.