Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes browser need to be refreshed to see new cookie? Confusing issue I've noticed
    primarykey
    data
    text
    <p>I am working through the Udacity course on web app development using Python and Google App Engine. During a lesson on setting cookies (to count visitor count) and hashing I noticed something that is confusing me:</p> <p>For fun I decided to print out the cookie value of 'visits' before and after adding 1 more visit. Here is the output: </p> <pre><code>5|e4da3b7fbbce2345d7772b0674a318d5 [[[this is cookie before adding 1 to it]] You've been here 6 times! [[this is printed after adding 1 to cookie, but is not the cookie]] 5|e4da3b7fbbce2345d7772b0674a318d5 [[this is printing the cookie after one has been set using "Set-Cookie"]] </code></pre> <p>The 6 in the middle line is correct. Using a cookie viewer I have verified that it matches the cookie. So the "5" in the first line is also correct (because this line is reading the cookie before 1 was added).</p> <p>What is confusing me is that I also print out the cookie value AFTER adding 1, and it's STILL prints "5" -- even though the cookie has already been reset to 6.</p> <p>Why is that? Does the browser need to be refreshed before the new cookie is read correctly or something? </p> <p>Here is the code in question: class CookiePage(BlogHandler): def get(self): self.response.headers['Content-Type'] = 'text/plain' visits = 0 visits_cookie_str = self.request.cookies.get('visits') self.write(visits_cookie_str) # this is the first line printed self.write("\n")</p> <pre><code> if visits_cookie_str: cookie_val = check_secure_val(visits_cookie_str) visits = int(cookie_val) visits += 1 new_cookie_val = make_secure_val(str(visits)) self.response.headers.add_header('Set-Cookie', 'visits=%s' % new_cookie_val) self.write("You've been here %s times!\n" % visits) # this is the 2nd line printed cookie = self.request.cookies.get('visits') self.write(cookie) # this is the 3rd line printed which SHOULD match the one above it but doesn't </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload