1

I have a simple page in HTML/CSS/PHP that connects to MySQL DB.

"index.php" is loaded and "mainPage::showSectionLogin($_SESSION['login'])" shows logging form

<?php session_start(); ?>
<?php require_once 'clMainPage.php'; ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML>
  <HEAD>
    <?php mainPage::setSectionHEAD() ?>
    <LINK rel="stylesheet" type="text/css" href="style.css">
  </HEAD>
  <BODY>
    <DIV id="sidebar">
      <?php mainPage::showSectionLogin($_SESSION['login']) ?>
      <?php mainPage::showSidebarMenu($_SESSION['login']) ?>
    </DIV>
    <DIV id="main">
      <?php mainPage::showActualNews(5) ?>
    </DIV>
  </BODY>
</HTML>

"login.php" is executed after the logging form was filled

<?php session_start(); ?> 
<?php require_once 'clMainPage.php'; ?>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
  $dblink = mainPage::openDBconn();
  $result = mainPage::checkIfUserCanLogIn($dblink, $_POST['inpLogin'], $_POST['inpPassw']);

  if (mysql_num_rows($result) == 1) {
    $row = mysql_fetch_array($result);
    mainPage::logUserIn($row['login'], $row['passw']);
  }
  else
  {
    die("error checking user: there is no such user in a database");
  }

  mainPage::closeDBconn($dblink);
  header("refresh:1;url=index.php");
} ?>

I don't inderstand why, during logging in, "header("refresh:1;url=index.php");" (line:18) says that "require_once 'clMainPage.php';" in file "login.php" (line:2) sends headers. How is it possible that "require_once 'clMainPage.php';", that is a class declaratin containing only static functions, actually sends headers?

3
  • 1
    What's in clMainPage.php? You never know, whitespace or errors can cause headers to be sent... Commented Jan 18, 2011 at 9:39
  • consider the session_start() also sends the header Commented Jan 18, 2011 at 9:39
  • @Shakti Singh, session_start() does send headers, but you are able to do a header redirect after those headers are sent as nothing has been sent to the browser to be rendered. In this example the white space \r\n after the ?> was being sent to the browser. Commented Jan 18, 2011 at 9:48

2 Answers 2

5

There is white space after your closing php tag on line 1, that's what sends the headers

<?php
session_start();
require_once 'clMainPage.php';
if($_SERVER["REQUEST_METHOD"] == "POST") {
Sign up to request clarification or add additional context in comments.

3 Comments

Good catch - line breaks are whitespace too.
No problem @dygi, hard one to spot as there would be nothing to see in the browser either! Feel free to accept my answer. :)
Don't use closing tags at the end of php files, that's in Zend's conventions and coding standards document
1

Do you have any whitespace / output before / after your < ?php. This is often the cause.

What does 'clMainPage.php' contain?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.