Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing message after redirect

Tags:

redirect

php

I'm trying to show a message after a successful redirect in my project, but can't find a really good way to do so. Right now I have a very simple and not really good solution which looks like this:

// Things happened:
$Alert = dangerMessage("Heyho, I am a message!");
header('refresh: 1.5 ; url = index.php');

My dangerMessage is a simple echo of a predefined Alertbox:

function dangerMessage($text) {
    return "<div class='alert alert-danger alert-dismissable fade show mt-4'>
    <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>".$text."</div>";
}

It simply shows a little box with text inside.

Anyway, like I said this is my "simple" solution right now, it works fine but it's not the best solution. I know that you should be putting a die(); or a exit(); after the header(), but my problem is that when I put them under my header() the page is just blank for 1.5 seconds, redirects, and shows no messagebox.

Is there a way to have a decent/good redirect that also shows my message?

like image 633
NakedPython Avatar asked Feb 07 '18 08:02

NakedPython


People also ask

Is it possible to redirect a text message to another page?

I think No need of redirecting to same page. by the way, you can display the message either of the two ways. Method I: using message box. keep one Label and display message as Label Text.

How to show MSG when redirecting to page using session?

The secure way here is use session whenever you redirect to page. After at load time you just put code like this, In this way you can show msg and also remove session. You can also pass any secure message to session. But make sure to remove session variable. I will be back if any query. The content must be between 30 and 50000 characters. …

How to pass $message variable to $location in a redirect?

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: session_start (); $_SESSION ['message'] = 'success'; header ("Location: $location");


2 Answers

An elegant and common approach is to use a flash message. This is basically a one-off session entry which is set in one request and is delivered in the next one, then instantly deleted. Consider the following example.

On the redirecting page, you set the flash message in the session object:

$_SESSION["flash"] = ["type" => "success", "message" => "You are great!"];

On a target page, check if there’s a flash message. If so, output and delete it.

if (isset($_SESSION["flash"]))
{
    vprintf("<p class='flash %s'>%s</p>", $_SESSION["flash"]);
    unset($_SESSION["flash"]);
}

Note: The example assumes you are using PHP sessions already. If not, you must start the session first.

like image 70
lxg Avatar answered Oct 23 '22 03:10

lxg


I would advise you use a session to display your message. The issue with $_GET is that since you rely on the URL, anyone can constantly manipulate the message being displayed.

Before calling header(), save the success or failed message to a session variable. For example

$_SESSION['success'] = 'Data Saved to Database';

On the index page, check for the session key, echo it and then remove it from the session array.

if(!empty($_SESSION['success'])){
  echo $_SESSION['success'];
 unset($_SESSION['success']);//so you do not have to display it over and over again
}
like image 37
Rotimi Avatar answered Oct 23 '22 03:10

Rotimi