0

Ok I guess this question may be similar to other in the "remote cookies" kind, but I'm not sure that other answers I've read are applied to my case anyway, so here we go.

I have two applications, a client and a server. The server "has" (I know they're actually stored client-side) a cookie and a page which uses it to print out a computed data based on the cookie.

If I access the server page directly, the cookie is taken into account and the data is output correctly.

If I call the same server page from the client via a file_get_contents() the cookie on the server page doesn't get read, and I get an answer computed with an empty cookie.

How to make the server read its own cookies when answering a similar request? Is cURL the only option?

Frhay
  • 424
  • 2
  • 16

1 Answers1

1

You need to:

  1. Make a request that gets a Set-Cookie header in the response (assuming the cookies are HTTP cookies and not JS cookies)
  2. Store the cookies
  3. Include the cookies in the HTTP request to the page that displays them

cURL is probably the sanest way to go about dealing with being an HTTP client in PHP when you need to pay attention to the headers. Another question gives some guidance about how to go about doing that.

Note that there is no way to send the cookies that the browser accessing your PHP script would sent to the remote server. They are a secret that belong to the browser and that server and will not be shared with your server.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • For example in Ajax calls the server uses its cookies correctly, you are saying that with a cURL call instead of a file_get_content I can get the same behaviour? – Frhay Apr 30 '13 at 14:43
  • BTW I edited the question so the problem is clearer, because the cookie is not the wanted data for the answer, but it's used to compute it... – Frhay Apr 30 '13 at 14:58
  • Given *Server A*, *Server B* and *Browser X*. If X gets a cookie from A, then X talks to B, then B uses file_get_contents, or cURL, or anything else to talk to A then there is no way for that request to include the cookie that A gave to B. (You'd need to look into passing a token through some non-cookie based mechanism instead). – Quentin Apr 30 '13 at 15:14
  • So jQuery Ajax calls are passing automatically browser cookies to the server, right? And I should do something like that by myself as you were pointing in the link you provided in your answer? – Frhay Apr 30 '13 at 15:37
  • Ajax involves asking the browser to make a request. Cookies are automatically included in all requests from browsers. – Quentin Apr 30 '13 at 15:39
  • Assuming that the initial request to the server setting the cookies was made by the browser - you can't. You don't know what data is in the cookie sent to the browser. If all requests to the server that sets the cookies are coming from your server, then you can. – Quentin Apr 30 '13 at 15:40