dpa89292 2011-04-16 02:07
浏览 37

通过ajax设置和检索外部cookie

I am sending an Ajax request to a PHP script where I am using setcookie to set the cookies on an external domain. After that, I am sending another Ajax request to the external domain to read the cookies. I am unable to achieve the results. I am using jQuery to send Ajax requests.

The PHP script:

<?php
// yummy cookies ?

header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']);
header("Access-Control-Allow-Credentials: true");

if($_GET['read'] == true) {
    echo json_encode(
        array(
            'user_id' => $_COOKIE['user_id'],
            'twitter_name' => $_COOKIE['twitter_name'],
            'facebook_id' => $_COOKIE['facebook_id']
        )
    );
}

if($_GET['write'] == true) {
    if($_GET['type'] == 'twitter') {
        setcookie('twitter_name', $_GET['twitter_name'], time() + 1209600);
        setcookie('user_id', $_GET['user_id'], time() + 1209600);
    }

    if($_GET['type'] == 'facebook') {
        setcookie('facebook_id', $_GET['facebook_id'], time() + 1209600);
        setcookie('user_id', $_GET['user_id'], time() + 1209600);
    }
}
  • 写回答

1条回答 默认 最新

  • doutang1884 2011-04-16 04:02
    关注

    You might be running into the problem where ajax doesn't allow cross site calls (can't call to another domain).

    Most people use an iframe instead of ajax, put the src to the other site's cookie script.

    The iframe would look like <iframe src="otherdomain.com/cookiecode.php?var1=something" style="visibility:hidden;display:none"></iframe>

    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退