PHP array from cookie info: only shows up after yo

2019-01-20 18:45发布

So I'm trying to return items from an array that's made out of user inputted info from cookies, and it works fine, except when you first visit the page from the previous page, nothing shows up, and you have to refresh it to actually see the array. I have no idea why it wouldn't show up the first time around? (There are some JavaScript alerts in there that work fine. It's only the array that I'm having trouble with.) Here's my code, any advice would be great, thanks:

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


                        }


    }


?>

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-20 18:57

You can't read the value of a cookie until a new page request is made. This is because the value of cookie data is sent with the page request. So it isn't available for to access its value until after it is set and a new page request is made.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-20 19:08

While I generally agree with John here, I want to add: your script gets cookie from special header in request sent by client's browser. Considering browser didn't know future cookies when it was sending first request for page, it could not provide them.

查看更多
登录 后发表回答