我用php开发了一个API函数,使用户从站点a登录到站点b。siteA-login.php
$url = "https://api.siteb.com/login";
$cookie = "cookie-api.txt";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
//curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "data=".json_encode($curlPost));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($curl, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($curl, CURLOPT_REFERER, $url);
$response = (curl_exec($curl));
curl_close($curl);
header("location: https://siteb.com/restricted-content");
这是我用CI4登录用户的网站:
$session = session();
$session->set(array('user'=>$user));
然而,在重定向之后,我去登录页面,而不是在限制的内容。我该如何解决?
您不能为其他域名设置cookie。
也许你可以试试这样做:
- 用户试图登录
- 从A到B获取密钥的API请求(在数据库站点B中记住)
- 用此键重定向用户到站点B(如果键正确,在数据库中查询)
- 登录用户,删除Key以防止二次使用