tags:

views:

39

answers:

3

Hi, I have a PHP page with content that my users can view. When this page receives a POST request from a certain external URL, I'd like to redirect the user to another page.

The problems I'm' having are:

How can I monitor the page for requests being sent in an efficient way?

How can I actually redirect them since header() doesn't work.

Thanks.

+1  A: 

You can use an AJAX script to "ping" the server intermittently to see if there have been any changes. If there are, redirect the user with JavaScript.

It's pretty much the only way.

Aaron Harun
What would I ping? I can't predict when the external post comes.
Rebecca
The PHP file itself. You would have to store the external ping in a temporary file or a database until the client-side could retrieve it.
Aaron Harun
A: 

we can put this snippet to check the post request

if($_POST['flag']==1) {
          header("location:newpage.php");
          exit();
}

But If you want to check the request regularly without user interaction than you will have to use AJAX

Starx
Don't forget to call exit after calls to header('Location: ...').
Emil Vikström
+1  A: 

For the second question, header() will work if you use it before you output any information to the page. However, if you echo, print, or have HTML before it, it will give an error.

Edit: In response to Toad's comment, then you'd have to do as Aaron Harun suggested. Have the page save the $_POST data to the database or a file (make sure you sanitize it!), just like you would with any $_POST data. You would then need to use AJAX to get a response from a second PHP page that simply checks for the existence of updated data from wherever you saved it. If the response comes back true, then you redirect using a JavaScript redirect.

The only other way to do it without using AJAX would be to refresh the page using an HTML meta refresh element at timed intervals to check if $_POST data has been received. HTML would perform the refresh, PHP would do the checking, and you could use either for the redirect.

VirtuosiMedia
@virtuosimedia: The questions is not about the page being requested being redirected... It is about a page which is already rendered being redirected due to someone else hitting the same page
Toad