Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Headers allready sent when trying to setcookie

Status
Not open for further replies.

alfalf

Programmer
Joined
Mar 6, 2003
Messages
155
Location
BA
Hello.
I have a small problem.

I know that setcookie must be sent before all <html> or <head> headers.

I have an search form, and option there to remember search settings for future visits.

Then, when form is Submitted, it redirects user to (for example) results.php page, on wich I put on top of it a setcookie to collect variables with $_POST. It is like this:

<?php
if (@isset($_COOKIE['b_s']))
{
}
else
{
/* one year cookie*/
setcookie (&quot;b_s&quot;, $_POST['D1'].&quot; &quot;.$_POST['sel'].&quot; &quot;.$_POST['category'].&quot; &quot;.$_POST['country'].&quot; &quot;.$_POST['state'].&quot; &quot;.$_POST['city'], time()+24*60*60*100);
}
?>


But... I get <headers allready sent error> when I compile it.

Does this $_POST superglobal have to do something with it, should I use $_GET or what?

Even if I put only this line on the top:
setcookie (&quot;b_s&quot;, $_POST['D1'].&quot; &quot;.$_POST['sel'].&quot; &quot;.$_POST['category'].&quot; &quot;.$_POST['country'].&quot; &quot;.$_POST['state'].&quot; &quot;.$_POST['city'], time()+24*60*60*100);

I still get the same result.

I would like those Posted values to be passed to my cookie value.

Note: When I use both above codes without $_POST (I mean when I put for cookie value something like: &quot;1 2 5 6 7 8&quot;) it works ok.

Thanks for any suggestions.
 
Ok, cookies must be set before you send any output to client browser. If there is just &quot;echo $blah;&quot; above the cookie, you will receive the error even if &quot;<HTML>&quot; and other code is below the setcookie().

In fact I also had some problems when using superglobals such as $_POST['...'] or $_GET['...'] in my code. So I made something like this at the beginning of the code which solves the issue:
Code:
<?php
if(isset($_POST['blah'])) $blah=$_POST['blah'];

// then you can set cookie like this

setcookie(&quot;blah&quot;,$blah,time()+900); // this should work fine

?>

Try this, maybe this is the solution of your problem. It works fine within my code ;-)
 
Also, check for any spaces or new lines before the <?php, if anything is before it then you won't be able to set cookies.
 
All good advice.
However, when you look at the error message it will actually tell you where the output started, it usually tells the exact line number.
setcookie does nothing else than send an initial header with the cookie. Since headers must be the very first thing sent to the browser any output before the first header will produce the error message.
You can also use PHPs very convenient output buffering functions to keep anything to be sent before the headers.
Read about output handling:
 
I'm having a realy big trouble here.
Yap. I tryed what gizmicek suggested, also, was carefull about empty lines as KEMPCGDR said, and have written setcookie with outputed buffering like this:

<?php
ob_start();
?>
<?php
$a4=$_POST['country'];
?>
<?php
setcookie ('b_s', &quot;$a4&quot;, time()+24*60*60*100);
?>
<html>
<head>
<title>Sans Titre</title>
</head>
<body bgcolor=&quot;#FFFFFF&quot;>
<?php
ob_end_flush();
?>
</body>
</html>

or such an variation:
<?php
ob_start();
$a4=$_POST['country'];
setcookie ('b_s', &quot;$a4&quot;, time()+24*60*60*100);
?>
<html>
<head>
<title>Bla bla</title>
</head>
<body bgcolor=&quot;#FFFFFF&quot;>
<?php
ob_end_flush();
?>
</body>
</html>

and still cannot get cookie to work (I don't receive message that headers are allready sent, don't know what is happening.
But, this piece of code works perfectly:
<?php
setcookie ('b_s', '1-2-3-4-5-8-9-10', time()+10368000);
?>
and there's no problem with it.

??
Thanks.



 
I notice you do an awful lot of context-switching in your PHP samples. That's not a good idea.

Also, in both examples of your use of output contol functions, you're issuing ob_flush() and then producing more output. There's no point in using output control functions unless they're going to control all your output.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Well, here is a piece of code from my pages which works fine (but I noticed some problems to login into pages on several computers, so I'm going to read something about sessions and do the login process using php sessions):
Code:
<?php
  $dbname=&quot;&quot;;    // adresa mysql databaze
  $dbacount=&quot;&quot;;      // uzivatelske jmeno
  $dbpwd=&quot;&quot;;           // heslo
  $administrator=&quot;&quot;;
/////////////////// VYPNUTI CACHE ////////////////////
  header(&quot;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quot;);
  header(&quot;Last-Modified: &quot; . gmdate(&quot;D, d M Y H:i:s&quot;) . &quot; GMT&quot;);
  header(&quot;Cache-Control: no-cache, must-revalidate&quot;);  // HTTP/1.1
  header(&quot;Pragma: no-cache&quot;);                          // HTTP/1.0

  include &quot;functions.inc&quot;;

  $mysql=mysql_connect($dbname,$dbacount,$dbpwd);
  mysql_select_db(&quot;destandazi&quot;,$mysql);

  fix_online_status($mysql);  // nastavi online na 0 u uzivatelu, kteri nebyli aktivni 10 minut

  if(isset($_COOKIE['auth_user'])) {  // prodlouzeni doby vyprseni
    $auth_user=$_COOKIE['auth_user'];
    $result=mysql_query(&quot;SELECT * FROM torch_users WHERE login='$auth_user'&quot;,$mysql);
    $lastvisit=time();
    mysql_query(&quot;UPDATE torch_users SET online='1', lastvisit='$lastvisit' WHERE login='$auth_user'&quot;,$mysql);
    setcookie(&quot;auth_user&quot;,$auth_user,time()+900);
    $usrstat=mysql_result($result,0,9);
    if(mysql_result($result,0,13)!=&quot;&quot;) $team_member=1;
  } else $usrstat=0;

// ...
?>

Don't care about the comments, they are written in my mother language (czech) and aren't relevant... ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top