来自 cookie 信息的 PHP 数组:仅在刷新页面后显示? [英] PHP array from cookie info: only shows up after you refresh the page?

查看:29
本文介绍了来自 cookie 信息的 PHP 数组:仅在刷新页面后显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图从一个由用户输入的 cookie 信息组成的数组中返回项目,它工作正常,除非您第一次从上一页访问该页面时,没有任何显示,您必须刷新它实际上看到了数组.我不知道为什么它不会第一次出现?(那里有一些 JavaScript 警报可以正常工作.只是我遇到了问题的数组.)这是我的代码,任何建议都会很棒,谢谢:

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


                        }


    }


?>

推荐答案

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

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.

这篇关于来自 cookie 信息的 PHP 数组:仅在刷新页面后显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆