[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

php script issue. cant send header information?

Posted on 2004-11-03
2
Medium Priority
?
374 Views
Last Modified: 2013-11-18
ok...
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 pass..it works right and redirects


login.php========================

<?php

$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;
        break;
      }
    }
  }
} 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>
  <center>
<table border=0 cellpadding=0 cellspacing=0 style=border-collapse: collapse bordercolor=#111111 width=300 height=1>
  <tr>
    <td width=105 height=1>
<b><font face=Verdana size=2>User Name: </font></b>
   </td>
    <td width=195 height=1> <input name=user size=20></td>
  </tr>
  <tr>
    <td width=105 height=22><b><font face=Verdana size=2>Password:
    </font></b></td>
    <td width=195 height=22> <input type=password name=pass size=20></td>
  </tr>
  <tr>
    <td width=105 height=26>&nbsp;</td>
    <td width=195 height=26>
<input type=submit value=Enter></td>
  </tr>
</table></center>
</div>
</form>

  <?php
}

if ($login_good) {
  $_SESSION['logged_on'] = true;  
  header("Location: https://www.mydomain.com/lockedpage.php"); //set this URL to where you wish to redirect
  exit;
} elseif (!$login_untested) {
//  header("Location: https://www.mydomain.com/?errmsg=You must login");
  echo "<center><b><font color=red>Username or Password incorrect</font></b></center><br />";
session_unset();
}
?>
</body>
</html>


========================================


lockedpage.php=================
<?php
session_start();
if(!$_SESSION['logged_on']){
      echo "You have not been logged in";
      header("Location: https://www.mydomain.com/?errmsg=You must login");
      //include "login.php";
      exit();
}
?>

=====================================
0
Comment
Question by:Caiapfas
2 Comments
 
LVL 2

Accepted Solution

by:
gslaven earned 2000 total points
ID: 12486591
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.
0
 
LVL 2

Author Comment

by:Caiapfas
ID: 12487563
boom.....fast...much thanks....just got rid of it (didn't need it)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Screencast - Getting to Know the Pipeline
Suggested Courses

834 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