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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

There is output at: C:\xampp\phpMyAdmin\site\index.php: Line: 39
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Yes, that's the start of the PHP side of the index.php template. Where the menu script is.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply