2012-05-07 2 views
0

동일한 사이트에 대해 두 개의 쿠키를 설정하려고합니다. (하나를 제외하고는 HTTP이고 하나는 HTTPS 사이트입니다.) HTTP_SERVER가 http://www.somesite.com 포함되어 있음을 가정두 도메인에 대한 쿠키 설정

$cookie_domain_http = parse_url(HTTP_SERVER); 
$cookie_domain_https = parse_url(HTTPS_SERVER); 

그 HTTPS_SERVER, 나는 다음을 사용하고 https://ssl1.otherdomain.com이 포함되어 있습니다.

setcookie("referrer_key", 
    $_GET['referrer'], 
    time() + 60*60*24*365, 
    $cookie_domain_http['path'], 
    $cookie_domain_http['host']); // Set the cookie for the non-HTTPS version. 

if(ENABLE_SSL == "true") { 
    setcookie("referrer_key", 
    $_GET['referrer'], 
    time() + 60*60*24*365, 
    $cookie_domain_https['path'], 
    $cookie_domain_https['host']); // Set the cookie for HTTPs version. 
} 

이제 첫 번째 setcookie은 세션 쿠키를 올바르게 설정합니다. 그러나 두 번째 줄은 표시하지 않습니다.

어떻게이 코드를 사용할 수 있습니까? 특히 PHP에서는이 작업을 수행 할 수 있습니다.

답변

1

코드는 하나의 도메인, 즉 코드가 실행되는 도메인에만 설정할 수 있습니다. 가장 가까운 주소는 somesite.com으로 설정되며,이 경우 somesite.com의 모든 하위 도메인으로 전송됩니다.

somesite.com과 othersite.com에서 동일한 쿠키로 쿠키를 설정할 수 없습니다. 암호.

그렇지 않으면 임의의 도메인에 쿠키를 추가 할 수 없습니다.

1

http://php.net/manual/en/function.setcookie.php

끝에 또 하나 개의 매개 변수는 쿠키가 안전 여부를 지정하는 데 사용할 수,있다.

if (ENABLE_SSL == "true") 
    setcookie("referrer_key", 
    $_GET['referrer'], 
    time() + 60*60*24*365, 
    $cookie_domain_https['path'], 
    '.somesite.com', // Parse out the base domain, and put it here with the leading "." 
    true); //this is the parameter to set specify a secure cookie 
+0

이렇게하면 내 쿠키 목록에 추가되지 않습니다. –

+1

아마도 더 좋은 대답은 .somesite.com 접근 방식을 사용하는 것입니다. 새로운 답변을 잠시 확인해보십시오. – Bryan