• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • 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
 
b0lsc0ttIT ManagerCommented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
b0lsc0ttIT ManagerCommented:
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
 
b0lsc0ttIT ManagerCommented:
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
 
b0lsc0ttIT ManagerCommented:
I'm glad I could help.  Thanks for the fun question, the grade and the points.

bol
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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