-4

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??

user1159454
  • 3,267
  • 3
  • 19
  • 25
  • 2
    Your question might not be stupid but it’s definitely incomprehensible. What are you trying to accomplish? – Gumbo Jan 23 '12 at 11:26
  • I am trying to set a cookie but ONLY if all conditions (the $_POST value being in the array as one of the keys, is true) My problem is it doesn't look like setting an array before the cookie is set is working or allowed, so I don't know how to go about comparing it to the allowed values. All this cookie would do is tell the page it gets redirected to that the submitted value was successful. – user1159454 Jan 23 '12 at 11:34
  • possible duplicate of [Headers already sent](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) if your actual problem is indeed the statement order. – mario Jan 23 '12 at 11:42
  • This got closed pretty quickly, so I can't answer it, but take a look at [filter_input_array](http://www.php.net/manual/en/function.filter-input-array.php) to check your POST data against an array. – Leigh Jan 23 '12 at 11:47

2 Answers2

1

check if the post array variable is set or not.

if(isset($_POST['arr']))
{
 if(in_array("value", $bArray)
{
setcookie("choice1","true",time()+20);
}
}
Naveen Kumar
  • 4,543
  • 1
  • 18
  • 36
  • Hey Naveen, thanks, but what I'm trying to do is not just accept it if it's set, but if it matches one of the array values. – user1159454 Jan 23 '12 at 11:37
1

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?

symcbean
  • 47,736
  • 6
  • 59
  • 94