php - 如何通过 curl 或任何方法设置跨域 cookie?

标签 php curl cookies cross-domain setcookie

我有两个网站。我想通过另一个域上的 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/

相关文章:

javascript - Yii2 Active Record 在 where 子句中使用 FIND_IN_SET

php-node Node Express 找不到模块

java - Android-Java HTTPCLIENT 通过 Content-Disposition 图片上传发送 http 参数

ruby-on-rails - 使用 curl、params 数组测试 rails grape API

python - 从登录的另一端下载文件

javascript - Wordpress 收藏夹插件 - 从 cookie 中读取一系列帖子

php - if(!$variable) 和 if(isset($variable)) 有什么区别?

curl - 使用 api 列出 github 上的所有 Java 存储库

c++ - Libcurl+QtCreator+debian

android - 将 Cookie 从 HttpURLConnection (java.net.CookieManager) 传递到 WebView (android.webkit.CookieManager)