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

Thread: Header Problems

  1. #1
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Header Problems

    Right again with the shoutbox problems :P..

    I have written some spam protection code
    and this is the last of it:

    PHP Code:
       if(!$spam){
        
    setcookie("shoutbox""11211111511610110033"time()+$total_secs);
        
    $insert mysql_query("INSERT INTO `[column]`.`[table]` (`id` ,`message` ,`author` ,`time` ,`ip`) VALUES (NULL, '{$message}', '{$user}', '{$shoutbox_date}', '{$ip}')");
        if(!
    $insert){
         echo 
    "Could Not Post Shout";
        } 
    but im getting this error:

    Warning: Cannot modify header information - headers already sent by (output started at [PATH]/top.php:2) in [path]shoutbox.php on line 38

    line 38 is the setcookie.

    Whats wrong with it? :s

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    On line 2 of top.php you're outputting something. Post top.php if you want specific help, but to avoid the error you're going to have to change it so it doesn't output anything or use output buffering.

  • #3
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    top.php, line 2:
    echo "<html>\n <head>\n <title>".$site['name']."</title>\n";

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Yeah, you can't echo/print/output anything before you set headers or cookies(because cookies use headers). You should be doing all of the processing and cookie setting before you display the page, but you can use ob_start() at the top of your page and ob_end_flush() at the bottom to avoid the headers already sent error by buffering all output until the page is fully generated.
    Last edited by Inigoesdr; 12-27-2007 at 01:35 AM.

  • Users who have thanked Inigoesdr for this post:

    Jesuspwnt (12-27-2007)

  • #5
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    still doesn't work with ob_start() and ob_end_flush();

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Post the code you're using.

  • #7
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    PHP Code:
    <?php
    // File: /includes/panels/shoutbox_panel/shoutbox.php

    ob_start();
    $short_date date("d/m/Y H:i");
    $user $_SESSION['username'];
    $ip $_SERVER['REMOTE_ADDR'];
    $shoutbox_date $short_date.":".date("s");
    $flood_time mysql_result(mysql_query("SELECT [field] FROM [column] WHERE id='1'"), 0);

    $time explode(":"$flood_time);
    $h str_replace("h"""$time[0]);
    $m str_replace("m"""$time[1]);
    $s str_replace("s"""$time[2]);

    $total_secs "";
    if(
    $h != ""){
     
    $total_secs $total_secs+($h*60*60);
    }
    if(
    $m != ""){
     
    $total_secs $total_secs+($m*60);
    }
    if(
    $s != ""){
      
    $total_secs $total_secs+($s);
    }
        
    if(isset(
    $_SESSION['username'])){
     echo 
    "<form method=\"post\" name=\"shoutbox\">\n<br />\n<textarea name=\"message\" class=\"shoutbox_ta\"></textarea>\n<br /><input type=\"submit\" value=\"Shout!\" name=\"shout_submit\"></form>";
     if(isset(
    $_POST['shout_submit'])){
      if(isset(
    $_POST['message'])){
       
    $message trim(htmlentities($_POST['message']));
       if(
    $message == ""){
        echo 
    "You Must Include Alpha Numeric Characters In Your Post for it to become valid";
        
    $spam=1;
       }
       if(
    $_COOKIE['shoutbox'] == "11211111511610110033"){
        
    $spam=1;
        echo 
    "Leave ".$total_secs." seconds Between Each Of Your Shouts";
       }
       if(!
    $spam){
        
    setcookie("shoutbox""11211111511610110033"time()+$total_secs);
        
    $insert mysql_query("INSERT INTO `[db]`.`[column]` (`id` ,`message` ,`author` ,`time` ,`ip`) VALUES (NULL, '{$message}', '{$user}', '{$shoutbox_date}', '{$ip}')");
        if(!
    $insert){
         echo 
    "Could Not Post Shout";
        }
       }
      }
     }
     echo 
    "<hr width=\"100%\" color=\"#00ff00\">";
    }

    $shouts mysql_query("SELECT * FROM shoutbox ORDER BY id DESC LIMIT 0,5");
    echo 
    "        <table>\n";
    while(
    $shout mysql_fetch_array($shouts)){
     echo 
    "         <tr>\n          <td>\n           <b>".$shout['author']."</b>\n";
      if(isset(
    $_SESSION['auth']) && $_SESSION['auth'] >= 103){
       echo 
    "[<font color=red><a href=\"/[admin panel]/shoutbox.php?action=delete&id=".$shout['id']."\">x</a></font>]";
      }
     echo 
    "<br />".substr($shout['time'], 0, -3);
     echo 
    "\n           <br />".$shout['message'];
     if(
    $_SESSION['auth'] > 102){
      echo 
    "<br />".$shout['ip'];
     }
     echo 
    "\n          </td>\n         </tr>\n";
    }
    echo 
    "        </table>";
    ob_end_flush();
    ?>

  • #8
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    In that code i dont see where you include top.php?

    The ob_start needs to be before you start outputting things, i.e. at least before the echo in top.php.

    Its best if you place it at the very top of your page so take into account any includes you make and confirm that all of them are after the ob_start.
    Mike

  • #9
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    the reason you don't see the script including top.php is because shoutbox.php is also an included page...

    Edit:
    YAY FIXED!!! WOOP WOOP
    Last edited by Jesuspwnt; 12-27-2007 at 01:35 PM.


  •  

    Posting Permissions

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