PandemoniumSyndicate
PandemoniumSyndicate

Reputation: 2955

PHP Header does not redirect in iframe, no error shown

I have an iframe which does some server-side work and displays an image. We use the iframe in order to show image dynamically. The user hits upload image, we show a spinner, and reload the iframe with the new image. My goal is to avoid the confirm form resubmission page that displays if a user gets to the page via the 'back' button.

Essentially, the page which is the src of the iframe is a php file which checks against three conditions:

1) Was a variable named redirect set? If so, 'urldecode()' it, and use it as the img src.

2) Was a file uploaded? If so save it to the db, and redirect to this page with the image path as a GET variable

header('Location : mypage?redirect=path%2Fto%2Fimg.jpg'); 
die('upload');

3) Nothing was uploaded? Grab what we have from the db, then redirect to this page with the path as a GET variable

header('Location : mypage?redirect=path%2Fto%2Fimg.jpg'); 
die('default'); 

And what we have in the parent page is

<iframe id='myframe' src='mypage.php'>

Problem is, we never get a redirect. The page does die though, so instead of a profile pic, we see a white square with either the string upload or default from the die() call.

When using header() inside an iframe, does it behave as it does normally? I would like it to redirect the contents of the iframe.

Thanks for your time.

Upvotes: 2

Views: 4323

Answers (4)

Neil Hillman
Neil Hillman

Reputation: 365

Aside from your 'extra space' issue, PHP's header (location) does not work inside iframes!

(I couldn't find this documented, but I was struggling with it myself, so wrote a dummy script to test it, and it's true).

WORKS: When called "stand-alone", this will redirect to Google

<?php
header ('Location: http://www.google.com/');
exit ();
?>

DOESN'T WORK: When the above script is loaded in an iframe

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>test</title>
    <meta name="author" content="Neil Hillman">
</head>
<body>

<iframe src='./redirect.php'
    id='redirect'
    name='redirect'
    height='500'
    scrolling='auto'
    style='border: 1px solid grey;'>
</iframe>

</body>
</html>

Try it for yourselves!

Upvotes: 0

Abraham Covelo
Abraham Covelo

Reputation: 961

The best way to avoid form resubmission is use redirection after submit. Instead of process the data directly after the POST you should redirect the user browser. Take a look at this document for futher information:

http://en.wikipedia.org/wiki/Post/Redirect/Get

You can also have problems with specific browser and/or cache. Try to add these headers to avoid cache problems:

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1

header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

Upvotes: 2

spidEY
spidEY

Reputation: 944

I just replicated this in my testing environment. Your header is incorrect, there shouldn't be a space between Location and :

Works

header('Location: mypage?redirect=path%2Fto%2Fimg.jpg'); 
die('default'); 

Doesn't work

header('Location : mypage?redirect=path%2Fto%2Fimg.jpg'); 
die('default'); 

Upvotes: 4

axiomer
axiomer

Reputation: 2126

If I am right about what you do, I would do the following:

No redirect, just an <img src="path_to_img.jpg" /> where the path_to_img.jpg is the $_GET["redirect"]'s variable or if there is a problem, something based on that.

No IFrame, but a PHP include()

Upvotes: 1

Related Questions