Curl cookie handling

后端 未结 2 848
野的像风
野的像风 2021-01-16 03:20

Is that possible that with cURL not every user use the same cookie?

Because it\'s cool that I store the cookie that I get, but this cookie will be used by everybody,

相关标签:
2条回答
  • 2021-01-16 03:37

    Your question is unclear, do you want all user to use the same cookie or not ? What is an user in your case, a visitor on your website ?

    In any case, you can set which file curl will use to save/load its cookies using curl_setopt and the CURLOPT_COOKIE* constants.

    0 讨论(0)
  • 2021-01-16 03:55

    Here's a really basic overview of how cookies work

    1. Client (browser) makes request

    2. Server sees request and asks "hey, did this client send me a cookie?"

    3. Server doesn't see a cookie, so it does some stuff, and then sends back a response, with a cookie

    4. Client (browser) sees the response and says "hey look, a cookie for me, I better save this"

    5. The next time the client makes a request to that same server, it sends along that same cookie

    6. Server sees the request and asks "hey, did this client send me a cookie?"

    7. Server sees the cookie this time, and does some different stuff because of what's in the cookie, and then sends back a response, with a cookie

    8. Client (browser) sees the response and says "hey look, a cookie for me, lets update the one I have"

    It sounds like the problem you're running into is you have multiple curl requests running from the same machine, but you want each one to use a different cookie file.

    You should be able to achieve this by using the following two curl options

    CURLOPT_COOKIEJAR   //tells curl which file to save the cookie from the server in
    CURLOPT_COOKIEFILE  //tells curl which file to look in and send as the request cookie
    

    If you setup a system so that each different curl request is setting a different path value for these two options, you should be set.

    0 讨论(0)
提交回复
热议问题