简体   繁体   English

PHP在一个域中登录,然后在另一个域中使用会话

[英]PHP login in one domain then using the session in another one

So I have this variable I must send to another website. 因此,我必须将此变量发送到另一个网站。 How can I send it in a way that user can't change it? 如何以用户无法更改的方式发送它? It like an account id, for a login session, since the user logs in through Steam, he doesn't need to enter a password(at least on the site). 就像帐户ID一样,对于登录会话,由于用户通过Steam登录,因此他不需要输入密码(至少在站点上)。 I need to pass his account id to another domain securely. 我需要将他的帐户ID安全地传递到另一个域。 By securely I mean, I don't care if he sees it as long as he can't change it no matter what. 我的意思是,只要他无论如何都无法更改,我不在乎他是否会看到它。 is there any way of doing this? 有什么办法吗? The reason I can't let them log in directly in the first website is because steam has flagged it, like it did to a lot of sites that has ref codes. 我之所以不能让他们直接在第一个网站上登录,是因为Steam对其进行了标记,就像许多具有引用代码的站点所做的那样。 So the user is kinda not able to log in cause in the warning the "continue anyways" button is so tiny that many users don't even see it. 因此,用户有点无法登录,原因是警告中的“仍然继续”按钮很小,以至于许多用户甚至看不到它。 So I made like big sites did, bought another domain and redirected the login to it. 因此,我像大型网站一样,购买了另一个域,然后将登录重定向到该域。 This other site is not blacklisted so the login can go normally. 此其他站点未列入黑名单,因此登录可以正常进行。

That issue seems a bit challenging, I mean I would do it through an api request. 这个问题似乎有点挑战,我的意思是我将通过api请求来完成此任务。 Got to be that way. 就是那样。 Probably somebody spices that task a bit more 可能有人加香料了

But that is the way to communicate between two domains, via curl calls and therefore you need TO BUild an api for it. 但这是通过curl调用在两个域之间进行通信的方式,因此您需要为其构建一个api。 The redirection is an issue there probably via js-ajax. 重定向可能是通过js-ajax出现的问题。

[Edit] Redirect users to the second site (php Header), once in the second site, do the login, that is, authenticate and populate the session variable through curl requests to the first site [编辑]将用户重定向到第二个站点(php标头),然后在第二个站点中进行登录,即通过对第一个站点的curl请求对会话变量进行身份验证和填充

I would look into using CURL to authenticate and get a response back from the authenticating domain to use on the origin domain. 我将研究使用CURL进行身份验证,并从身份验证域获得响应以在原始域上使用。

origin.com/authenticate-user.php origin.com/authenticate-user.php

 $ch = curl_init('http://authenticator.com/authenticate-user.php'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('steam_id' => $_SESSION['steam_id']))); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $response = curl_exec($ch); curl_close ($ch); $_SESSION['user_valid'] = $response; 

authenticator.com/authenticate-user.php authenticator.com/authenticate-user.php

 //optional conditions to test origin or requesting source to prevent cross-site attacks. if (isset($_POST['steam_id']){ //..validate user here echo 1; } else { echo 0; } 

This would make the request sent by your server instead of the client, and unchangeable by the user. 这将使请求由您的服务器而不是客户端发送,并且用户无法更改。 It will also only require you to manage a session from one domain instead of two, since all the authenticator does is process the information sent to it. 它还将只需要您从一个域(而不是两个域)管理会话,因为所有身份验证者所做的都是处理发送给它的信息。 Not sure why a session would need to be set on the authenticating domain though, if you can give a scenario it would be needed. 但是,不确定是否需要在身份验证域上设置会话,如果可以给出方案的话,则需要这样做。 You can also change the response to anything you like, serving the entire webpage if desired. 您还可以将响应更改为任何您喜欢的内容,并根据需要投放整个网页。


Another option is to use cookies as opposed to sessions. 另一种选择是使用cookie而不是会话。 Cookies can be shared cross-domain by allowing it within your API processor via CORS and an XMLHttpRequest (ajax). 通过允许CORSXMLHttpRequest (ajax)在API处理器中允许Cookie,可以跨域共享Cookie。 But anything being sent via XMLHttpRequest can be stopped and manipulated by the user. 但是,通过XMLHttpRequest发送的任何内容都可以由用户停止和操纵。

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

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