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!
ok the below code works fine on its own page loginbox.php
but I need it as an include on other pages, but I get the header error unless I remove the set cookie line (third line)
How can I go about this? any ideas?
setcookie() is itself a kind of header call so the same rules as for header() apply, ie no browser output prior to the header call.
If as you say this script works on its own but not when it's included, check the calling script for any output (including whitespace as explained in this thread) prior to the include.
PS: I changed the [ code ] tags to [ php ] to make it easier to read.
Not recommended since you don't really want to put the whole page in a buffer before outputting it.
The way I use is to buffer then include the page you want included right at the start of the page, and move whatever HTML comes from the page to a variable that can be put in wherever you'd include it normally. That way all the headers get taken care of, and a variable with the content is generally easier to take care of than an include.
<?php
ob_start(); // start buffering
include "loginbox.php"; // include file
$content = ob_get_contents(); // set the variable
ob_end_clean(); // empty the buffer
?>
--Start rest of code in the including page--
<?php echo $content; // put loginbox.php in ?>
--Rest of code--