Redirect page with message in php

I want to redirect to a page and then display a message:

What I have is:

//succes            
$message = 'succes';
redirect_to('index.php');

On the index page I have:

if (!empty($message)) {
    echo '

'.$message.'

'; }

The redirect function is working fine:

function redirect_to( $location = NULL ) {
    if ($location != NULL) {
        header("Location: {$location}");
        exit;
    }
}

But it won't display my message. It's empty.

Redirect page with message in php

Dharman

28k21 gold badges75 silver badges127 bronze badges

asked Aug 16, 2012 at 13:19

0

By the time the redirect happens and the PHP script depicted by $location is executed, $message variable would have been long gone.

To tackle this, you need to pass your message in your location header, using GET variable:

header("Location: $location?message=success");

And

if(!empty($_GET['message'])) {
    $message = $_GET['message'];
// rest of your code

You could also have a look into sessions

session_start();
$_SESSION['message'] = 'success';
header("Location: $location");

then in the destination script:

session_start();
if(!empty($_SESSION['message'])) {
   $message = $_SESSION['message'];
   // rest of your code

answered Aug 16, 2012 at 13:22

Redirect page with message in php

Andreas WongAndreas Wong

58.5k19 gold badges105 silver badges123 bronze badges

0

Variables cease to exist after the script ends. Each separate request, each separate PHP script invocation is an entirely new context with no data from any other invocation.

Use sessions to persist data.

answered Aug 16, 2012 at 13:21

decezedeceze

497k81 gold badges719 silver badges867 bronze badges

You can use sessions

//succes            
$_SESSION['message'] = 'succes';
redirect_to('index.php');

And on index

if (!empty($_SESSION['message'])) {
    echo '

'.$_SESSION['message'].'

'; unset($_SESSION['message']); }

Redirect page with message in php

Dharman

28k21 gold badges75 silver badges127 bronze badges

answered Aug 16, 2012 at 13:22

Redirect page with message in php

Mihai IorgaMihai Iorga

38.6k15 gold badges107 silver badges106 bronze badges

Since you are running header("Location: {$location}"); the value of $location (set in the first file) is lost when index.php is loaded.

answered Aug 16, 2012 at 13:22

JocelynJocelyn

11k10 gold badges43 silver badges60 bronze badges

you can avoid redirect function and use this code in the page

header("Location: $locationpage?message=success")

index.php

if(!empty($_GET['message'])) {
$message = $_GET['message'];
 echo '

'.$message.'

'; }

answered Feb 21, 2018 at 16:15

Redirect page with message in php

use the following code....

header("Location: index.php?message=success");

in index.php

$msg=$_GET['message'];
echo $msg;

answered Aug 16, 2012 at 13:24

AnoopAnoop

9936 silver badges16 bronze badges

How will you redirect a page using PHP?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

How show success message from another page in PHP?

The best way to solve this problem is set a session message after the success of your operation in the process page. Then in the redirected page check whether the session message is set or not. If it is set then simply echo that message.

How do I redirect to another page in PHP w3schools?

Redirecting Browser You can redirect your user to some other page. php header("Location: http://www.example.com/"); ?> The following command will redirect the browser window to the given location as soon as the command is executed.

What is use of header () function in PHP?

The header() function is an predefined PHP native function. With header() HTTP functions we can control data sent to the client or browser by the Web server before some other output has been sent. The header function sets the headers for an HTTP Response given by the server.