Page 1 of 1

setting cookies in .inc file: what with these "headers"?

Posted: Mon Jul 11, 2011 12:42 pm
by simonmlewis

Code: Select all

<?php
$u=$_GET['u'];
if ($u != NULL)
{
$name=$_POST['name'];
$session=$_GET['session'];
setcookie("name", $name, time()+3600);
setcookie("session", $session, time()+3600);
}
I am posting 'name' and 'session' to the top of the page.
Name is the persons posted name, and session is a random number of so-many digits.

I get this when I use it:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\phpMyAdmin\site\index.php:39) in C:\xampp\phpMyAdmin\79new\includes\chat79.inc on line 7

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\phpMyAdmin\site\index.php:39) in C:\xampp\phpMyAdmin\79new\includes\chat79.inc on line 8
Line 7 and 8 are the two cookie lines.

Why won't this work?

Re: setting cookies in .inc file: what with these "headers"?

Posted: Mon Jul 11, 2011 1:10 pm
by Jonah Bron
That means that there has already been output. There's probably a space before the opening tag (<?php) or something.

After any output, setcookie(), session_start(), and header() functions cannot be called because they manipulate the header.

Re: setting cookies in .inc file: what with these "headers"?

Posted: Mon Jul 11, 2011 1:17 pm
by simonmlewis
Sorry but I don't understand.
The code is in an Include file, so yes, within the overall rendered page there is a great deal of code above it. But I can't put this into a *.php file separately anyway. Unless it was all done in a POPUP window, which I prefer not to do.

I've added cookies within PHP files before, where the cookie setup script is way down the page.

Why will this not work? and what do you mean by there has already been output? do you mean Cookies must be set right slap-bang at the top of the page?

If so - why??

Re: setting cookies in .inc file: what with these "headers"?

Posted: Mon Jul 11, 2011 1:25 pm
by AbraCadaver
There is output at: C:\xampp\phpMyAdmin\site\index.php: Line: 39

Re: setting cookies in .inc file: what with these "headers"?

Posted: Mon Jul 11, 2011 1:32 pm
by simonmlewis
Yes, that's the start of the PHP side of the index.php template. Where the menu script is.

Re: setting cookies in .inc file: what with these "headers"?

Posted: Mon Jul 11, 2011 1:40 pm
by simonmlewis
Actually, maybe the only way for me to do this, is to post the original form to a PHP file, stock the COOKIE, and then return them to the main page where it uses the cookie. If it means you cannot just store a cookie within an include file at the top of the page.

Re: setting cookies in .inc file: what with these "headers"?

Posted: Mon Jul 11, 2011 1:51 pm
by Jonah Bron
Because creating a cookie adds a header field, which comes before the output. That's how HTTP works, it's not PHP's fault. You can't output headers after normal page contents.

Re: setting cookies in .inc file: what with these "headers"?

Posted: Mon Jul 11, 2011 1:53 pm
by simonmlewis
You can do it after a number of queries though, even if the queries don't actually render anything to a page.
It's annoying, but as I say, guess I will have to do it in a separate PHP file that then redirects the user to the INCLUDE file that looks up the cookie.