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

Announcement

Collapse
No announcement yet.

php redirect not working

Collapse
X
 
  • 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..
    <?php
    phpinfo();
    ?>

  • #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.

    http://us.php.net/manual/en/function.ob-start.php

    Comment


    • #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:
      <?php
      // login.php

      include_once("../jquery/server/session.php");
      ?>

      <html>
      <head>

          <title>Mold</title>

          <!-- 
          =========================================
          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>

      </head>

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

      <?php 
      include_once("../jquery/server/constants.php");
      include_once(
      "../jquery/server/database.php");
      include_once(
      "folder.php");
      include_once(
      "html_draw.php");
      ?>

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

      <?php
          
      if($session->logged_in)
              {
              
      header("Location:test.php"false);
              
      //header("Location:pg_photo_mess.html", false);
              
      }
          else
              {
              
      $HD->SetMoreStyle("opacity:0;");
              
      $introPicts = new Folder("../mold_data/admin_photos");
              
      $HD->DrawImageWithID("intro_img"$introPicts->GetRandomImage(), 00true);
              
      $HD->BeginHiddenIDBox("login_box","25%""25%"400400);

              
      $HD->SetMoreStyle("");
              
      $HD->SetID("");
              
      $HD->BeginForm("lib_user/process.php""POST");
              
      $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->DrawInput("hidden""sublogin""1");
              
      $HD->DrawSubmitButton("Login"58,168,35,70);
              
      $HD->DrawLinkText("Forgot Password?"14052300""12normal"left""yellow""pg_forgotpass.php");
              
      $HD->DrawLinkText("New Member?  Sign Up!"16040300""12normal"left""yellow""pg_register.php");

              
      $HD->EndForm();
              
      $HD->EndBox();
              }    
      ?>
      </body>
      </html>

      Comment


      • #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.

        Comment


        • #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> ";

          Comment


          • #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.

            Comment


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

              Comment


              • #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.

                Comment

                Working...
                X