From the PHP Docs..
Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires.
So whilst you clicked the button the second time, the actual load was in effect and you were able to see it(the cookie).