Cannot modify header information - headers already sent

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1444 Views - Last Post: 04 April 2013 - 11:46 PM Rate Topic: -----

#1 AlexxanderX  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 13-January 12

Cannot modify header information - headers already sent

Posted 26 March 2013 - 09:32 AM

Hello. I have another error to resolve: I've changed the hosting and with that I've changed and the version of PHP to last one or most stable one and I was greeted with some errors. Some errors I resolved but steel are 2 or 1 that I can't figure it how to solve.
PHP Error:
Warning: Cannot modify header information - headers already sent by (output started at /.../index.php:4) in /.../system.php on line 109

Warning: Cannot modify header information - headers already sent by (output started at /.../index.php:4) in /.../system.php on line 111


And here are the code, a part of it:
index.php
<!---Versiunea 1.0--->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="ro">
<?php include_once("protection.php"); // <- Here I include system.php - LINE 4
if ($_SESSION["user"] == "") setcookie($name, $_COOKIE[$name], time()-60);
if ($_SESSION["user"] == " ") setcookie($name, $_COOKIE[$name], time()-60); ?>
<head>
<title>...</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="..." />
<LINK REL=StyleSheet HREF="css/alexxanderx.css" TYPE="text/css" MEDIA=screen>
<LINK REL=StyleSheet HREF="css/button.css" TYPE="text/css" MEDIA=screen>
</head>
<body>
<table border="0" width="100%"><tr><td><div id='header-box'>Bine ai venit, 
<?php if ($_SESSION["user"] == "Administrator") { echo "<font color='red'>".$_SESSION["user"]."</font>"; }
else { echo $_SESSION["user"]; } ?></div></td></tr></table>
<table border="0" width="100%">
<tr><td style="width:200px; height:205px;"><div id='menu-box'><?php include("nav.php"); ?></div></td>
<td><div id='content-box'>
...
</div></td></tr></table>
<table border="0" width="100%"><tr><td><div id='footer-box'>...</div></td></tr></table>
</body>
</html>


system.php
<?php ob_start(); @session_start(); ?>
<?php
if (!function_exists('curPageURL'))
{
	function curPageURL() 
	{
		$pageURL = 'http';
		if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
		$pageURL .= "://>/>/>/";
		if ($_SERVER["SERVER_PORT"] != "80") 
		{
			$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		} 
		else {
			$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		}
		return $pageURL;
	}
}
$url = curPageURL();
define('TIMEOUT_MINUTES', 5);
$name = 'login';
$ltime = 60;
session_name($name);
//--
if(isset($_GET['logout'])) {
  setcookie($name, $_COOKIE[$name], time()-3600, '/'); // clear password;
  header("Location: http://".$_SERVER['SERVER_NAME']);
  exit();
}

if(isset($_GET['help'])) {
	die('Include following code into every page you would like to protect, at the very beginning (first line):<br>&lt;?php include("' . str_replace('\\','\\\\',__FILE__) . '"); ?&gt;');
}

if (!function_exists('showLoginForm'))
{
	function showLoginForm($error_msg)
	{
		?>
		<head>
			<title>...</title>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<meta name="author" content="..." />
			<LINK REL=StyleSheet HREF="css/alexxanderx.css" TYPE="text/css" MEDIA=screen>
			<link href="css/button.css" media="screen" rel="stylesheet" type="text/css" />
		</head>
		<body>
			<div id="t1">
			<table border="1" style="margin-left:auto; margin-right:auto;">
			<form name="login" method="post">
				<tr>
					<th><h3>LOGIN</h3></th>
				</tr>
				<?php if ($error_msg != "")
				{
					echo '<tr><td>' . $error_msg . '</td></tr>';
				} ?>
				<tr>
					<td><label for="username">Username: </label>
                        <input type="text" name="username" id="username" value="" />
					</td>
				</tr>
				<tr class="alt">
					<td><label for="password">Password: </label>
                        <input type="password" name="password" id="password" value="" />
					</td>
				</tr>
				<tr>
					<center><td><input type="submit" name="submit" value="Submit" class="abutton" /></td><center>
				</tr>
			</form>
			</table>
			</div>
		</body>
		<?php die();
	}
}

if(isset($_GET['login'])) showLoginForm("");

if ($_POST["submit"])
{
	if (!$_POST["username"])
	{
		...
	}
	else
	{
		if (!$_POST["password"])
		{
			...
		}
		else
		{
			include_once("include/connect.php");
			...
			if($result->num_rows > 0) 
			{
				...
				else
				{
					setcookie($name, $_COOKIE[$name], time() + $ltime, '/'); // LINE 109
					$_SESSION["user"] = $_POST["username"];
					header('Location: '.$url); // LINE 111
				}
			}
			else
			{
				showLoginForm("Username-ul nu exista!");
			}
		}
	}
}
else
{
	if (!isset($_COOKIE[$name]) || ($_SESSION["user"]=="") || ($_SESSION["user"]==" "))
	{
		showLoginForm("");
	}
ob_flush();
}


protection.php
<?php include_once("/.../system.php"); ?>


Thnaks for helping.

This post has been edited by AlexxanderX: 26 March 2013 - 09:33 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot modify header information - headers already sent

#2 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6084
  • View blog
  • Posts: 10,513
  • Joined: 28-September 07

Re: Cannot modify header information - headers already sent

Posted 26 March 2013 - 09:41 AM

It's because you've placed HTML before your PHP call.

Move your call to protection.php above your HTML and it should eliminate the 'Cannot modify header information - headers already sent' errors.
Was This Post Helpful? 2
  • +
  • -

#3 AlexxanderX  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 13-January 12

Re: Cannot modify header information - headers already sent

Posted 26 March 2013 - 12:03 PM

Tried:
<!---Versiunea 1.0--->
<?php include_once("protection.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="ro">
<?php ...

But same errors.

So I changed in:
<?php include_once("protection.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="ro">
<?php ...

And when click on Login button not working. Is redirectoring but I think he can't set the session/cookie. Why?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

Re: Cannot modify header information - headers already sent

Posted 26 March 2013 - 12:39 PM

setcookie() also needs to be called before any HTML.
Was This Post Helpful? 0
  • +
  • -

#5 AlexxanderX  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 13-January 12

Re: Cannot modify header information - headers already sent

Posted 27 March 2013 - 03:06 AM

And how to do that?

<?php ob_start(); @session_start(); ?>
<?php
...
if (!function_exists('showLoginForm'))
{
	function showLoginForm($error_msg)
	{
		?>
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="ro">
		<head>
			<title>IMO Database - Login</title>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<meta name="author" content="Prisacariu Alexandru" />
			<LINK REL=StyleSheet HREF="css/alexxanderx.css" TYPE="text/css" MEDIA=screen>
			<link href="css/button.css" media="screen" rel="stylesheet" type="text/css" />
		</head>
		<body>
			<div id="t1">
			<table border="1" style="margin-left:auto; margin-right:auto;">
			<form name="login" method="post">
				<tr>
					<th><h3>LOGIN</h3></th>
				</tr>
				<?php if ($error_msg != "")
				{
					echo '<tr><td>' . $error_msg . '</td></tr>';
				} ?>
				<tr>
					<td><label for="username">Username: </label>
                        <input type="text" name="username" id="username" value="" />
					</td>
				</tr>
				<tr class="alt">
					<td><label for="password">Password: </label>
                        <input type="password" name="password" id="password" value="" />
					</td>
				</tr>
				<tr>
					<center><td><input type="submit" name="submit" value="Submit" class="abutton" /></td><center>
				</tr>
			</form>
			</table>
			</div>
		</body>
		<?php die();
	}
}

if(isset($_GET['login'])) showLoginForm("");

if ($_POST["submit"])
{
	if (!$_POST["username"])
	{
		showLoginForm("Nu ai introdus nici un utilizator!");
	}
	else
	{
		if (!$_POST["password"])
		{
			showLoginForm("Nu ai introdus parola");
		}
		else
		{
			include_once("include/connect.php");
			$mysqli = dbConnect("read");
			$result = $mysqli->query("SELECT parola FROM users WHERE nume='$_POST[username]'") or die($mysqli->error.__LINE__);
			if($result->num_rows > 0) 
			{
				$row = $result->fetch_assoc();
				$pass = $_POST["password"];
				if (md5($pass) != $row['parola'])
				{
					showLoginForm("Parola este gresita!");
				}
				else
				{
					setcookie($name, $_COOKIE[$name], time() + $ltime, '/');
					$_SESSION["user"] = $_POST["username"];
					header('Location:'.$url);
				}
			}
			else
			{
				showLoginForm("Username-ul nu exista!");
			}
		}
	}
}
else
{
	if (!isset($_COOKIE[$name]) || ($_SESSION["user"]=="") || ($_SESSION["user"]==" "))
	{
		showLoginForm("");
	}
ob_flush();
}


First I need to show HTML: user need to introduce his pass and user and if is correct to set the cookie.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3462
  • View blog
  • Posts: 11,729
  • Joined: 12-December 12

Re: Cannot modify header information - headers already sent

Posted 27 March 2013 - 03:22 AM

Quote

First I need to show HTML

You need to re-write your code so that it doesn't output any HTML before setting a cookie or modifying headers.

You can set cookies, check GET-data, call PHP functions, etc., all before outputting a single line of HTML: you need to do this, otherwise it won't work. Set some variables during this process, such as $loggedIn, $userName, etc., so that you can then refer to these variables when creating, and outputting, your HTML.

Your code currently lacks variables. When you have checked (and validated) $_SESSION, $_GET, etc., data, store some of these values in new variables. Once you've created these variables it is no longer necessary to keep referring back to the original globals ($_SESSION, $_GET).
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

Re: Cannot modify header information - headers already sent

Posted 27 March 2013 - 03:22 AM

View PostAlexxanderX, on 27 March 2013 - 11:06 AM, said:

First I need to show HTML: user need to introduce his pass and user and if is correct to set the cookie.

but that are two calls to the server and you need to set the cookie on the second callís HEAD.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3462
  • View blog
  • Posts: 11,729
  • Joined: 12-December 12

Re: Cannot modify header information - headers already sent

Posted 27 March 2013 - 03:34 AM

BTW

if (!function_exists('showLoginForm'))

Why would the function not exist? If you've set-up, and included, the appropriate files then this function should be available. If it is not available then you need to examine your code to discover why.

function_exists() is generally used to discover if certain built-in functions are available, maybe due to a version-issues, or changes, on the web-server. It can be used with user-defined functions, but normally in more complex code than yours is currently.
Was This Post Helpful? 0
  • +
  • -

#9 AlexxanderX  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 13-January 12

Re: Cannot modify header information - headers already sent

Posted 27 March 2013 - 03:37 AM

View PostDormilich, on 27 March 2013 - 03:22 AM, said:

View PostAlexxanderX, on 27 March 2013 - 11:06 AM, said:

First I need to show HTML: user need to introduce his pass and user and if is correct to set the cookie.

but that are two calls to the server and you need to set the cookie on the second call’s HEAD.


I don't understand what you want to say :(/>

@andrewsw: I will try without function_exists()
RE @andrewsw: I tried but no difference.

This post has been edited by AlexxanderX: 27 March 2013 - 03:42 AM

Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

Re: Cannot modify header information - headers already sent

Posted 27 March 2013 - 03:45 AM

View PostAlexxanderX, on 27 March 2013 - 11:37 AM, said:

I don't understand what you want to say

didnít you ever asked yourself how cookies are transferred from PHP to the browser?
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3462
  • View blog
  • Posts: 11,729
  • Joined: 12-December 12

Re: Cannot modify header information - headers already sent

Posted 27 March 2013 - 03:53 AM

View PostAlexxanderX, on 27 March 2013 - 10:37 AM, said:

@andrewsw: I will try without function_exists()
RE @andrewsw: I tried but no difference.

I wasn't suggesting that removing this would fix your current issue, just that it should be unnecessary for you to use function_exists.
Was This Post Helpful? 0
  • +
  • -

#12 AlexxanderX  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 13-January 12

Re: Cannot modify header information - headers already sent

Posted 27 March 2013 - 04:38 AM

View PostDormilich, on 27 March 2013 - 03:45 AM, said:

View PostAlexxanderX, on 27 March 2013 - 11:37 AM, said:

I don't understand what you want to say

didnít you ever asked yourself how cookies are transferred from PHP to the browser?


No, but I searched but can't found nothing relevant.
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

Re: Cannot modify header information - headers already sent

Posted 27 March 2013 - 04:53 AM

then this is a good starting point.

and in case you want to know what HTTP is (and how it works): this link.

This post has been edited by Dormilich: 27 March 2013 - 04:54 AM

Was This Post Helpful? 1
  • +
  • -

#14 AlexxanderX  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 13-January 12

Re: Cannot modify header information - headers already sent

Posted 01 April 2013 - 06:45 AM

I read some parts from link but can't find a solution to my problem. I added some lines to my code above the if($_POST["submit"]) - line 52 from my last posted code and every time I click on submit button I see FALSE:
if(isset($_COOKIE[$name]))
{
    echo 'TRUE';
}
else{
    echo 'FALSE';
}

So my cookie can't set the value. Please if can help me.


I deleted the line 81 - header() - and it's working! :D

This post has been edited by AlexxanderX: 01 April 2013 - 06:51 AM

Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

Re: Cannot modify header information - headers already sent

Posted 01 April 2013 - 06:54 AM

well, at first $name must be defined. other than that I recommend to use var_dump() to check $_COOKIEís contents
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2