php script issue. cant send header information?

If a user goes to login.php then tries to bypass the page and go directly to lockedpage.php it gives errors on lockedpage.php
You have not been logged in
Warning: Cannot add header information - headers already sent by (output started at lockedpage.php:4) in lockedpage.php on line 5
instead of redirecting to the right page?
But if you don't goto login.php and try to go directly to lcokedpage.php it works and redirects you.
how can i fix this? meaning even if they goto login.php then try to by works right and redirects



$show_form = true;

if ($_POST['user']) {

  $given_user = $_POST['user'];
  $given_pass = $_POST['pass'];

  $htpasswd = '.htpasswd';
  $lines = file($htpasswd);

  $login_good = false;
  $login_untested = false; //This is the first visit to the page,
                           //no attempt has been made to login

  foreach ($lines as $line) {
    list($user, $pass) = preg_split("/\:/", $line);
    $pass = trim($pass);
    if ($given_user == $user) {
      if (crypt($given_pass, $pass) == $pass) {
        $login_good = true;
        $show_form = false;
} else {
  $login_untested = true;

if ($show_form) {

if (isset($_REQUEST[errmsg]))

<center><strong><font color="red"><? echo $_REQUEST[errmsg]; ?></font></strong></center>

<? } ?>
<form action="<? echo $PHP_SELF; ?>" method=post>
<div align=center>
<table border=0 cellpadding=0 cellspacing=0 style=border-collapse: collapse bordercolor=#111111 width=300 height=1>
    <td width=105 height=1>
<b><font face=Verdana size=2>User Name: </font></b>
    <td width=195 height=1> <input name=user size=20></td>
    <td width=105 height=22><b><font face=Verdana size=2>Password:
    <td width=195 height=22> <input type=password name=pass size=20></td>
    <td width=105 height=26>&nbsp;</td>
    <td width=195 height=26>
<input type=submit value=Enter></td>


if ($login_good) {
  $_SESSION['logged_on'] = true;  
  header("Location:"); //set this URL to where you wish to redirect
} elseif (!$login_untested) {
//  header("Location: must login");
  echo "<center><b><font color=red>Username or Password incorrect</font></b></center><br />";


      echo "You have not been logged in";
      header("Location: must login");
      //include "login.php";

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The problem is that you are outputing text to the browser before setting the headers.  

If you want to stay with this error_message "You have not been logged in"

Remove the echo portion of the script on lockedpage.php and echo $HTTP_GET_VARS(errmsg) on the login page. above the login.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CaiapfasAuthor Commented: thanks....just got rid of it (didn't need it)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.