所以我试图从一个由用户输入的信息来自cookie的数组中返回项目,它工作正常,除非你第一次访问上一页的页面,没有任何显示,你必须刷新它实际上看到了数组。我不知道为什么它不会第一次出现? (在那里有一些JavaScript警报工作正常。它只是我遇到麻烦的数组。)这是我的代码,任何建议都会很棒,谢谢:
$usergatsby = $_COOKIE['gatq'];
$usercatcher = $_COOKIE['catcherq'];
$userwaves = $_COOKIE['wavesq'];
$userstranger = $_COOKIE['strangerq'];
$userulysses = $_COOKIE['ulyssesq'];
$userpride = $_COOKIE['prideq'];
$usermockingbird = $_COOKIE['mockingbirdq'];
$userroad = $_COOKIE['roadq'];
if ($_COOKIE['fname'] == NULL or $_COOKIE['lname'] == NULL or $_COOKIE['address'] == NULL or $_COOKIE['city'] == NULL or $_COOKIE['state'] == NULL or $_COOKIE['zip'] == NULL or $_COOKIE['email'] == NULL)
{echo "<script language='javascript'>
window.alert('You left some information on the personal info page! You will be redirected.');
window.location.href='personal.php';</script>";
}
else
{
if ($_COOKIE['gatq'] == NULL &&
$_COOKIE['catcherq'] == NULL &&
$_COOKIE['wavesq'] == NULL &&
$_COOKIE['strangerq'] == NULL &&
$_COOKIE['ulyssesq'] == NULL &&
$_COOKIE['prideq'] == NULL &&
$_COOKIE['mockingbirdq'] == NULL &&
$_COOKIE['roadq'] == NULL)
{echo "<script language='javascript'>
window.alert('You don't have anything in your shopping cart! You will be redirected.');
window.location.href='inventory.php';</script>";
}
else
{$productarray = array($usergatsby=>'The Great Gatsby <img src="gatsby.jpg">',
$usercatcher=>'Catcher in the Rye <img src="catcher.jpg">',
$userwaves=>'The Waves <img src="waves.jpg">',
$userstranger=>'The Stranger <img src="stranger.jpg">',
$userulysses=>'Ulysses <img src="ulysses.jpg">',
$userpride=>'Pride and Prejudice <img src="pride.jpg">',
$usermockingbird=>'To Kill a Mockingbird <img src="mockingbird.jpg">',
$userroad=>'On the Road <img src="ontheroad.jpg">'
);
asort($productarray);
foreach ($productarray as $book=>$info)
{if ($book > 0)
{echo "Quantity: " . $book . " " . $info . "<br>";}
}
}
}
?>
答案 0 :(得分:3)
在发出新的网页请求之前,您无法读取Cookie的值。这是因为cookie数据的值随页面请求一起发送。因此,在设置并发出新的页面请求之前,无法访问其值。
答案 1 :(得分:0)
虽然我在这里普遍同意John,但我想补充一点:您的脚本会从客户端浏览器发送的请求中的特殊标头中获取cookie。考虑到浏览器在发送第一个页面请求时不知道将来的cookie,它无法提供它们。