简体   繁体   English

PHP读取另一个域上的cookie

[英]PHP read a cookie that is on another domain

I have two domains. 我有两个域名。 One domain contains the login script. 一个域包含登录脚本。 It creates a cookie when logged in. Another domain have a URL shortener. 它在登录时创建一个cookie。另一个域有一个URL缩短器。

So, on the 2nd domain that have the URL Shortener script have a file called session.php. 因此,在具有URL Shortener脚本的第二个域上有一个名为session.php的文件。 Usually I was using $_COOKIE['sessionid'] to get the session id and match it using database. 通常我使用$_COOKIE['sessionid']获取会话ID并使用数据库进行匹配。

How can I get the session id now? 我现在如何获得会话ID? I have tried few ways but none of them have solve my problem. 我尝试过几种方法,但没有一种能解决我的问题。

For obvious security reasons, you can't read a cookie that belongs to another domain. 出于明显的安全原因,您无法读取属于另一个域的cookie。 You can do it across sub-domains though. 您可以跨子域执行此操作。

Why not append the session id to the forwarded URL? 为什么不将会话ID附加到转发的URL?

Have you considered using a SSO implementation? 您是否考虑过使用SSO实施?

http://www.jasig.org/cas http://www.jasig.org/cas

http://en.wikipedia.org/wiki/Single_sign-on http://en.wikipedia.org/wiki/Single_sign-on

We use it at work, it's awesome! 我们在工作中使用它,真棒! It means we don't have to worry about these types of problems. 这意味着我们不必担心这些类型的问题。

Cookies are sent by the browser and only to the domain, the cookies were set for. Cookie由浏览器发送,仅发送到域,Cookie已设置为。

There is not much (meaning nothing ;) ) you can do. 你能做的并不多( 没有意义;))。

But if your domains are two different subdomains (eg login.yourdomain.com and shortener.yourdomain.com ) you just have to set the domain name accordingly to make the cookie valid for all subdomains. 但如果您的域名是两个不同的子域(如login.yourdomain.comshortener.yourdomain.com ),你只需要设置相应的域名,使饼干适用于所有子域。
In this case, it would be .yourdomain.com . 在这种情况下,它将是.yourdomain.com

You might want to read the documentation of setcookie() . 您可能想阅读setcookie()的文档。


Maybe it is better if you clearly describe what you want to accomplish. 如果你清楚地描述你想要完成的事情,也许会更好。 Probably there is another solution that does not involve cookies. 可能还有另一种不涉及cookie的解决方案。

Just while setting cookie from the login page set the cookie to entire domain like this 就在从登录页面设置cookie时,将cookie设置为整个域,就像这样

setcookie("c","value",time()*3600*24,"/");

in this way you can set cookie to your entire domain. 通过这种方式,您可以将cookie设置为整个域。

You can't read a cookie from the other domain. 您无法从其他域读取cookie。
though there are several ways to pass a session id. 虽然有几种方法可以传递会话ID。 You can search SO for the cross-domain authorization 您可以搜索SO以获取cross-domain authorization

The easiest way is to pass a session id via query string 最简单的方法是通过查询字符串传递会话ID

You can't. 你不能。 Cookies are bound to a single domain. Cookie绑定到单个域。 You can use cookies across multiple subdomains though. 您可以在多个子域中使用cookie。

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

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