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

  • SEARCH
  • Home
  • 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 1029327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:31:39+00:00 2026-05-16T12:31:39+00:00

I don’t remember having many problems using Cookies in the past but I was

  • 0

I don’t remember having many problems using Cookies in the past but I was playing around and encountered some unexpected results.

(i’m running on localhost, hence my domain setting)

<?php
$sessionCookie = rand();
setcookie("crav_auto_login_cookie", $sessionCookie, false,"/crav/", false);
echo "Cookie Set<br/>";
echo "Cookie equals: ".$_COOKIE["crav_auto_login_cookie"]."<br/>";
echo "should equal: ". $sessionCookie;
?>

This will yield in the following output:

Cookie Set
Cookie equals: 457718770
should equal: 318511886

I am obviously missing something, but not sure why the values are different. Is it because cookies are loaded on page call, and $_COOKIE["crav_auto_login_cookie"] is returning the results from the instance called when the page is opened and not reflecting the newly set value? If that is the case, then why?

  • 1 1 Answer
  • 1 View
  • 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-16T12:31:39+00:00Added an answer on May 16, 2026 at 12:31 pm

    setcookie sets up headers to send back to the client in order to set a cookie. The new cookie won’t be accessible in the $_COOKIE array (which contains cookies sent from the client) until the next request.

    Here is a simplified progression of events when a user accesses your page:

    1. User’s browser sends a request to your server. This request contains headers, including what cookies are set for that user for your domain. PHP fills the $_COOKIE (as well as $_GET, $_POST, etc.) array based on the data in this request.
    2. The server parses the user’s request, and sets up a response. This response begins with response headers (including any headers you set yourself through header, and also headers for cookies set through setcookie). All headers must precede any page output (as you may have encountered, PHP will give you an error if you try to send more headers after you’ve begun outputting page content).
    3. The server sends the page content (in reality, the headers and content are part of the same transmission).
    4. The connection between the server and client closes (let’s ignore AJAX for the purposes of this discussion).
    5. The client parses the headers and content it received, sets cookies as necessary, and renders the page (in reality, this could very well happen in serial with receiving the page).

    So by the time the set-cookie header is received and processed by the client, the client is already done communicating with the server, which is why the cookie won’t appear to PHP until the next request.

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

Sidebar

Related Questions

Don't ask how I got there, but I was playing around with some masking,
Don't know why this is happening, but after submitting a form via JS (using
Don't ask why but I have the requirement to draw a border around certain
Don't know whether I'm having a "thick day" - but I just wondered what
Don't know if this is possible, but I have some code like this: val
don't know if the title describes anything about what I'm trying to say but
Don't ask me how but I'm in a situation where I have DCPs published
don't know what happened in my jcrop selection. I think I pressed some keys
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query

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.