473,608 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Headers already sent error in conditional block

Hi,

I am using header("Locatio n: in two places in my script, but they are
in mutually exclusive blocks i.e if one runs, the other can't.

However, an error is thrown pointing to the session_destroy () line
saying it started output. But it's not being called!

I removed the header() line below session_destroy () but it still gives
the same error so it seems to be session_destroy () existing in the
script even if not called.
if($loginmode== "logout")
{
$_SESSION=array ();
if(isset($_COOK IE[session_name()]))
{
setcookie(sessi on_name(),'',ti me() -42000,'/');
}

session_destroy ();

header("Locatio n: index.php");
exit;
}
// Another bhlock that can't run if the first block has......

if($errorMessag e=="")
{
// Set the $_SESSION variables for this logon

$_SESSION['loginuser']=$row['username'];
$_SESSION['loginlevel']=$row['level'];

header("Locatio n: index.php");
exit;
}
How can I write the php script so that multiple header("Locatio n:
options are present, or can co-exist with session_destroy () ?
Jun 27 '08 #1
2 1319
On 9 Jun, 13:35, mercedes1954 <alandam...@hot mail.comwrote:
Hi,

I am using header("Locatio n: in two places in my script, but they are
in mutually exclusive blocks i.e if one runs, the other can't.

However, an error is thrown pointing to the session_destroy () line
saying it started output. But it's not being called!

I removed the header() line below session_destroy () but it still gives
the same error so it seems to be session_destroy () existing in the
script even if not called.

if($loginmode== "logout")
* * * * {
* * * * $_SESSION=array ();
* * * * if(isset($_COOK IE[session_name()]))
* * * * * * * * {
* * * * * * * * setcookie(sessi on_name(),'',ti me() -42000,'/');
* * * * * * * * }

* * * * session_destroy ();

* * * * header("Locatio n: index.php");
* * * * exit;
* * * * }

// Another bhlock that can't run if the first block has......

if($errorMessag e=="")
* * * * {
* * * * // Set the $_SESSION variables for this logon

* * * * $_SESSION['loginuser']=$row['username'];
* * * * $_SESSION['loginlevel']=$row['level'];

* * * * header("Locatio n: index.php");
* * * * exit;
* * * * }

How can I write the php script so that multiple header("Locatio n:
options are present, or can co-exist with session_destroy () ?
Not that I doubt you or anything, but what precisely stops the second
block from running if the first one has run? Why can't there be
something in $errorMessage just because $loginmode=="lo gout"?

Also, the complete error message will detail 2 lines, the one where
output was started and the one that is trying to send more headers. Is
the second reference to the header() line directly below the
session_destroy ()?

Also, looking at your code in the first block, you seem to check for
the presence of a session by checking for $_COOKIE[session_name()].
However, even if you do not find this cookie, you still invoke
session_destroy (). I would expect thhis to give an error along the
lines of "Trying to destroy uninitialized session..." and this would
certainly be some "output already sent".
Jun 27 '08 #2
On Jun 9, 2:16*pm, Captain Paralytic <paul_laut...@y ahoo.comwrote:
On 9 Jun, 13:35, mercedes1954 <alandam...@hot mail.comwrote:


Hi,
I am using header("Locatio n: in two places in my script, but they are
in mutually exclusive blocks i.e if one runs, the other can't.
However, an error is thrown pointing to the session_destroy () line
saying it started output. But it's not being called!
I removed the header() line below session_destroy () but it still gives
the same error so it seems to be session_destroy () existing in the
script even if not called.
if($loginmode== "logout")
* * * * {
* * * * $_SESSION=array ();
* * * * if(isset($_COOK IE[session_name()]))
* * * * * * * * {
* * * * * * * * setcookie(sessi on_name(),'',ti me() -42000,'/');
* * * * * * * * }
* * * * session_destroy ();
* * * * header("Locatio n: index.php");
* * * * exit;
* * * * }
// Another bhlock that can't run if the first block has......
if($errorMessag e=="")
* * * * {
* * * * // Set the $_SESSION variables for this logon
* * * * $_SESSION['loginuser']=$row['username'];
* * * * $_SESSION['loginlevel']=$row['level'];
* * * * header("Locatio n: index.php");
* * * * exit;
* * * * }
How can I write the php script so that multiple header("Locatio n:
options are present, or can co-exist with session_destroy () ?

Not that I doubt you or anything, but what precisely stops the second
block from running if the first one has run? Why can't there be
something in $errorMessage just because $loginmode=="lo gout"?

Also, the complete error message will detail 2 lines, the one where
output was started and the one that is trying to send more headers. Is
the second reference to the header() line directly below the
session_destroy ()?

Also, looking at your code in the first block, you seem to check for
the presence of a session by checking for $_COOKIE[session_name()].
However, even if you do not find this cookie, you still invoke
session_destroy (). I would expect thhis to give an error along the
lines of "Trying to destroy uninitialized session..." and this would
certainly be some "output already sent".- Hide quoted text -

- Show quoted text -

Thank you for the quick response. My brain tricked me into looking at
line 19 - where session_destroy () is - instead of realising it was
line 19 of a small include file that had 2 blank lines after the ?>

Now how many times did I read that in countless posts before turning
here for help???

Thanks for the other comments, but it was a vastly cut down piece of
code and the original addressed your concerns.

Thanks again for your time.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2591
by: Dariusz | last post by:
I have a problem where when I run the PHP code offline, there are no errors produced and the code runs as expected. However when I uploaded the same script and run it, it says the headers have already been sent. The script is for a "downloader" type script, where the user clicks on a file link (to the PHP script) using $_GET to pass the selected files ID to the script, and the information about the file is read out from a database, also...
6
2515
by: Douglas F. | last post by:
My host doesn't use buffering so I placed the form validation (php) ahead of the html like this: <?php if(isset($_POST)) { <do the validation processing> if(file_exists($fn)) { header("Location: $fn"); <---Bombs } }
5
2530
by: Philip Ronan | last post by:
OK, here's my 2p worth: === Q. Why am I getting the error message 'Headers already sent'? A. PHP produces this error message when you try to set a header for a web page after you have already started sending out the content of the page. Web content is always delivered with a few headers at the top, ending with a blank line. For example, a web page might start like this:
3
1635
by: lkrubner | last post by:
I'm getting this error: Warning: session_start(): Cannot send session cache limiter - headers already sent in /home/httpd/vhosts/publicdomainsoftware.org/httpdocs/pdsIncludes/McSessionInfo.php on line 34 This is line 33 to line 36 of that class:
8
24261
by: Andreas Klemt | last post by:
Hello, I get this error Message "cannot redirect after http headers have been sent" when I do this response.redirect ("home.aspx") How can I find out with vb.net if already a http header has been sent like If response.IsSentHttpHeader then ....
6
7758
by: Andreas Klemt | last post by:
Hello, I get this error Message "cannot redirect after http headers have been sent" when I do this response.redirect ("home.aspx") How can I find out with vb.net if already a http header has been sent like If response.IsSentHttpHeader then ....
22
2210
by: Platero | last post by:
Hi, I've a stupid question but... The code is the following: if(($role!='tutor')&&(array_key_exists('tutor_id',$_GET))) { $possible_ids = array(2,6,7,8,9,10); $t_id = $_GET;
4
1755
by: craigtomo | last post by:
I am getting the following error when i try to log on to my data base Warning: session_register() : Cannot send session cookie - headers already sent by (output started at D:\virtualservers\182763\root\test\login\checklogin.php:1) in D:\virtualservers\182763\root\test\login\checklogin.php on line 18 Warning: session_register() : Cannot send session cache limiter - headers already sent (output started at...
6
11476
Markus
by: Markus | last post by:
Things to discuss: Headers What are they? What does PHP have to do with headers? Why can they only be sent before any output? Common causes
0
8496
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8475
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8148
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8338
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6816
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3962
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4024
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1594
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.