doucheyi1347
2012-01-23 11:22
浏览 23
已采纳

更多cookie问题,php [关闭]

I'm sorry to keep asking stupid questions, but I have tried to research this and couldn't find it.

This time, I want to be able to check a _POST against an array, before deciding it's ok to set the cookie. Here's my snippet.

<?php
header( 'Location: http://www.site.com/ler.php' ) ;
?>
<?php
setcookie("choice1","true",time()+20);
?>


<?php

$match_id = strtoupper($_POST["arr"]);


//var_dump(implode($_POST));


$bArray = array(
"A"=>"1",
"B"=>"2",
"C"=>"3",
"D"=>"4",
"ETC"=>"5");

I need the array and variable to be set before the cookie, because I want to use array_key_exists conditional. I tried switching the order but that didn't do anything. I know it's not the header because the other code with cookies and headers works fine. Any ideas??

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyan3478 2012-01-23 11:31
    已采纳

    Not sure what you're asking here, but its clear that the code you've presented is probably not going to work as you expect.

    You're outputting body content before the call to setcookie(). So unless you've got output buffering enabled it's going to fail. Setting an expiry time of 20 seconds is a dumb idea too - client side clock is unlikely to be synchronised.

    Also there are several browsers which ignore all subsequent headers after a redirect. Further, depending on the timing of processing, some browsers will drop the connection after a redirect - in the absence of an ignore_user_abort() this may cause premature termination of the code.

    Further, presumably there's a reason for parsing the data / setting a cookie - how can you be sure this has completed before the redirect request from the browser is procesed?

    I want to be able to check a _POST against an array, before deciding it's ok to set the cookie

    Its apparent that the code you've provided does not check anything before setting the cookie - why have you included it in your question?

    点赞 评论
  • duanlan2003 2012-01-23 11:30

    check if the post array variable is set or not.

    if(isset($_POST['arr']))
    {
     if(in_array("value", $bArray)
    {
    setcookie("choice1","true",time()+20);
    }
    }
    
    点赞 评论

相关推荐 更多相似问题