Your code works fine as it is written but maybe this is not what you are trying to accomplish? As it is now the first time a user visits your site it sets a cookie. Now every subsequent time that user returns to the site the cookie value will stay the same and not change. Is this what you want? The cookie value will only expire 4 hours after being originally set. Not sure what you mean when you say it does not work. Be a little more specific on what you want this script to do.