PHP Session-Based Flash Message

PHP Session-Based flash message

I just solved my problem by adding exit after redirecting user to escape the execution of the register page, so the session won't be unset in the current page before using it in the next page.

Cookie vs. Session based flash message

The problem with a cookie is that the user may disable this functionality. If so, your flash message won't be showed. CakePHP try to be general enough and uses session storage.

You have 3 options:

  1. Session: the most used approach. It will work in any client computer but, as you say, it could give problems with some server configurations.
  2. Cookies: it's a good option in general, but the user may block this mechanism. Only recommendable when the your app requirements include the need of cookies.
  3. Data base: the universal solution. The problem is that it requieres an access to the database (slow). An ID should be passed with the URL (GET method) so the application knows which database register corresponds to this access.

In my applications I use a combination of the 2nd and 3rd approaches: I test for cookies and if they are available, I use them. If not, I use database access, BUT I always cache the DB access in order to not query more than once for each message.

Display flash messages after submiting a php form

For checking that session started, can you change your code?

public function __construct(){
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
}

I think $_SESSION global variable always will set by default.

UPD.

And as I noticed, You first flash the messages and then redirect the page

UPD2

After quick rewiew I think your addNewPost() method must be like this

public function addNewPost()
{
$session = new Session();
$manager = new PostsManager($this->db);
if (isset($_POST['publish'])) {
if (empty($_POST['title']) || empty($_POST['header']) || empty($_POST['author']) || empty($_POST['content'])) {
$_SESSION['addPostDatas'] = $_POST;
$session->setFlash('"Title", "Header", "Author" and "Content are required and cannot be empty"');
header('Location: ' . $_SERVER['REQUEST_URI']);
exit();

} else {
$newpost = new Post([
'title' => $_POST['title'],
'header' => $_POST['header'],
'author' => $_POST['author'],
'date' => date("Y-m-d H:i:s"),
'content' => $_POST['content'],
'featuredImg' => $this->uploadImg()
]);
$manager->add($newpost); // Create a new post
unset($_SESSION['addPostDatas']);
$session->setFlash('The post was published !', 'success');
header('Location: index.php?p=blog');
exit();

}
} else {
$session->flash();
}
}

Notice that you must call $session = new Session(); $session->flash(); when you process your success request(index.php?p=blog)

Detect if a PHP exception occurred and output flash message based off that exception

You just move the error message setting lines from your "main" PHP code ...

session_start();
if (isset($_POST['submit_email'])) {
require_once 'views/Swiftmail.php';
}

to your Swiftmail.php file ...

try {
// Create a message
$message = (new Swift_Message($_POST['subject']))
->setFrom('ME@YOU.COM')
->setTo($finalEmailList) //Array of email address
->setBody($_POST['message'], 'text/html')
->setReplyTo('you@me.com');
$_SESSION['message']= "Email Sent Successfully";
} catch (Swift_RfcComplianceException $e) {
$_SESSION['email_error'] = "Invalid Email Address Entered";
}

FYI, most modern PHP frameworks have flash message components built in.



Related Topics



Leave a reply



Submit