473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
5 6425
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lau | last post by:
I need to send 1000 emails from an asp.net website. Normally I would use System.Web.Mail.MailMessage() to send thru an SMTP server. But the large amount of emails results in a timeout. My server...
1
by: Piotrekk | last post by:
Hi I have the following code: request = (HttpWebRequest)WebRequest.Create(FullPath); request.Headers.Clear(); request.ProtocolVersion = HttpVersion.Version11; response =...
8
alpnz
by: alpnz | last post by:
Hi, I have a need to send snap reports to various shipping agents. E.g. A PalletCard, A FreightNote, A Consignment Advice, and an Export declaration of Conformity. It is easy enough to code a...
1
by: pankhudi | last post by:
How can i send multiple files of different lenghts over a network and using the same socket without the need of closing it in between. and what if this is to be done in JAVA ?
2
by: sat1983 | last post by:
Hi I am beginner to C Sharp. trying to attach a multiple files to a mail. I can attach a single file or i can create multiple object of an Attachment class and add to a mail attachment object....
5
by: wktarin | last post by:
Hi. I'm a relative newcomer to the world of php, and I keep bumping into a problem with a mail () form. I need to send an automatic email to two addresses, but I can't seem to get it to work. One...
5
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I've been using stream_context_create() to send cookies along with a file_get_contents() call when requesting a remote page. This is useful if I need to pass information to the remote page that my...
1
by: kalees waran | last post by:
is it possible function available to use multiple headers in a page
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.