所以我试图从一个数组中返回的项目是由用户输入的信息从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>";}
}
}
}
?>
在新的页面请求发生之前,您无法读取cookie的值。这是因为cookie数据的值是随页面请求一起发送的。因此,只有在它被设置并发出一个新的页面请求之后,才能访问它的值。
虽然我一般同意约翰在这里,我想补充:您的脚本从客户端浏览器发送的请求中获得特殊头部的cookie。考虑到浏览器在发送第一个页面请求时不知道未来的cookie,因此无法提供它们