php script issue. cant send header information?

Posted on 2004-11-03
Last Modified: 2013-11-18
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";

Question by:Caiapfas
    LVL 2

    Accepted Solution

    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.
    LVL 2

    Author Comment

    by:Caiapfas thanks....just got rid of it (didn't need it)

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now