dongque6377 2015-01-16 14:11
浏览 109
已采纳

PHP在同一台服务器上的不同域上创建cookie(cURL)

I'm creating a multilingual web site and after user logs in I need to create cookies across all domains (on same server):

example.com
example.de
example.it

I know for this solution - http://subinsb.com/set-same-cookie-on-different-domains (calling a .php file from inside img attribute) but is there any pure PHP solution ?

I'm trying to work out cURL solution but it doesn't work:

$url = "http://www.example.de/createcookie.php?value=hashvar";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_exec($curl);

After user login it needs to create cookie on current domain and then calls (via cURL) createcookie.php file that creates cookies on other domains.

Is something like this possible via cURL or some other PHP function like get_file_contents ?

All domains is placed on same server.

  • 写回答

2条回答 默认 最新

  • douhui3760 2015-01-16 14:18
    关注

    There is no pure php solution to this. You can not set a cookie for another domain than the one you are serving your response from. Curl and get_file_contents do not interact with the user's browser. Setting a cookie is performed by the browser which handles your server response.

    However, instead of serving an image (or other file type) from the other two domains (called third party domains), you can also redirect through all those domains and set a cookie before every subsequent redirect (using header("Location: url");)

    I.e. after login on example.com, you redirect to:

    • example.it/your_script.php. The file you redirect to sets the cookie and without returning any html redirects to
    • example.de/your_script.php. Again, you set a cookie and redirect again to
    • example.com/login_destination.php

    This however introduces a higher load time than using non-blocking third party objects in your html.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?