Web Analytics Made Easy -
StatCounter Header giving error, but script still executing - CodingForum

Announcement

Collapse
No announcement yet.

Header giving error, but script still executing

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Header giving error, but script still executing

    Weirdness, my code below is simply to delete entries in a MySQL database. I am executing my code and then using Header("Location"); to take the user back to the page they came from. In other functions, it works fine and in others it doesn't. Like the one below
    PHP Code:
    function delete($id){
            
            
    $this->id $id;
            
    $this->connect();
            
    $query "DELETE FROM blog WHERE id='{$this->id}'";
            
    mysql_query($query) or die(mysql_error());
            
    $queery "DELETE FROM comments WHERE bid='{$this->id}'";
            
    mysql_query($queery) or die(mysql_error());
            
    Header("Location: main.php?mode=cp&cpmode=deletepost&id=$this->id");
        } 
    It's giving me an error as does when HTML is outputted before the header, but the script is still being executed and it's giving the header error.

    Does anyone know why it is giving the error on some and no errors on others?

    This code below gives no header error
    PHP Code:
        function editEntry($author,$topic,$email,$mood,$entry,$id){
            
            
    $this->author $author;
            
    $this->topic $topic;
            
    $this->email $email;
            
    $this->mood $mood;
            
    $this->entry $entry;
            
    $this->id $id;
            
            
    $this->connect("localhost","root","rootpass","test");    
            
            
    $this->emoticons();
            
    $this->parsed nl2br($this->parsed);
            
    $query "UPDATE blog SET author='{$this->author}', topic='{$this->topic}', email ='{$this->email}', mood='{$this->mood}', entry='{$this->parsed}', np='{$this->entry}' WHERE id='{$this->id}'";
            
    mysql_query($query) or die(mysql_error());
            
    Header("Location: main.php?mode=cp&cpmode=view");
        } 
    Anyone?
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #2
    Re: Header giving error, but script still executing

    PHP Code:
    function delete($id){
            
            
    $this->id $id;
            [...]
            
    mysql_query($queery) or die(mysql_error());
            
    Header("Location: main.php?mode=cp&cpmode=deletepost&id=$this->id");
        } 
    It's giving me an error as does when HTML is outputted before the header, but the script is still being executed and it's giving the header error.
    Of course it is being executed - all until the line you want to redirect with header().
    Btw, to stay standard compliant you have to use absolut urls redirecting with header().

    Does anyone know why it is giving the error on some and no errors on others?
    Because in some scripts you send http headers to the browser before you redirect and in others not. The error message gives you exactly the script and the line where the first http output has started. Have a look there.

    Greetz
    piz
    www.united-scripts.com
    www.codebattles.org

    Comment


    • #3
      Okay so the error is
      --------------------------------------
      Warning: Cannot modify header information - headers already sent by (output started at c:\server\htdocs\blog\admin\main.php:10) in c:\server\htdocs\blog\blog.php on line 315
      --------------------------------------

      and line 315 is
      PHP Code:
      Header("Location: main.php?mode=cp&cpmode=deletepost&id=$this->id"); 
      So how can I fix the error?
      Stevie Peele
      Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
      #dev - any programming,etc. question
      #design - design discussion and critque
      #central - general chat
      Come join us!

      Comment


      • #4
        Okay so the error is
        --------------------------------------
        Warning: Cannot modify header information - headers already sent by (output started at c:\server\htdocs\blog\admin\main.php:10) in c:\server\htdocs\blog\blog.php on line 315
        --------------------------------------
        Big enigma... which is the file and which is the line where the output started?

        and line 315 is
        PHP Code:
        Header("Location: main.php?mode=cp&cpmode=deletepost&id=$this->id"); 
        Of course, that's the line where the error comes up.
        www.united-scripts.com
        www.codebattles.org

        Comment


        • #5
          chances are that an error or simple warning is being generated somewhere in your script and that is causing the header error ...

          e.g.

          $yaks = mysql_num_rows($rubbish);

          if you do not die() on the mysql_error then a warning will be output causing the error , @mysql_num_rows($etc) would cure.

          another possible ..

          echo $var[unquoted_variable_name] ;

          will output a warning unless unquoted_variable_name is a defined constant & error reporting is set to show notices etc.

          find a situation where you get the header error & die() instead of setting the header and see what output exists.

          you can work around this issue with ob_start() but you are better off finding out what the issue is for further reference.
          resistance is...

          MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

          Comment


          • #6
            I'll keep looking into it, until then, I'll probably have to use a javascript redirect.

            My entire code is in the link in my sig, if someone is a quick spotter.

            Thanks
            Last edited by SDP2006; Feb 29, 2004, 07:47 AM.
            Stevie Peele
            Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
            #dev - any programming,etc. question
            #design - design discussion and critque
            #central - general chat
            Come join us!

            Comment


            • #7
              ok , adding
              ob_start();
              to the head of your script will overcome the header issue though thats a cheat rather than anything , when you call any mysql function add an @ before the call...

              if(mysql_num_rows($query)){

              can for instance throw a warning (if query was empty), which is output as far as the web-server is concerned, whilst

              if(@mysql_num_rows($query)){

              will not, also test with error_reporting(E_ALL) which may offer some other clues.

              ..............................................................
              <off_topic>

              whilst on the database theme .... you are connecting to mysql evey time you call $this->connect() , ok PHP will try and use the existing connection anyway but its still a wasted call , and your DB function should return a handle which you then reuse...

              e.g. (just the basics)

              PHP Code:
              <?
              class blah{
                function 
              blah(){
                  
              $this->db_link $this->_connect() ;
                }
                
                function 
              _connect(){
                   
              $link mysql_connect($host,$user,$pass) or die() ; 
                   
              mysql_select_db($database,$link) or die(mysql_error()) ; 
                   return 
              $link ;
                }
                
                function 
              some_other_function(){
                  
              //stuff
                   
              mysql_query$sql $this->db_link ) ;
                }

                function 
              an_other_function(){
                   
              mysql_query$sql $this->db_link ) ;
                }
              }
              ?>

              now many will also say that you should not have your sql calls in your classes in the first place and that rather you should access a seperate database class but thats another issue altogether.

              </off_topic>
              resistance is...

              MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

              Comment

              Working...
              X