kenneth_oe
kenneth_oe

Reputation: 125

PHP Header refresh with post variables

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)

Upvotes: 2

Views: 6285

Answers (2)

Poiz
Poiz

Reputation: 7617

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);

Upvotes: 1

Ravi Hirani
Ravi Hirani

Reputation: 6539

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">

Upvotes: 0

Related Questions