Link to home
Start Free TrialLog in
Avatar of Opychus
Opychus

asked on

Redirecting with header("Location: ----"); when working with PHP over IIS

Hi!
This is my problem, just recently I have installed PHP on Windows XP using its IIS. Everything seems to be in order, but when I try to redirect from one page to another using header("Location: blabla.php");, nothing happens! The code simply stops executing when it comes to that line. Is there a way of forwarding to another page without using the header function?
Avatar of __macro__
__macro__

Try use that
header("Location: blabla.php"); //forward to another page
exit; //end the PHP processing
Avatar of Opychus

ASKER

No, that doesn't solve the problem. PHP automatically forwards to another page when it executes the header function, so the code that appears after that line is not relevant.
you could try using javascript at the end of the code:

echo "<SCRIPT>document.location='Blah.php'</SCRIPT>";

Once executed it should redirect.
i experience at my work a problem that the redirect gave a mozilla error page. the only way i success to solve the problem is to echo something after the redirection.

thus so
header("Location: blabla.php");
echo ' '; //echo a space or something
exit;
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

<html>
<?php
// This will give an error. Note the output
// above, which is before the header() call
header('Location: http://www.example.com/');
?>

Note: In PHP 4, you can use output buffering to get around this problem, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive on in your php.ini or server configuration files.
Avatar of Opychus

ASKER

echoing after header didn't do the trick nor did ob_start. Actually I think there is NO way around this problem when using PHP in combination with XP's IIS. This is even mentioned on php.net as a certified bug, something to do with the headers that IIS sends by default. Basically, what I need is to redirect to a page before it begins loading, depending on some variables that I send via URL. Here's sample code:

session_start();
include("scripts/stats/stats_counter.php");
if(!$_SESSION["currentgame"]{
   $_SESSION["currentgame"] = $game;
   header("Location: game_display.php");
}
It's dependent on if you're using HTTP/1.0 or 1.1.  If you're using 1.1, you MUST have an absolute URL.  That means a full http://www.server.com/file.php.  So, using your sample code, your statement should be as follows.

session_start();
include("scripts/stats/stats_counter.php");
if(!$_SESSION["currentgame"]{
  $_SESSION["currentgame"] = $game;
  header("Location: http://www.server.com/game_display.php");
  exit(); // Added for coding conventions.
}

If you wish, you can use the additional construction strings from the following URL to build your redirect.

http://www.php.net/manual/en/function.header.php

If the server and page name are not dependent on other pieces of code, then you won't need the variables.  But they're there if you need them.
You shouldn't call "header()" after any output. The header should be the first data send!!
So if there's any output before, it won't work.
I would use a javascript redirect instead of playing about with the header information. However what you are trying to do should work, i have no explaination why it doesnt. Generally as a workaround and to get it working, i would use JS as described above.
ASKER CERTIFIED SOLUTION
Avatar of csnyderLane
csnyderLane

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Opychus

ASKER

Yes, that is a great way to work around the header function without javascript! Thanks