By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,255 Members | 2,640 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,255 IT Pros & Developers. It's quick & easy.

sending multiple headers (session_start() and setcookie() )

P: n/a
Ben
Hi all,

In my .php file, I'm using both session_start() and setcookie() before
<html> tag. It gives me following warning message:

Warning: Cannot modify header information - headers already sent by
(output started at D:\Apache Group\Apache2\htdocs\YC\songs.php:4) in
D:\Apache Group\Apache2\htdocs\YC\ycphpfunc.php on line 148

My .php file looks like this:

<?php session_start(); ?>

<?php
ob_start();
include 'ycphpfunc.php';
$login = new login_class;
if ($_POST[logusername] == "" || $_POST[logpassword] == "") {}
else {
$login->check_login($_POST['logusername'], $_POST['logpassword'],
$_POST['remember']);
}
ob_end_flush();
?>
<html>
........
........
</html>

The call to check_login()calls setcookie() function through another
function inside my "ycphpfunc.php" file.

Can someone show me a way to include both session_start() and
setcookie() before <html>?

Thanks for your time!
Ben
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ben wrote:
In my .php file, I'm using both session_start() and setcookie() before
<html> tag. It gives me following warning message:

Warning: Cannot modify header information - headers already sent by
(output started at D:\Apache Group\Apache2\htdocs\YC\songs.php:4) in
D:\Apache Group\Apache2\htdocs\YC\ycphpfunc.php on line 148

My .php file looks like this:

<?php session_start(); ?>

It looks to me like there's white space (a line break) right here
inbetween the closing ?> and opening <?php I'm not sure why you've
coded it like this though. Why not just have one code block? See my
example below.
<?php
ob_start();
include 'ycphpfunc.php';
$login = new login_class;
if ($_POST[logusername] == "" || $_POST[logpassword] == "") {}
else {
$login->check_login($_POST['logusername'], $_POST['logpassword'],
$_POST['remember']);
}
ob_end_flush();
?>
<html>
.......
.......
</html>

The call to check_login()calls setcookie() function through another
function inside my "ycphpfunc.php" file.

Can someone show me a way to include both session_start() and
setcookie() before <html>?


Change it to be like this:

<?php
session_start();
********ob_start();
...
or like this:

<?php
********ob_start();
session_start();
...

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2

P: n/a
Ben wrote:
[snip]
Warning: Cannot modify header information - headers already sent by
(output started at D:\Apache Group\Apache2\htdocs\YC\songs.php:4) in
D:\Apache Group\Apache2\htdocs\YC\ycphpfunc.php on line 148 [snip] include 'ycphpfunc.php';
$login = new login_class;
if ($_POST[logusername] == "" || $_POST[logpassword] == "") {}
else {
$login->check_login($_POST['logusername'], $_POST['logpassword'],

[snip]

According to your error-message, the output is started at line 148 in
your ycphpfunc.php file, which is included before you call your
check_login method. The error isn't generated by session_start, but by
setcookie().

What does line 148 in ycphpfunc.php do? ;)
Roy W. Andersen
--
ra at broadpark dot no / http://roy.netgoth.org/

"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama
Jul 17 '05 #3

P: n/a

Roy W. Andersen wrote:
Ben wrote:
[snip]
Warning: Cannot modify header information - headers already sent by
(output started at D:\Apache Group\Apache2\htdocs\YC\songs.php:4) in D:\Apache Group\Apache2\htdocs\YC\ycphpfunc.php on line 148 [snip]
include 'ycphpfunc.php';
$login = new login_class;
if ($_POST[logusername] == "" || $_POST[logpassword] == "") {}
else {
$login->check_login($_POST['logusername'], $_POST['logpassword'],

[snip]

According to your error-message, the output is started at line 148 in

your ycphpfunc.php file, which is included before you call your
check_login method. The error isn't generated by session_start, but by setcookie().
just a reminder, the login_class class is also included inside
ycphpfunc.php file.
What does line 148 in ycphpfunc.php do? ;)
line 148 in ycphpfunc.php has the following line:
setcookie('remember',$cookie, time()+31104000);

Roy W. Andersen
--
ra at broadpark dot no / http://roy.netgoth.org/

"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama


Jul 17 '05 #4

P: n/a
Ben wrote:
Hi all,

In my .php file, I'm using both session_start() and setcookie() before <html> tag. It gives me following warning message:

Warning: Cannot modify header information - headers already sent by
(output started at D:\Apache Group\Apache2\htdocs\YC\songs.php:4) in
D:\Apache Group\Apache2\htdocs\YC\ycphpfunc.php on line 148

My .php file looks like this:

<?php session_start(); ?>

<?php
ob_start();
include 'ycphpfunc.php';
$login = new login_class;
if ($_POST[logusername] == "" || $_POST[logpassword] == "") {}
else {
$login->check_login($_POST['logusername'], $_POST['logpassword'],
$_POST['remember']);
}
ob_end_flush();
?>


1. Put everything under ob_start()
2. Send all headers (cookies/session/etc) before outputting

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #5

P: n/a
Ben wrote:

<?php session_start(); ?>

<?php
ob_start();


That blank line is killing you. In fact, any carriage return characters
not INSIDE <?php and ?> characters will cause output to be sent by PHP
unless you are using output buffering (ob_start()).

That means included/required files too !!!!

It can be quite upsetting to track all those down, so just make sure that
ob_start() is absolulutely the first thing that happens in ANY php script
file ...
marc.


--
I am not an ANGRY man. Remove the rage from my email to reply.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.