[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

Problem reading cookie after set on same page.

I have a include page that sets cookie on my main page but when I load my main page the input field does not fill until you refresh for the second time how do I fix this?
0
lhaluska
Asked:
lhaluska
  • 4
  • 2
1 Solution
 
b0lsc0ttCommented:
lhaluska,

If I understand you correctly you can't.  The cookie isn't available to PHP until the browser has gone back to the server. That is why it works with the refresh.  You should change the PHP script so when it sets the cookie or reads the cookie a variable is set.  Use that variable to set the field instead of trying to read the cookie.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
lhaluskaAuthor Commented:
Can I read the variable and create the cookie?
0
 
lhaluskaAuthor Commented:
Sorry the cookie is writen to my local machine when you first open the page, but the page will not pick it up until I refresh the page for a second time.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
b0lsc0ttCommented:
You can't create the cookie and then read it in the same script.  That is just part of how PHP handles cookies.  Your statement in http:#20097041 is true, as I mentioned here and earlier, and there is no way around it.  You have to use something besides the cookie.  Since you are setting the cookie you certainly have the info in the script anyways.  Just use or set a variable instead, like I suggested above.  This method should work whether it is the first visit (and cookie is set) or a future visit (when cookie is read).

bol
0
 
b0lsc0ttCommented:
By the way, to clarify, when you set the cookie PHP actually just asks the browser to do that when the page goes back.  The cookie isn't actually there (in the browser's header) until the page comes back to the PHP server.  That is why the code that sets the PHP cookie can't read it.

Let me know if the reason for this isn't clear.  I can try to clarify or refer you to some good explanations on the web.

bol
0
 
b0lsc0ttCommented:
I'm glad I could help.  Thanks for the fun question, the grade and the points.

bol
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now