Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-28-2012, 04:00 AM   PM User | #1
connormcwood
New Coder

 
Join Date: Sep 2011
Location: Blackpool
Posts: 31
Thanks: 6
Thanked 1 Time in 1 Post
connormcwood has a little shameless behaviour in the past
Cannot Modify Header Information

Hello people of codingforums I am having a problem with this "Cannot Modify Header Information". I know that extra blank lines can cause this problem but I just can't see where they are. They're bugging me an if you login here, you will receive the error. I dont seem to have this error on localhost but on my website I do?

This is the error:
Quote:
Warning: Cannot modify header information - headers already sent by (output started at /home/connor/public_html/game/includes/top.php:11) in /home/connor/public_html/game/login.php on line 18
Top.php:
Code:
<? 
if(session_id() == ""){ session_start(); } 
?><html><head>
<link rel="icon" href="images/favicon.ico" type="image/x-icon">
<title> Cyber States - A online state simulation game.</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<LINK REL=stylesheet HREF="style.css" TYPE="text/css">
<meta name="robots" content="all">
<meta name="revisit-after" content="7 days">
<meta name="author" content="connor mcgarty-wood">
<meta name="keywords" content="games, gaming, cyber, states, cyberstates, simulation, simulate, online, top, 100, interesting, internet, bored, game, windows, browser game, browser">
<meta name="description" content="A online simulation game built on the topic of owning your own state. It is online
and you simulate everyday and collect taxes. This is a very interesting simulation browser games.">
</head>
<body>
<div id="header">
<img src="images/cyberstates.jpg" ALT="Cyberstates">
</div>
<center>
/* adds */
</center>
<br />
Login.php
Code:
<? 
include('includes/top.php');
include('includes/menu.php'); 
$error="";
if(isset($_SESSION['username'])){
header('location:default.php');
}
if(isset($_POST['login'])) {
include('includes/connect.php');
$username = $_POST['username'];
$password = md5($_POST['password']);
$query = "select * from users where username='$username' and password='$password'";
$result = mysql_query($query);
if (mysql_num_rows($result) != 1) {
$error = "Your username or password was Incorrect.";
} else {
    $_SESSION['username'] = $_POST['username'];
	header('Location: loggedin.php');
} } 
?>
<div id="rightbox">
<H2>Login</H2>
<? 
if(!$error == ""){
echo "<div class='error_message'>";
echo "$error";
echo "</div>";
} 
?>
<center>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<p> Login into the game below or register <a href="register.php" title="register here">here</a>. You need to allow cookies and javascript, some firewalls
may block cookies. </p>
<p> Username: <input name="username" id="username" title="Username" value="" size="30" maxlength="16" /><br /></p>
<p> Password: <input name="password" id="password" type="password"  title="Password" value="" size="30" maxlength="24" /><br /></p>
<input type="submit" name="login" value="Login" title="Login into the Game">
</form>
</center>
<? 
include('includes/bottom.php'); 
?>
__________________
Google was late to search. Facebook was late to social networking. Apple was late to the MP3 player. It's never too late. Just do it better
My website: http://www.connormcwood.com/forum/
connormcwood is offline   Reply With Quote
Old 08-28-2012, 08:15 AM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Change

Code:
<? 
include('includes/top.php');
include('includes/menu.php'); 
$error="";
if(isset($_SESSION['username'])){
header('location:default.php');
}
to

Code:
<? 
$error="";
if(isset($_SESSION['username'])){
header('location:default.php'); // this line needs to come
}
include('includes/top.php'); // before this line
include('includes/menu.php');
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 08-28-2012, 11:09 AM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,515
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Please see the link in my signature about headers already sent.

The short version of the long story is:
Headers are sent BEFORE any html / text output. Therefore if you send text / html BEFORE a header, your header can not be sent.

In your top.php you have a lot of html output - you then come back out of it into login.php and try to send a header! This will not work.

For more information, please see the headers link in my signature.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 08-28-2012, 08:43 PM   PM User | #4
connormcwood
New Coder

 
Join Date: Sep 2011
Location: Blackpool
Posts: 31
Thanks: 6
Thanked 1 Time in 1 Post
connormcwood has a little shameless behaviour in the past
Quote:
Originally Posted by felgall View Post
Change

Code:
<? 
include('includes/top.php');
include('includes/menu.php'); 
$error="";
if(isset($_SESSION['username'])){
header('location:default.php');
}
to

Code:
<? 
$error="";
if(isset($_SESSION['username'])){
header('location:default.php'); // this line needs to come
}
include('includes/top.php'); // before this line
include('includes/menu.php');
I've done this and the error is still there:/.
Code:
<? 
$error="";
if(isset($_SESSION['username'])){
header('location:default.php');
}
include('includes/top.php');
include('includes/menu.php'); 

if(isset($_POST['login'])) {
include('includes/connect.php');
$username = $_POST['username'];
$password = md5($_POST['password']);
$query = "select * from users where username='$username' and password='$password'";
$result = mysql_query($query);
if (mysql_num_rows($result) != 1) {
$error = "Your username or password was Incorrect.";
} else {
    $_SESSION['username'] = $_POST['username'];
	header('Location: loggedin.php');
} } 
?>
<div id="rightbox">
<H2>Login</H2>
<? 
if(!$error == ""){
echo "<div class='error_message'>";
echo "$error";
echo "</div>";
} 
?>
<center>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<p> Login into the game below or register <a href="register.php" title="register here">here</a>. You need to allow cookies and javascript, some firewalls
may block cookies. </p>
<p> Username: <input name="username" id="username" title="Username" value="" size="30" maxlength="16" /><br /></p>
<p> Password: <input name="password" id="password" type="password"  title="Password" value="" size="30" maxlength="24" /><br /></p>
<input type="submit" name="login" value="Login" title="Login into the Game">
</form>
</center>
<? 
include('includes/bottom.php'); 
?>
__________________
Google was late to search. Facebook was late to social networking. Apple was late to the MP3 player. It's never too late. Just do it better
My website: http://www.connormcwood.com/forum/
connormcwood is offline   Reply With Quote
Old 08-28-2012, 08:53 PM   PM User | #5
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,515
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
session_start() before
if(isset($_SESSION['username'])){


Line 18 felgall, is this:
PHP Code:
} else {
    
$_SESSION['username'] = $_POST['username'];
    
header('Location: loggedin.php'); 
(The bit that comes after the html)
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 08-28-2012 at 09:07 PM..
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:18 PM.


Advertisement
Log in to turn off these ads.