繁体   English   中英

带有 cURL 的 PHP 会话

[英]PHP Session with cURL

我用 php 开发了一个 API 函数来让用户从 siteA 登录到 siteB。 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));

但是,在重定向后,我进入登录页面,而不是受限内容。 我该如何解决?

你不能像你一样为其他域设置 Cookies。

也许你可以尝试这样的事情:

  • 用户尝试登录
  • 从 A 到 B 的 API 请求以获取密钥(在数据库站点 B 中记住)
  • 使用此密钥将用户重定向到站点 B(如果密钥正确,请在数据库中查询)
  • 登录用户,删除Key,防止二次使用

暂无
暂无

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

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