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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts

    "Headers already sent" Cookie Problem

    I'm getting the error that "headers were already sent" in my php prog for each time I try to call or even delete a cookie.

    Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/testzone/newLoginTest.php:80) in /home/nathan/public_html/testzone/newLoginTest.php on line 30

    Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/testzone/newLoginTest.php:80) in /home/nathan/public_html/testzone/newLoginTest.php on line 31

    Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/testzone/newLoginTest.php:80) in /home/nathan/public_html/testzone/newLoginTest.php on line 32

    Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/testzone/newLoginTest.php:80) in /home/nathan/public_html/testzone/newLoginTest.php on line 33

    Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/testzone/newLoginTest.php:80) in /home/nathan/public_html/testzone/newLoginTest.php on line 89
    PHP Code:
    function login($username$password$type$hidden)
    {
    mysql_connect("localhost""*********""*********") or die("Error inputting information to the database. Please try again later, or email us at excalo@cox.net. Sorry!");
    mysql_select_db("********") or die("Error connecting to the database. Please try again later, or email us at excalo@cox.net. Sorry!");

    $unames=array();
    $res mysql_query("SELECT username FROM users");
    while (
    $row mysql_fetch_row($res)) {
    array_push ($unames$row[0]);
    }

    $passes=array();
    $res2 mysql_query("SELECT password FROM users");
    while (
    $row mysql_fetch_row($res2)) {
    array_push ($passes$row[0]);
    }

    $numU count($unames);

    for(
    $i=0$i<$numU$i++)
    {
    if(
    $unames[$i]==$username)
    {
        if(
    $passes[$i]==$password)
        {
            if(
    $type=="auto")
            {
            
    $inTwoMonths 60 60 24 30 12 10 time();
            
    setcookie("loggedin""true"$inTwoMonths);
            
    setcookie("username"$unames[$i], $inTwoMonths);
            
    setcookie("password"$passes[$i], $inTwoMonths);
            
    setcookie('content',$hidden,1);
            }
        }
        else if(
    $password=="")
        {
            if(
    $type=="auto")
            {
            
    setcookie("loggedin"""time()-3600);
            
    setcookie("username"""time()-3600);
            
    setcookie("password"""time()-3600);
            
    setcookie('content','Access denied!',0);
            }
            elseif(
    $type=="manual")
            {
            echo 
    "No password given!";
            }
        }
        else if(
    $passes[$i]!=$password)
        {
            if(
    $type=="auto")
            {
            
    setcookie("loggedin"""time()-3600);
            
    setcookie("username"""time()-3600);
            
    setcookie("password"""time()-3600);
            
    setcookie('content','Access denied!',0);
            }
            elseif(
    $type=="manual")
            {
            echo 
    "Incorrect password!";
            }
        }
    $uGood="true";
    }
    }

    if(!
    $uGood && $username!="")
    {
    echo 
    "Incorrect username!";
    }
    else if(!
    $ugood && $username=="")
    {
    echo 
    "No username given!";
    }
    }

    if (
    $_COOKIE['loggedin'] && $_COOKIE['username'] && $_COOKIE['password'])
    {
    echo 
    "um, hi!";
    login($_COOKIE['username'], $_COOKIE['password'], 'auto''YO!');
    }
    else
    {
    echo 
    "um, bye.";
    }

    echo 
    $_COOKIE['content'];
    setcookie("content"""time()-3600); 
    Any ideas?
    Oh yeah, its the beggening ideas of a login system I'm designing, if that matters at all.

  • #2
    New Coder
    Join Date
    Mar 2008
    Location
    Lakeland, FL
    Posts
    39
    Thanks
    1
    Thanked 3 Times in 3 Posts
    a space before your <?php tags or a space after your ?> tags in your initial includes can cause this problem. Your not alowed to output anything at all even whitespace before all your headers are sent and yes cookies and session_start are included in the headers.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    output started at /home/nathan/public_html/testzone/newLoginTest.php:80
    What's on line 80?

  • #4
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    What's on line 80?
    Code:
    echo "um, hi!";
    I was trying to start simple, to just echo something if all the cookies required for the login were set. That part worked. An echo shouldn't disallow cookies from being called, should it?
    a space before your <?php tags or a space after your ?> tags in your initial includes can cause this problem.
    I checked, no whitespaces, no characters no nothing before or after the php tags (the whole document is just the block of php in the first post).

  • #5
    Regular Coder GO ILLINI's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    634
    Thanks
    0
    Thanked 7 Times in 7 Posts
    yes the echo is causing the problem.
    Nothing can be printed to the page before you use the session,cookie,or headers commands.
    Remove that and all should be well.

    -Adam
    Why not thank me?

    http://adamsworld.name

  • #6
    New Coder
    Join Date
    Jan 2008
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts
    just put your header on top of the page before anything prints.

  • #7
    New Coder
    Join Date
    Mar 2008
    Location
    Lakeland, FL
    Posts
    39
    Thanks
    1
    Thanked 3 Times in 3 Posts
    You can also look into output buffering or setting variables to echo at the end after your if statements are done. Outputting anything at any point ends your ability to set anymore headers because they are already sent to the browser.

  • #8
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    Alright, sorry I haven't posted for a couple days; I was busy. I got rid of the echo and *viola!* it worked! Thank you to everyone who responded on this post; I really appreciate it.


  •  

    Posting Permissions

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