简体   繁体   English

PHP和cURL cookie

[英]PHP and cURL cookies

I am trying to setup a php page which uses cURL to hit a third party server to log them in. Right now my code does successfully log in to the third party server, but since cURL is not sending the SESSION cookies to the user, the user must login in themselves 我正在尝试建立一个使用cURL击中第三方服务器以登录他们的php页面。现在我的代码确实成功登录了第三方服务器,但是由于cURL没有将SESSION cookie发送给用户,因此用户必须自己登录

How can I get cURL to forward cookies to the client. 如何获取cURL将cookie转发到客户端。 Thanks 谢谢

Check out the documentation for the following cURL options: 请查看文档 ,了解以下cURL选项:

CURLOPT_COOKIE
CURLOPT_COOKIEFILE
CURLOPT_COOKIEJAR

EDIT 编辑

After reading your question more thoroughly, I'm afraid to tell you there is NO way to write the cookies to the client browser unless you redirect the client to the site you are attempting authorize them on. 在更彻底地阅读了您的问题之后,除非您将客户端重定向到尝试对其进行授权的站点,否则恐怕无法告诉您将cookie写入客户端浏览器的方法。 Browser cookies operate on a security model known as Same Origin Policy . 浏览器cookie在称为“ 相同来源策略”的安全模型上运行。 This basically means that domains can only issue cookies for their OWN domains and may not issue cookies for others domains. 这基本上意味着域只能为自己的OWN域发布cookie,而不能为其他域发布cookie。 In your particular case: 在您的特定情况下:

client --CONTACTS--> foo.com --cURL-LOGIN--> bar.com
bar.com --bar.com-COOKIE--> foo.com --foo.com-COOKIE--> client
client --foo.com-COOKIE--> bar.com (Will not work)

Basically, foo.com CAN NOT create cookies on the client for bar.com! 基本上,foo.com不能在bar.com的客户端上创建cookie!

The user's browser will most probably not allow you to set cookies for another domain anyway. 无论如何,用户的浏览器很可能不允许您为另一个域设置cookie。

You can, in your PHP code, login and fetch a session cookie for (eg) Hotmail. 您可以使用PHP代码登录并获取(例如)Hotmail的会话cookie。 But you won't be able to pass that session on to the user (so he/she would also be logged in). 但是您将无法将该会话传递给用户(因此他/她也将登录)。

This is because many browsers and configurations deny setting 3rd party cookies. 这是因为许多浏览器和配置都拒绝设置第三方Cookie。

You can set the cookie in the user's browser without redirecting him to the server. 您可以在用户的​​浏览器中设置cookie,而无需将其重定向到服务器。 what you need to do is get the user to hit your php page with the curl code in it. 您需要做的是让用户使用curl代码访问您的php页面。 then you can take his post data and send this data to the server. 那么您可以获取他的帖子数据,并将其发送到服务器。 But dont let the server redirct you. 但是不要让服务器重新引导您。 Set the option as CURLOPT_FOLLOWLOCATION as false and set CURLOPT_HEADER as true Now grab the header and extract the cookie and location headers and then pass those as headers to the client browser like header("Location: ...) and header("Set-Cookie: ....). 将选项CURLOPT_FOLLOWLOCATION设置为false并将CURLOPT_HEADER设置为true现在抓取标题并提取cookie和位置标题,然后将它们作为标题传递给客户端浏览器,例如header(“ Location:...)and header(” Set-Cookie :....)。 you can also send other headers by extracting them too. 您也可以通过提取其他标头来发送它们。 the following [post] html page not getting cookies through libcurl has a similar situation 以下[post] html页面未通过libcurl获取cookie的情况类似

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM