2

I have seen this script

<?php header("Location: secondpage.php", TRUE, 307); ?>

I have also seen this script

<?php header("refresh:2; url=secondpage.php"); ?>

I want something like the combination of both. I tried the script below, but didnt work. Is there a way this can be done?

<?php header("refresh:2; url=secondpage.php", TRUE, 307); ?>

I also tried the script below, but didnt work. Is there a way this can be done?

<?php header("refresh:2; url=secondpage.php", FALSE, 307); ?>

The point is to have the delay and still send the post on redirect.

eg.

FROM: firstpage.php

REDIRCT TO: secondpage.php (use $_POST from firstpage.php here)

REDIRCT AGAIN TO: thirdpage.php (use $_POST from firstpage.php here also)

kenneth_oe
  • 125
  • 1
  • 4
  • 12

2 Answers2

1

The Boolean Flag TRUE OR FALSE in the header() determines whether you want the current header() to replace a previous one. If you want multiple headers, set it to FALSE. By default, this is TRUE... This means any subsequent header() replaces the Previous One. The 3rd Parameter is the HTTP RESPONSE CODE.

To explicitly set the Response Code to 307, You can use PHP http_response_code($code). Combining the 2, You can have something like this:

    // EXPLICITLY SET RESPONSE CODE TO 307
    http_response_code(307); 

    // REFRESH & REDIRECT TO page.php  
    // IN FACT; THERE IS NO NEED FOR THE http_response_code(307) ABOVE
    // THIS ONE LINE DOES IT ALL IN ONE GO... 
    header("refresh:2; url=page.php", FALSE, 307);

If you increased the refresh to (say) 15 so that you have enough time to look into the the Browser's Web-Developer Tools (under the Networks Tab), You'd notice that the HTTP Response Code of 307 was indeed set & sent....

To pass some data from the current page to page.php, You have 2 Options. Pass the Data via $_GET like so:

<?php
    $url    = urlencode("page.php?user=web&password=develop‌​per&city=bronx");
    header("refresh:2; url={$url}", FALSE, 307);

Or You could use Session like so:

<?php

    //FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
    if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
        session_start();
    }

    // SET SOME DATA INTO THE $_SESSION (TO BE USED IN page.php, ETC.)
    $_SESSION['user']   = 'web';
    $_SESSION['pass']   = 'develop‌​per';
    $_SESSION['city']   = 'bronx';

    header("refresh:2; url=page.php", FALSE, 307);
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • Ok. But i am still unable to receive my POST variables on page.php even after setting the flag to FALSE. – kenneth_oe Aug 16 '16 at 05:52
  • 1
    If you want to POST data to `page.php`, there are 2 ways: You can either Use Session or Pass them as query strings to the header like so: `header("refresh:2; url=page.php?username=web&password=developer&city=new-york", FALSE, 307);` – Poiz Aug 16 '16 at 06:11
  • Ok. useful addition to the answer but more importantly i am trying to get my POST variables from my very first page(firstpage.php) to my third page(thirdpage.php). I would rather not use sessions. Please see my edited question if this helps to explain my problem. – kenneth_oe Aug 16 '16 at 06:51
  • I tried header("refresh:2; url=page.php?usernam‌​e='".$_POST['postVarFromFirstPage']."'", FALSE, 307); But this didnt work too – kenneth_oe Aug 16 '16 at 06:57
  • @ken ***You can't expressly eat your cake and still have it back;*** now, can you? *How did you plan to explicitly send http header & post some request simultaneously on a page which only was ON for 2 seconds...* In the end: you'd come to either accept the `$_GET` alternative or the `$_SESSION` – Poiz Aug 16 '16 at 06:57
  • @ken ***Where is the $_POST['postVa‌​rFromFirstPage'] coming from?*** Truth is: Only YOU can see what it is that you have over there. To assist, we may need to share the same Plate. Now, where is that value `$_POST['postVa‌​rFromFirstPage']` declared: Before the Header? After the Header? Try this at the very top of your page: `var_dump($_POST['postVa‌​rFromFirstPage']); exit;`. What did you get? – Poiz Aug 16 '16 at 07:03
  • i ended up making do with sessions. Thanks – kenneth_oe Aug 17 '16 at 21:19
0

Below header must be work

header("Refresh: 2; url=page.php");

If it is not working then make sure you have no echo statement after header.

Check this link:-

How to fix "Headers already sent" error in PHP

Using HTML

<meta http-equiv="refresh" content="2;URL=page.php">
Community
  • 1
  • 1
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42