header error

hi im working on an assignment and i cant figure out what this problem is or how to fix it

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/Assign2/inc/header.php:13) in /Applications/XAMPP/xamppfiles/htdocs/Assign2/inc/header.php on line 23

that like 23 looks fine to me but here it is
[php]header(“Location:…/pages/login.php”);[/php]

this is the only time i have this code in my assignment please help.

You cannot have ANY output to the browser before sending a header(). No html, no echos, no error messages, etc. Also be sure there is not a blank line above your <?php tag.

so if i have this would i have to change it so that the header("Location:… blah is before the echo?

im just trying to make it so if i go on another page on the site if i am not logged in it will automatically redirect to the login.php page

[php]<?php

if (isset($_SESSION[‘empID’]))
{
// the user is logged in
echo “

You are logged in, {$_SESSION[‘empID’]}!

”;
}
else
{
//the user is not logged in
header(“Location:…/pages/login.php”);
//echo ‘Log in’;
}

?>[/php]

That is fine because the output is conditional. You must have output somewhere else.

is this the complete codes?

this codes you posted should be fine.

Yes this is the complete code for redirecting the user to the login.php page if there is no one logged in. I have this within the my header using a

[php]<?php
include “…/inc/header.php”;
?>[/php]

This is on every page, full code for the header.php file

[php]<?php
require “dbconnect.php”;
?>

Template <?php if (isset($_SESSION['empID = 2'])) { // the user is logged in echo "

You are logged in, {$_SESSION['empID = 2']}!

"; } else { //the user is not logged in header("Location:../pages/login.php"); //echo 'Log in'; } ?>

Products | Add Product | Customers

[/php]

sorry if im breaking any rules with some HTML in there to

the below blocks of code should be at the very top of the page before you start your html session

[php]

<?php if (isset($_SESSION['empID'])) { // the user is logged in echo "

You are logged in, {$_SESSION['empID']}!

"; } else { //the user is not logged in header("Location:../pages/login.php"); //echo 'Log in'; } ?>

[/php]

you can not have html and then a redirect a header

the below blocks of code should be at the very top of the page before you start your html session <?php if (isset($_SESSION['empID'])) { // the user is logged in echo "

You are logged in, {$_SESSION['empID']}!

"; } else { //the user is not logged in header("Location:../pages/login.php"); //echo 'Log in'; } ?>

you can not have html and then a redirect a header

Iv done what wilson382 said but now but now im getting the same error just on s different line because i moved the php code

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/Assign2/inc/header.php:6) in /Applications/XAMPP/xamppfiles/htdocs/Assign2/inc/header.php on line 16
?>

it looks like there is something wrong with the

[php] header(“Location: …/pages/login.php”);[/php]

is it possible that the problem isn’t on this page but on the login.php page?

this is the login.php page code

[php]<?php
include “…/inc/header.php”;
?>

Login

Username:
Password:
<?php include "../inc/footer.php"; ?>[/php]

Look this is easy

[php]
if (not logged in)
{
header back to the login page
}

//here you all your private stuff and html outputs

[/php]

but please put your header and if statement at the very top.

if you think you doing it correctly and still getting the same error. show me the codes of how you doing it.
I might help you

Iv put this right up the top and im still getting that error

[php]?php
require “dbconnect.php”;

?>

<?php if (isset($_SESSION['empID'])) { // the user is logged in echo "

You are logged in, {$_SESSION['empID']}!

"; } else { //the user is not logged in header("Location: ../pages/login.php"); //echo 'Log in'; } [/php] The error Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/Assign2/inc/header.php:6) in /Applications/XAMPP/xamppfiles/htdocs/Assign2/inc/header.php on line 16 ?>

which is saying that there is something wrong with line 16, line 16 is this:

[php]
//this is line 16
header(“Location: …/pages/login.php”);
[/php]

[php]
?php
require “dbconnect.php”;

?>

<?php [/php] Does this space exist between the two php tags? That is considered output.

if i get rid of that space and do this

[php]<?php
{
require “dbconnect.php”;
}

if (isset($_SESSION['empID']))

{
// the user is logged in
echo “

You are logged in, {$_SESSION[‘empID’]}!

”;
}
else
{
//the user is not logged in
header(“Location: …/pages/login.php”);
//echo ‘Log in’;
}

?>[/php]

i get a browser error saying that it is redirecting and never ending

“Firefox has detected that the server is redirecting the request for this address in a way that will never complete.”

Well you have solved one problem but now you have a redirect loop.

You really need to post all your code from both pages.

Ok cool thanks heres the code from both my header.php page and my login.php page

header.php
[php]<?php
require “dbconnect.php”;

if (isset($_SESSION['empID']))

{
// the user is logged in
echo “

You are logged in, {$_SESSION[‘empID’]}!

”;
}
else
{
//the user is not logged in
header(“Location: …/pages/login.php”);
//echo ‘Log in’;
}

?>

Template

Products | Add Product | Customers

[/php]

login.php
[php]<?php
include “…/inc/header.php”;
?>

Login

Username:
Password:
<?php include "../inc/footer.php"; ?>[/php]

You are looking for a session variable $_SESSION[‘empID’] but I don’t see where you are calling session_start()?

session start is in the dbconnect.php which is reacquired in the header.php

dbconnect.php

[php]<?php
session_start();
mysql_connect(“localhost”, “root”, “”);
mysql_select_db(“assign2_damian_henley”);
?>[/php]

And where is $_SESSION[‘empID’] set?

I don’t believe there is ever a reason to have a redirect inside an include.

so should i put the redirect on every page to avoid this problem?

Well no, that isn’t going to solve your problem. From what I can see is if you load login.php you are guaranteed a loop because you include header.php (with the redirect) before ever allowing them to log in and set the session.

It’s hard to say where the problem is exactly because I’m not seeing all the code.

it works if i put the redirect on every page, take it out of the header.php and and out of the login.php so there is no redirect on the login.php page but i get the same error as i was having before :frowning:

im getting this again
Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/Assign2-test/inc/header.php:16) in /Applications/XAMPP/xamppfiles/htdocs/assign2-test/pages/show-customers.php on line 11

Sponsor our Newsletter | Privacy Policy | Terms of Service