我有两个网站。我想在另一个域上通过curl请求来设置cookie。
我在我的第一个网站上做卷发请求。代码结构如下:
$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但是cookies不是为www.secondwebsite.com设置的。有没有其他方法可以设置跨域cookie?实际上,我想开发两个网站之间的SSO(单点登录)功能。我想如果任何用户登录到firstwebsite.com,那么用户将自动登录到secondwebsite.com。
在php中有没有实现SSO功能的方法?请分享..
发布于 2015-01-30 01:11:50
您不能在域之间共享cookies。想象一下会话窃取和其他类似的东西。
有很多关于创建单点登录的站点,例如http://merbist.com/2012/04/04/building-and-implementing-a-single-sign-on-solution/,这是来自谷歌的第一个站点。
发布于 2017-06-19 19:00:44
您可以使用php中的Access-Control方法来实现跨域的Cookie和Session。
将此代码保存到php文件中
header("Access-Control-Allow-Origin: http://example.com");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods: GET, POST,OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, *");然后你必须通过ajax请求来请求这个文件。
$.ajax(.....
crossDomain: true,
xhrFields: {
withCredentials: true
},
...);Cookie和会话的请求域将控制从您的请求domain.And的Cookie和会话数据也将在您的同一浏览器...
https://stackoverflow.com/questions/28220409
复制相似问题