Jump to content

Cannot modify header information - headers already sent by Problem


Ziph

Recommended Posts

Dear PHP Freaks,

 

Im trying to create a Login system for a little website.

To start with im pretty noob at php only started today tho im not stupid so i pretty much understands what everything does tho i dont understand what the code exaclty does.

 

I encountered this error,

Warning: Cannot modify header information - headers already sent by (output started at /home/gosuh0/public_html/school/checklogin.php:9) in /home/gosuh0/public_html/school/checklogin.php on line 42

 

I checked on whitespaces behind my <?php and ?> so thats not it

 

<?php

ob_start();

$host="localhost"; // Host name

$username="********"; // Mysql username

$password="******"; // Mysql password

$db_name="********"; // Database name

$tbl_name="members"; // Table name

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

// Define $myusername and $mypassword

$myusername=$_POST['myusername'];

$mypassword=$_POST['mypassword'];

 

// To protect MySQL injection (more detail about MySQL injection)

$myusername = stripslashes($myusername);

$mypassword = stripslashes($mypassword);

$myusername = mysql_real_escape_string($myusername);

$mypassword = mysql_real_escape_string($mypassword);

 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

$result=mysql_query($sql);

 

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

 

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

$_SESSION["myusername"] = $myusername;

$_SESSION["mypassword"] = $mypassword;

header("location:login_success.php");                                    //This is the line whats it all about i guess.

}

else {

echo "Wrong Username or Password";

}

 

ob_end_flush();

?>

 

:D

1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

2<html xmlns="http://www.w3.org/1999/xhtml">

3<head>

4<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

5<title>Login Check</title>

6</head>

7

8<body>

9<?php

 

ye there are no whitespaces before or behind <?php none behind <body> just 1 enter thats it and even if i remove the enter so line 8 becomes <body><?php the error still apears

 

first sentence:

i've seen this error pop up time and time again, and the answer is ALWAYS the same.  please look here before you post it.  the problem is you are outputting to the browser (whitespace included) before sending a header.  this is unallowed.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.