使用PHP阅读经典ASP的cookie

时间:2015-06-28 06:22:58

标签: php cookies asp-classic

如何使用PHP获取asp cookie的名称和值,以便将其分配给变量? PHP和ASP在同一个域中。

经典Asp创建Cookie

response.cookies("apple")("red")="reddelicious"
response.cookies("apple")("yellow")="gingergold"
response.cookies("apple")("green")="grannysmith"
response.cookies("apple").expires = dateadd("d",2,Now())

经典ASP阅读Cookie

strRed = request.cookies("apple")("red")
strYellow = request.cookies("apple")("yellow")
strGreen = request.cookies("apple")("green")

使用PHP echo

读取ASP cookie
echo $_COOKIE["apple"];

在萤火虫中,扩大了苹果'控制台内的cookie,' echo $ _COOKIE [" apple"]'输出:

red=reddelicious&yellow=gingergold&green=grannysmith 

尝试:

$strRed = $_COOKIE["apple"]["red"]; //doesn't work

2 个答案:

答案 0 :(得分:1)

你可以使用php中的parse_str函数

<?php

parse_str($_COOKIE["apple"], $apple);

echo($apple["red"]);

?>

答案 1 :(得分:0)

要将字符串red=reddelicious&yellow=gingergold&green=grannysmith设置为多维数组的格式,请尝试:

$itemArray = explode('&', $_COOKIE['apple']);  // Split variables at '&'
foreach($itemArray as $item){ // for each variable pair 'key=value'
    $itemParts = explode('=', $item); // split string to '[key, value]'
    $apple[$itemParts[0]] = $itemParts[1]; // set each item to $apple[key] = value; 
}

然后你可以使用这样的变量:

$strRed = $apple["red"]; //should work :)