dongqing314511 2013-01-10 17:41
浏览 58
已采纳

提交页面后显示$ _COOKIE []的旧值,直到下次刷新

I am having an issue where after first clicking Submit, and the page reloads, the page shows the old value for each cookie set until the page is refreshed or reloaded.

Here is essentially how I have it set up.

index.php

<?php 
    if(isset($_POST['filter'])){
        foreach($_POST['filter'] as $key=>$value){
            $xNums[] = $key;    
        }
        $vals = $_POST['filter'];
        updateFilter($vals, $xNums);
    }
?>
<form action='index.php' method='post'>
<input type='radio' name=filter['".$xNum."'] value='yes' />Show<input type='radio' name=filter['".$SomeVariable."'] value='no' />Hide
<input type=submit value=" Submit " />
<?php
    echo displayStatus($xNum);
?>

functions.php

function updateFilter($vals, $xNums){
    foreach($xNums as $xNum){
        $val = $vals[$xNum];
        setcookie($xNum, $val, time()+3600);
    }
    return;
}

function displayStatus($xNum){
    if($_COOKIE["'".$xNum."'"]=='no'){
        return "no";
    } else {
        return "yes";
    }
}
  • 写回答

1条回答 默认 最新

  • dongpeiwei8589 2013-01-10 17:48
    关注

    The cookie which you set using setcookie is not visible by PHP script in $_COOKIES superglobal variable until next request because setcookie only set Set-Cookie header in http response.

    If you want to read these cookies in the same request you can use:

    setcookie($xNum, $val, time()+3600);
    $_COOKIE[$xNum] = $val;
    

    edit: here is similar problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?