来自cookie信息的PHP数组:仅在刷新页面后显示?

所以我试图从一个由用户输入的信息来自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数据的值随页面请求一起发送.因此,在设置并发出新的页面请求之前,它不可用于访问其值.

上一篇:python – Kivy – 单击按钮时编辑标签


下一篇:Microsoft Dynamics CRM 常用JS语法(已转成vs2017语法提示)