Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6826975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:12:42+00:00 2026-05-26T22:12:42+00:00

When the following code is run for the first time, it does not echo

  • 0

When the following code is run for the first time, it does not echo out the “var1” value but it does on browser refresh (f5), Why? As i understand when the browser sends the code to the server, setcookie() stores the cookie variable (“var1”) to a client (browser) in a local file and puts the “var1” value available in global domain via $_COOKIE superglobal.

Since “var1” value is available immediately in $_COOKIE after the first server replies to browser’s initial request, then why the “var1” is not echoed out. Is it that setcookie() stores “var1” value in client’s browser on first request and only when the page is refreshed (2nd request) the browser sends back “var1” value to the server and then the server makes it available in the global domain via $_COOKIE function.


CODE

<?php
  setcookie("var1","5");
  echo $_COOKIE['var1'];
?>

Kindly clear this for me.

Thanks
djain.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:12:42+00:00Added an answer on May 26, 2026 at 10:12 pm

    It’s all explained in the manual. setcookie() causes a Set-Cookie: response header to be returned to the browser. The $_COOKIE array can only be filled with the next HTTP refresh when the browser had the opportunity to reply back with the Cookie: request header.

    When the following code is run for the first time, it does not echo out the “var1” value but it does on browser refresh (f5), Why?

    The browser needs to send the cookie back. On the first request it doesn’t know about that cookie yet. After the refresh it does. Only then it can send it.

    As i understand when the browser sends the code to the server,

    On the second request.

    setcookie() stores the cookie variable (“var1”) to a client (browser) in a local file

    The browser saves it.

    and puts the “var1” value available in global domain via $_COOKIE superglobal.

    Not immediately.

    Since “var1” value is available immediately in $_COOKIE after the first server replies to browser’s initial request, then why the “var1” is not echoed out.

    It is not immediately in $_COOKIE. It can’t be. That array is only populated once, when PHP starts.

    Is it that setcookie() stores “var1” value in client’s browser on first request and only when the page is refreshed (2nd request) the browser sends back “var1” value to the server and then the server makes it available in the global domain via $_COOKIE function.

    Yes. That’s how it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code does not run as rootNode is null when retrieved by name
When I run the following code, it only seems to be downloading the first
The following code will not run correctly in IE7 with the latest service packs
Good day, My code generates a run-time error and I'm not sure how to
Make a new AS3 Document in Flash, paste in the following code and run
The following code fails to run on Python 2.5.4: from matplotlib import pylab as
Let's say that I have the following code that's run in one thread of
When I run the following code to test copying a directory, I get a
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
When I run the following code: private void button1_Click(object sender, EventArgs e) { Bitmap

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.