Error in login page Warning:Cannot add header information...

Codes here !

Moderators: macek, egami, gesf

User avatar
wmdrumaizi
New php-forum User
New php-forum User
Posts: 40
Joined: Thu Jan 23, 2003 5:15 pm
Location: Malaysia
Contact:

Error in login page Warning:Cannot add header information...

Postby wmdrumaizi » Mon Apr 14, 2003 2:07 am

Hi guys,
I'm doing a login page for my website.
I got this error when I try to login:

"Warning: Cannot add header information - headers already sent by (output started at C:\apache\htdocs\researchers\loginok.php:4) in C:\apache\htdocs\researchers\loginok.php on line 87"

Where line 87 is:
header( "Location: http://localhost/researchers/login.php" );

Pages used are as follow:
login.php (User enter username and password)
loginok.php (process the login, give responds)

Here is my code:

Code: Select all

<?php

//check that the user is calling the page from the login form and not accessing it directly
//and redirect back to the login form if necessary
if (!isset($username) || !isset($pass))
{
header( "Location: http://localhost/researchers/login.php" );
}

//check that the form fields are not empty, and redirect back to the login page if they are
elseif (empty($username) || empty($password))
{
header( "Location: http://localhost/researchers/login.php" );
}
else
{
//convert the field values to simple variables

//add slashes to the username and md5() the password
$user = addslashes($_POST['username']);
$pass = md5($_POST['pass']);


//set the database connection variables

$dbHost = "localhost";
$dbUser = "";
$dbPass = "";
$dbDatabase = "researchers";

//connet to the database

$db = mysql_connect("$researchers", "", "") or die ("Error connecting to database.");

mysql_select_db("$researchers", $db) or die ("Couldn't select the database.");

$result=mysql_query("select * from maintable where username='$username' AND pass='$pass'", $db);

//check that at least one row was returned

$rowCheck = mysql_num_rows($result);
if($rowCheck > 0)
{
while($row = mysql_fetch_array($result)){

  //start the session and register a variable

  session_start();
  session_register('username');

  //successful login code will go here...
  echo 'Success!';

  //we will redirect the user to another page where we will make sure they're logged in
  header( "Location: checkLogin.php" );

  }

  }
  else {

  //if nothing is returned by the query, unsuccessful login code goes here...

  echo 'Incorrect login name or password. Please try again.';
  }
  }
?>


Could someone please give me an idea to solve this error, any help would be appreciated.

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Mon Apr 14, 2003 5:22 am

you can't have any html output (e.g. echo) infront of a header() command

//successful login code will go here...
echo 'Success!';

//we will redirect the user to another page where we will make sure they're logged in
header( "Location: checkLogin.php" );

that's your code. If you remove "echo 'Success!';

it'll work fine :)

Andrew


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 2 guests