Issue with header redirect

Discussion in 'PHP' started by guruguy, Jun 27, 2008.

  1. #1
    I have the following code that selects a random website from the database and redirects the user to the target website through our tracking system. It works fine locally however when running it on the production server it displays the IE7 stock standard error message (Internet explorer cannot display this page). Could anyone suggest how to rewrite this as to not run into the error.

    
    <?php
    require_once('../dbconnect.php');
    $query = "SELECT url FROM proxylist";
    $result = mysql_query($query);
    $numberofproxies = mysql_num_rows($result);
    $chosen_number = rand(1, $numberofproxies);
    $x = 0;
    while ($row = mysql_fetch_array($result)) {
        $x++;
    	  if ($x == $chosen_number)
    	       {
    		   header('Location: http://localhost/proxypot/ccd/index.php?xrl=http://www.' . $row['url']); // send to random url
    		   exit; // make sure the script exits after forwarding
               }
    		   }
    ?>
    
    PHP:
    I know that the file is in place on the production server as if I comment everything and add an echo line at the end there is no problem with loading

    Thanks
     
    guruguy, Jun 27, 2008 IP
  2. King Goilio

    King Goilio Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    33
    #2
    King Goilio, Jun 27, 2008 IP
    guruguy likes this.
  3. guruguy

    guruguy Active Member

    Messages:
    553
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thanks for that King Goillio, I feel so stupid for posting now. I have sent some green your way
     
    guruguy, Jun 27, 2008 IP
  4. King Goilio

    King Goilio Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    33
    #4
    thats ok glad to help
     
    King Goilio, Jun 27, 2008 IP
  5. itnashvilleCOM

    itnashvilleCOM Banned

    Messages:
    176
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    When you use header redirects, add this right after <?php

    
    ob_start();
    
    PHP:
    and this before ?>

    
    ob_end_flush();
    
    PHP:
    Then you can use headers all you want. If you use exit() or die() at the end of your script, ob_end_flush() comes just before that.
     
    itnashvilleCOM, Jun 28, 2008 IP
  6. guruguy

    guruguy Active Member

    Messages:
    553
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #6
    I just had a look up on those functions. If I am not intending to output anything should I still use ob_start() and ob_end_flush()?
     
    guruguy, Jun 28, 2008 IP
  7. itnashvilleCOM

    itnashvilleCOM Banned

    Messages:
    176
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yeah, it just buffers it for you. It will prevent messages like "HEADERS ALREADY SENT".
     
    itnashvilleCOM, Jun 28, 2008 IP