我有两个网站。我想通过另一个域上的 curl 请求设置 cookie。
我正在从我的第一个网站发出 curl 请求。 以下是代码结构:
$url = 'http://www.secondwebsite.com/ext/access/api/manage.php';
$data = array('id' => '23',
"firstname" => "First name",
"lastname" => "last name",
"email" => "email@gmail.com",
'username' => 'username',
'password' => 'password123',
'action' => 'add',
'authkey' => '12345tgtgtt');
$ch = curl_init($url);
$data_string = urlencode(json_encode($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer" => $data_string));
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
manage.php(代码结构)
setcookie('username', $_POST['username'], time() + (86400 * 30), "/"); // 86400 = 1 day
setcookie('password', $_POST['password'], time() + (86400 * 30), "/"); // 86400 = 1 day
但没有为 www.secondwebsite.com 设置 cookie。 还有其他方法可以设置跨域cookie吗? 实际上我想在两个网站之间开发 SSO(单点登录)功能。我希望如果任何用户登录 firstwebsite.com,那么用户将自动登录 secondwebsite.com。
在 php 中是否有任何 SSO 功能的方法?请分享..
最佳答案
您不能在域之间共享 cookie。想象一下 session 窃取和其他类似的事情。
有很多关于创建 SSO 的网站,例如http://merbist.com/2012/04/04/building-and-implementing-a-single-sign-on-solution/ 这是来自谷歌的第一个。
关于php - 如何通过 curl 或任何方法设置跨域 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28220409/