tags:

views:

378

answers:

2

Firstly, this is not the headers already sent' problem.

I have an include file, that does the redirect. This works on every server I have tried it on except the production server, which runs windows.

When I run it on the production server, it only redirects the include file, not the entire page.

I have the main file, index.php:

<?php include('red.php'); ?>
<html ....
>
And I do the redirect to another page in red.php. Then the redirected page will show up at the top of the index.php page, with the rest of the index.php html file after this page. Have the mess up some setting in php.ini? After some more investigation, the problem is when I use a full URL rather than a relative URL. The first will only redirect the included file. (the problem I discovered above) while the second works correctly red2.php:

   header("Location: http://example.com/newfile.php");
    header("Location: newfile.php");'

+1  A: 

When I run it on the production server, it only redirects the include file, not the entire page.

That's a funny thing to say because it's not really possible. Only a single blob of data is presented to the browser you can't "redirect" part of it but you can include from multiple files in order to produce a composite blob.

Perhaps, this is what you're doing. Perhaps you're doing an include instead of a redirect. Remember a redirect is done like this:

header('Location: file.php');
exit;

The exit at the end is recommended so execution doesn't continue unnecessarily.

Ollie Saunders
related read up: http://thephpcode.blogspot.com/2009/01/why-exit-after-header-redirect.html
thephpdeveloper
The problem occurs when I use a full url 'header("Location: http://example.com/newfile.php");'
A: 

John,

I think you're confused on what happens on the client vs what happens on the server.

When you call include(), the server will search on its local file system for the file you're including, and will simply run through it and execute it line by line.

Now when you call the Header() function in php, this alters the header data that the browser will receive. The redirection will therefore be done at the browser level, and not the server level.

That means that having the following code doesn't really make sense:

<?php
# Doesn't matter if you call it through include() or directly
Header("Location: http://example.com/newfile.php");
?>
<html><p>Hello world</p></html>

It's like you're telling the browser: You'll need to redirect to newfile.php, but here's some HTML contents anyways for you to display.

If you want to include newfile.php on the server side, you need to use either Server Side Includes, CURL or just include("remote_file"); (you'll need to alter your security settings in php.ini for the latter to work if the remote_file is sitting on another server, namely allow_url_fopen)

Hope it helps

Wadih M.
No, I am not getting confused. I guess I didn't explain it very well.I am trying to include a file, which sometimes has html, sometimes it redirects.The problem is, that when I include a file using the full URL, not just the file name, when it redirects it doesn't redirect the entire page, it only redirects that include. So rather than showing completely different content on the page, it shows the completely differnet content at the top of the page, and the rest of the original file at the bottom.
"Redirect only the include" makes absolutely no sense. Unless your page is setup using frames or iframes, if you set a Header() redirect properly and exit right after the call, the new page will show no matter if you use relative or absolute path. Please post all your code.
Wadih M.
Yes, I agree, it makes no sense, hence the question.Test this yourself if you wantI have the main file including another file with the full URL:main.php:<?php include("http://example.com/include.php"); ?>include.php:<?php header("Location:http://example.com/new.php"); ?>and this will cause only the include to be redirected. Yes, it makes no sense, I understand.