I'm a little late here, and you have probably figured this out already, but the reason php only detects your javascript-set cookie after you refresh the page is that php has already process by the time you are looking at you web page, which is where and when javascript happens. So, by the time your javascript can set a cookie, it is too late for php to do anything until you reload the page.
Server side processing like php, asp, cold fusion, perl, etc. always do their work before they serve you your html. Javascript operates only on what php has already processed.