I am fairly new to this site and have a question about a problem that I have recently. How to interact with a page on which we just made a redirection? That is, after validating a form, I would like to be able to redirect the user to the login page and show him a success message such as: Successful registration! But I can not do it.
3 Answers
Not the prettiest solution, but it will work.
The header function can be used to redirect a user to a page.
header("Location: myFile.php?message=success"); // success can be changed to fail
You can then use this code on the destination page to display the message accordingly.
if(isset($_GET['message'])) {
if($_GET['message'] == 'success') {
echo 'Registered successfully!';
} elseif($_GET['message'] == 'fail') {
echo 'Registration failed.';
}
}

- 2,630
- 3
- 20
- 38
To do a redirect use the header()
function:
header("Location: [you-url]");
To show a message I suggest you use $_SESSION[]
or $_GET[]
both of these variables are superglobal variables:
http://php.net/manual/en/language.variables.superglobals.php
Also remember if you use $_SESSION[]
then you have to put session_start()
at the beginning of each page...
TRY THIS ($_SESSION[]
session solution):
$_SESSION['message'] = "Successful Registration";
header("Location: [redirect-url]");
On the display page:
<?php
if(isset($_SESSION['message'])) {
echo $_SESSION['message'];
}
?>
TRY THIS ($_GET[]
solution):
header("Location: [redirect-url]?message=Successful%20Registration");
On the display page:
<?php
if(isset($_GET['message'])) {
echo $_GET['message'];
}
?>
You can change these up depending on if it failed or not!
-
Your `$_GET` solution won't work since you have a space in the parameter. You should change the space to `%20`. – rpm192 Nov 04 '18 at 19:39
-
-
You added it to the `$_SESSION` one, I was talking about the `$_GET` one. – rpm192 Nov 04 '18 at 20:11
-
I'd use $_SESSION variables to store the message you wish to display. Echo the contents of this $_SESSION variable when you want to show it and then UNSET() the variable once you're done

- 64
- 10