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 6 of 6
  1. #1
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    348
    Thanks
    54
    Thanked 0 Times in 0 Posts

    Weird Headers Error Message

    I have a PHP coded page that's housed inside of an HTML page and am getting a weird error having to do with headers on ONLY this page for some reason and can't figure out where it's coming from.

    The error says:

    "Warning: Cannot modify header information - headers already sent by (output started at /hermes/web01/b829/moo.rophoto1/sencha-tallyvote.php:7) in /hermes/web01/b829/moo.rophoto1/site/sencha/tally.php on line 44"

    I'm not sure what this means however. Here's the html code for the page [sencha-tallyvote.php]:

    PHP Code:
    <!DOCTYPE HTML>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Ruth Olson Photography</title>
        <link href="css/style.css" rel="stylesheet" type="text/css">
        <script type="text/Javascript" src="js/jquery-1.6.1.min.js"></script>
        <!--[if lt IE 9]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script type="text/Javascript" src="js/jqFancyTransitions.1.8.min.js"></script>
        <script type="text/Javascript" src="js/script.js"></script>
        <script type="text/javascript" src="js/prototype.js"></script>
        <script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
        <script type="text/javascript" src="js/lightbox.js"></script>
    </head>

    <body oncontextmenu="return false;">
        <header>
            <div id="logo">
                <a href="./index.php"><img src="./css/images/titleblock.png" alt="Ruth Olson Photography" /></a>
            </div>
            <nav>
                <?php include("./site/menu.php"); ?>
            </nav>
        </header>
        <div id="main-container">
            <div id="container">
                <div id="content">
                    <?php include("./site/sencha/tally.php"); ?>
                </div><!-- END CONTENT -->
                <div id="specs">
                    <?php include("./site/specs.php"); ?>
                </div><!-- END SPECS -->
                <div id="attn">
                    <?php include("./site/attn.php"); ?>
                </div><!-- END ATTN -->
            </div><!-- END CONTAINER -->
        </div><!-- END MAIN-CONTAINER -->
        <footer>
        </footer>
    </body>
    </html>
    ...and the coding for the [tally.php] file in the contents section:

    PHP Code:
    <?php
    require_once('./templates/mysql_connect.php');

    /////////////
    function escape_data ($data)
    {
        global 
    $dbc;
        
        if(
    ini_get('magic_quotes_gpc'))
        
    $data stripslashes($data);
        
        return 
    mysql_real_escape_string($data$dbc);    
    }
    ////////////

    $ip=$_SERVER['REMOTE_ADDR'];

    $queryIP "SELECT INET_NTOA('ip') FROM ips WHERE ip=INET_ATON('$ip')";
    $resultIP mysql_query ($queryIP);
    $existIP mysql_num_rows($resultIP);


    if (isset(
    $_COOKIE['vote']) || $existIP 0)
    {
        
    $message "<b>You have already voted in this year's Senior Challenge!</b> <p>To view the rankings of the current Senior Challenge contestants, <a href=\"/voteresults.php\">click here</a></p>";
    }
    else
    {
        
    //Get Senior info
        
    $query "SELECT * FROM seniors WHERE sen_id=$_GET[sen_id]";
        
    $result mysql_query ($query);
        
    $row mysql_fetch_array ($result);

        
    //Tally vote
        
    $votes $row['votes'] + 1;
        
    $queryTally "UPDATE seniors SET votes='$votes' WHERE sen_id=$_GET[sen_id] LIMIT 1";
        
    $resultTally mysql_query ($queryTally);
        
        if(isset(
    $resultTally))
        {
            
    $message "<b>Your vote has been registered!</b>  <p>Thank you for participating in the 2012 Senior Challenge.  To view the rankings of the current Senior Challenge contestants, <a href=\"/sencha-top10.php\">click here</a></p>";
            
            
    //Set cookie + ip
        
    setcookie ("vote""yes"time()+15552000);
        
    $queryInsertIP "INSERT INTO ips (ip, person) VALUES(INET_ATON('$ip'), '$row[last_name]')";
        
    $resultInsertIP mysql_query ($queryInsertIP);

        }
        else
        {
            
    $message "<b>Error:  Vote was not counted!</b>  <p>You have encountered an error, and your vote was NOT tallied.  Please, <a href=\"/contact.html\">contact us</a> to tally your vote.</p>";
        }
    }

    ?>

    <?php 
    $page_title 
    'Ruth Olson Photography - Vote Result';
    $query "SELECT * FROM seniors WHERE sen_id=$_GET[sen_id]";
    $result mysql_query ($query);
    $row mysql_fetch_array ($result);

    ?>

    <h3><?php echo $row['first_name'], " "$row['last_name'], " has <font style=\"color:#FF0000\">
    $row[votes]</font> votes"
    ;?></h3>

    <?php echo "<p>"$message"</p>"?>

    <br />
    <h3>Invite people to vote!</h3>
    <p>Increase <?php echo $row['first_name']?>'s chances of winning the Senior Challenge by inviting friends and family to vote!</p> 
    <form action ="<?PHP echo $_SERVER['PHP_SELF']; ?>?sen_id=<?php echo $row['sen_id']; ?>" method="post">
    <p>E-mail address: <input name="addy" type="text" size="30" maxlength="50" ></p>
    <input name="mail" type="submit" value="Send">
    </form> 

    <?php 
    //Email!

    if (isset($_POST['mail']))
    {
        if (empty(
    $_POST['addy']))
        {
            
    $e FALSE;
            
    $Pmessage .= '<p>Please enter the e-mail address...</p>';
        }
        
            else if(!
    eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$"stripslashes(trim($_POST['addy']))))
                {
                        
    $problem FALSE;
                        
    $Pmessage .= '<p>Please enter a valid e-mail address.</p>';
                }
                
        else
        
    $e escape_data($_POST['addy']);
        
        if (
    $e)
        {
        
        
    //Send an e-mail
                
    $to "$e";
                
    $subject "Vote for $row[first_name] $row[last_name] in the Ruth Olson Photography Senior Challenge!";
                
    $message "Hello,

    $row[first_name] $row[last_name] is a contestant in the Ruth Olson Photography Senior Challenge.  $row[first_name] currently has $row[votes] votes, but every new vote counts!

    If you would like to vote for $row[first_name], follow the link below...

    http://www.rutholsonphoto.com/sencha-vote-af.php

    Thank you,
    Ruth Olson Photography"
    ;
                
    $from "Senior Challenge@rutholsonphoto.com";
                
    $headers "From: $from";
                
    mail($to,$subject,$message,$headers);
                
                echo 
    "<br />The e-mail has been sent successfully!";
                            }
                            
                            else
                                echo 
    $Pmessage;
                        }    
    ?>
    Is there anything you guys can see that might be causing that message?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Its coming from your setcookie.
    You cannot push headers AFTER any output to the client since that doesn't make any sense. As soon as you've printed anything or dropped out of PHP and entered HTML mode, you have pushed your headers.
    The ways to fix this are to rewrite the code to change all output to variables and print them, rewrite to move all header calls to before any output, or cheat and use an output buffering by adding ob_start(); before any output.

  • Users who have thanked Fou-Lu for this post:

    Psionicsin (06-19-2011)

  • #3
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    348
    Thanks
    54
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Its coming from your setcookie.
    You cannot push headers AFTER any output to the client since that doesn't make any sense. As soon as you've printed anything or dropped out of PHP and entered HTML mode, you have pushed your headers.
    The ways to fix this are to rewrite the code to change all output to variables and print them, rewrite to move all header calls to before any output, or cheat and use an output buffering by adding ob_start(); before any output.
    I should have started off by saying that i didn't program any of this so, there, have no idea what you're talking about lol.

    Is there a quick and easy step-by-step you can give me for doing this?

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    What Fou-Lou is saying is this:

    PHP Code:
    <?
    //Start output buffering
    ob_start();
    ?>
    <!DOCTYPE HTML>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Ruth Olson Photography</title>
        <link href="css/style.css" rel="stylesheet" type="text/css">
        <script type="text/Javascript" src="js/jquery-1.6.1.min.js"></script>
        <!--[if lt IE 9]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script type="text/Javascript" src="js/jqFancyTransitions.1.8.min.js"></script>
        <script type="text/Javascript" src="js/script.js"></script>
        <script type="text/javascript" src="js/prototype.js"></script>
        <script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
        <script type="text/javascript" src="js/lightbox.js"></script>
    </head>

    <body oncontextmenu="return false;">
        <header>
            <div id="logo">
                <a href="./index.php"><img src="./css/images/titleblock.png" alt="Ruth Olson Photography" /></a>
            </div>
            <nav>
                <?php include("./site/menu.php"); ?>
            </nav>
        </header>
        <div id="main-container">
            <div id="container">
                <div id="content">
                    <?php include("./site/sencha/tally.php"); ?>
                </div><!-- END CONTENT -->
                <div id="specs">
                    <?php include("./site/specs.php"); ?>
                </div><!-- END SPECS -->
                <div id="attn">
                    <?php include("./site/attn.php"); ?>
                </div><!-- END ATTN -->
            </div><!-- END CONTAINER -->
        </div><!-- END MAIN-CONTAINER -->
        <footer>
        </footer>
    </body>
    </html>

    <?
    //End output buffering.
    ob_end_flush();
    ?>
    Basically when HTML is output, it causes the webserver to output information you can't see called headers. These can only be sent ONCE. Cookies are set in the headers so if you try to set a cookie after your headers have been sent (EG in the middle of your html) then it will produce that error.

    Output buffering works by storing anything your script tries to output to the browser in memory instead which allows the script to execute, do everything, any output is stored and then at the end you use ob_end_flush() to tell the PHP that it can finally send it all - Any modifications you made to the headers with output buffering turned on will be included.
    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!

  • Users who have thanked tangoforce for this post:

    Psionicsin (06-19-2011)

  • #5
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    348
    Thanks
    54
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    What Fou-Lou is saying is this:

    Basically when HTML is output, it causes the webserver to output information you can't see called headers. These can only be sent ONCE. Cookies are set in the headers so if you try to set a cookie after your headers have been sent (EG in the middle of your html) then it will produce that error.

    Output buffering works by storing anything your script tries to output to the browser in memory instead which allows the script to execute, do everything, any output is stored and then at the end you use ob_end_flush() to tell the PHP that it can finally send it all - Any modifications you made to the headers with output buffering turned on will be included.
    Gotcha. Thank you. I'm still only beginning PHP and should've mentioned that. I was totally lost, but thanks for the visual. That worked like a charm.

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    If it helps, I've created a new topic which explains this in more detail. The link is 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!

  • Users who have thanked tangoforce for this post:

    Psionicsin (06-19-2011)


  •  

    Posting Permissions

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