-4

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 Answers3

0

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.';
    }

}
rpm192
  • 2,630
  • 3
  • 20
  • 38
0

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!

-1

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

Ollie Brooke
  • 64
  • 10