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.
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
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
deceze♦deceze
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']];
}
Dharman♦
28k21 gold badges75 silver badges127 bronze badges
answered Aug 16, 2012 at 13:22
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
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