Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Sep 2011
    Location
    Blackpool
    Posts
    55
    Thanks
    7
    Thanked 1 Time in 1 Post

    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:
    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'); 
    ?>
    Subscribe to a channel dedicated to helping people learn HTML, PHP & CSS.
    http://www.youtube.com/user/RanTutorials

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,609
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,299
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    New Coder
    Join Date
    Sep 2011
    Location
    Blackpool
    Posts
    55
    Thanks
    7
    Thanked 1 Time in 1 Post
    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'); 
    ?>
    Subscribe to a channel dedicated to helping people learn HTML, PHP & CSS.
    http://www.youtube.com/user/RanTutorials

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,299
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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)
    Last edited by tangoforce; 08-28-2012 at 09:07 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #6
    New Coder
    Join Date
    Sep 2011
    Location
    Blackpool
    Posts
    55
    Thanks
    7
    Thanked 1 Time in 1 Post
    Quote Originally Posted by tangoforce View Post
    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)
    I sorted it. Thanks
    Subscribe to a channel dedicated to helping people learn HTML, PHP & CSS.
    http://www.youtube.com/user/RanTutorials

  • #7
    New Coder
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post
    Here is a easy fix:

    <?php

    ob_start();

    ?>

    Just add that before:

    <?php

    session_start();

    ?>

    I hope i helped.

  • #8
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,376
    Thanks
    263
    Thanked 32 Times in 31 Posts
    Also dont get in the habbit of using short tags ie: <? as depending on the server config they may not work. So its better to just get in the habbit now of always doing the full tag <?php every time..

    Glad you got it sorted out

  • #9
    New Coder
    Join Date
    Sep 2011
    Location
    Blackpool
    Posts
    55
    Thanks
    7
    Thanked 1 Time in 1 Post
    Quote Originally Posted by durangod View Post
    Also dont get in the habbit of using short tags ie: <? as depending on the server config they may not work. So its better to just get in the habbit now of always doing the full tag <?php every time..

    Glad you got it sorted out
    Thank you.

    I actually noticed that sometimes when using <? it didn't work but thanks for pointing that out aswell. I guess I get lazy sometimes.
    Subscribe to a channel dedicated to helping people learn HTML, PHP & CSS.
    http://www.youtube.com/user/RanTutorials


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •