Read classic ASP's cookies with PHP

我怕爱的太早我们不能终老 提交于 2019-12-24 13:13:07

问题


How do I obtain the asp cookie's name and value using PHP so i may assign it to a variable? PHP and ASP are on the same domain.

Classic Asp Create Cookie

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

Classic ASP Read Cookie

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

Reading The ASP cookies with PHP echo

echo $_COOKIE["apple"];

In firebug, after expanding the 'apple' cookie within the console, 'echo $_COOKIE["apple"]' outputs:

red=reddelicious&yellow=gingergold&green=grannysmith 

Tried:

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

回答1:


You could use the parse_str function in php

<?php

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

echo($apple["red"]);

?>



回答2:


To get the string red=reddelicious&yellow=gingergold&green=grannysmith to the format of a multi dimensional array try this:

$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; 
}

Then you can use the variable like this:

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


来源:https://stackoverflow.com/questions/31096781/read-classic-asps-cookies-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!