0

所以我试图从一个由用户输入的cookies信息组成的数组中返回项目,它工作正常,除非你第一次从上一页访问页面时,什么都没有显示,你必须将它刷新到实际见阵列。我不知道为什么它不会第一次出现?(那里有一些 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>";}
                                        }


                        }


    }


?>
4

2 回答 2

3

在发出新页面请求之前,您无法读取 cookie 的值。这是因为 cookie 数据的值是随页面请求一起发送的。因此,在设置它并发出新的页面请求之前,它不能访问它的值。

于 2013-05-02T20:48:18.713 回答
0

虽然我在这里普遍同意 John 的观点,但我想补充一点:您的脚本从客户端浏览器发送的请求中的特殊标头中获取 cookie。考虑到浏览器在发送第一个页面请求时不知道未来的 cookie,它无法提供它们。

于 2013-05-02T20:52:48.387 回答