Web Analytics Made Easy -
StatCounter php redirect not working - CodingForum


No announcement yet.

php redirect not working

  • Filter
  • Time
  • Show
Clear All
new posts

  • php redirect not working

    On my local Windows machine, the call, header("Location:test.php"); works fine, but when run on my host's unix server I get the error, "Warning: Cannot modify header information - headers already sent by (output started at /home2/...". PHP server code and php.ini's have been recently upgraded on both servers. Is there a php.ini setting that would enable/disable the function, header()?

    btw, the test.php file contains only..

  • #2
    For some reason (maybe the way Unix is interpreting a line feed or something) there is output prior to your header() call. The error msg you didn't fully give us provides for you the line number where that output begins.

    If you want to dump that output, rather than let it get in the way of your header() call, then use ob_start(), which dumps output to the buffer and flushes it if a header() is encountered.



    • #3
      Here's the complete error message and code. If you could explain how to fix this problem with or without ob_start I would be most appreciative.

      Warning: Cannot modify header information - headers already sent by (output started at /home2/eatyourp/public_html/mold/index.php:17) in /home2/eatyourp/public_html/mold/index.php on line 45

      PHP Code:
      // login.php




          Javascript Includes 
          <script src="../dome/ajax.js"></script>
          <script src="../sims/lib_pages/master.js"></script>
          <script src="../sims/ui_canvas.js"></script>
          <script src="../sims/lib_pages/panel_object.js"></script>
          <script src="../sims/lib_pages/panel.js"></script>
          <script src="../sims/lib_main/index_utils.js"></script>

          <script src="folder.js"></script>
          <script type="text/javascript" src="../jquery/jquery-1.3.2.js"></script>
          <script src="index_login.js"></script>


      <body bgcolor="#ffffff" link="#eecccc" vlink="99dd99" alink="5555aa">


      <?php include_once ("lib_user/priv.php");?>

      //header("Location:pg_photo_mess.html", false);
      $introPicts = new Folder("../mold_data/admin_photos");
      $HD->DrawImageWithID("intro_img"$introPicts->GetRandomImage(), 00true);

      $HD->DrawTextEditArea("Username or Email""user"$form->value("user"), 3001020228$form->error("user"), """""text");

      $HD->DrawTextEditArea("Password""pass"$form->value("pass"), 30501020140$form->error("pass"), """""password");
      $HD->DrawCheckBox("Remember Me""remember"false11010"#ffffff""");
      $HD->DrawLinkText("Forgot Password?"14052300""12normal"left""yellow""pg_forgotpass.php");
      $HD->DrawLinkText("New Member?  Sign Up!"16040300""12normal"left""yellow""pg_register.php");



      • #4
        All that HTML is being output to the browser. You cannot call header() after output has begun. It's just the way it is.

        As I mentioned, if you call ob_start(), then output is sent to a buffer instead of to the browser. Read the link I provided for more details on how ob_start() works and why it will help you out of your pickle.


        • #5
          I played with ob_start a bit but couldn't quite figure out how to make it work. In the interest of time, I concocted the following, which works fine. Thanks for your help. I'm sure I'll bump into a need to figure out ob_ functions eventually, but for now...

          echo "<script type='text/javascript'>";
          echo "location.href='pg_photo_mess.html';";
          echo "</script> ";


          • #6
            Doing a redirect that way will waste your hosting account's bandwidth because your pages is outputting content to the browser, then redirecting.

            It is always best to fix the actual problem rather than to apply a band-aid to cover it up.

            The best fix is to rearange your code so that the HTML content it is outputting is only output after the point where you do the header() redirect.

            The second best fix would be to use output buffering. Output buffering must be started before the content (the error message tells you where the content is that is being output.)
            Last edited by CFMaBiSmAd; Apr 8, 2009, 04:14 PM.
            Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.


            • #7
              I get your point and I agree. Thanks again.


              • #8
                I had this exact same problem,

                I added <? ob_start(); ?> to the top of my script and <? ob_flush(); ?> to the bottom and the problem went away.