2016-04-15 6 views
0

別のPHPスクリプトからCookie変数にアクセスする際に問題があります。コードスニペットはこちら他のPHPスクリプトからPHPクッキーにアクセスする

if (isset($remember) && $remember == 'on') { 

    setcookie("username", $user, time() + (60 * 60 * 24 * 30)); 
    setcookie("password", $pass, time() + (60 * 60 * 24 * 30)); 
} 

外部スクリプトからCookieコンテンツにアクセスするにはどうすればよいですか?ありがとう

+0

**が** VERY UNSAFEクッキーに周りのパスワードを渡すいけません。あなたはちょうどこのユーザーkindomに鍵を譲っている! – RiggsFolly

+0

'$ _COOKIE'配列を見てください。すべてのCookieはすべてのスクリプトに渡されます。彼らがタイムアウトしていないと仮定して – RiggsFolly

答えて

0

外部スクリプトからHTTPリクエストを送信すると、setcookie()メソッドは、Set-CookieというHTTP応答ヘッダーに新しいヘッダーを追加します。

Set-Cookie: username=myUserName 
Set-Cookie: password=myUserPass 

は、これらのクッキーを読み取るには、このようなものを使用します(実際には、我々が必要とするすべての応答からHTTPヘッダを解析することです):

file_get_contents("http://localhost:8080/test.php"); 

$receivedCookies = array(); 
$headerCount = count($http_response_header); 

for($i = 0; $i < $headerCount; $i++){ 
    if(strpos($http_response_header[$i], "Set-Cookie") !== false){ 
     $receivedCookies[] = $http_response_header[$i]; 
    } 
} 

var_dump($receivedCookies); 
関連する問題