Php 如何通过curl或任何方法设置跨域cookie?
我有两个网站。我想在另一个域上通过curl请求设置cookies 我从我的第一个网站提出卷发请求。 以下是代码结构:Php 如何通过curl或任何方法设置跨域cookie?,php,curl,cookies,cross-domain,setcookie,Php,Curl,Cookies,Cross Domain,Setcookie,我有两个网站。我想在另一个域上通过curl请求设置cookies 我从我的第一个网站提出卷发请求。 以下是代码结构: $url = 'http://www.secondwebsite.com/ext/access/api/manage.php'; $data = array('id' => '23', "firstname" => "First name", "lastname" => "last name", "email" => "email@
$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(代码结构)
但是www.secondwebsite.com没有设置cookies。
是否有其他方法来设置跨域cookie?
实际上,我想开发两个网站之间的SSO(单点登录)功能。我想如果任何用户登录firstwebsite.com,那么该用户将自动登录secondwebsite.com
php中是否有实现SSO功能的方法?请共享..您不能在域之间共享cookie。想象一下会话盗窃和其他类似的事情 有很多关于创建SSO的网站,例如。
这是google的第一个。您可以使用php中的访问控制方法跨域启用Cookie和会话 将此代码保存到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和会话将由您的请求域控制。Cookie和会话数据也将位于同一浏览器中…您不能。想象一下,如果
nastysite.ru
可以为您的onlinebank.com
操作(设置/读取/更改/删除)cookies,会发生什么情况?
$.ajax(.....
crossDomain: true,
xhrFields: {
withCredentials: true
},
...);