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:
- Session: the most used approach. It will work in any client computer but, as you say, it could give problems with some server configurations.
- 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.
- 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
How to Pass an Array of Pdo Parameters Yet Still Specify Their Types
Same Domain, Different Folder PHP Session
Laravel 4.1: How to Paginate Eloquent Eager Relationship
How to Get Around or Make PHP JSON_Decode Not Alter My Very Large Integer Values
PHP Sending Variables to File_Get_Contents()
Get the Values of 2 HTML Input Tags Having the Same Name Using PHP
Install PHP-Mcrypt on Centos 6
Write CSV to File Without Enclosures in PHP
Why Do I Need to Use a Popular Framework
Why Is My Number Value Changing Using Number_Format()
How to Use an Object Method as a Callback Function
Replace Values in a Uri Query String
Differencebetween the | and || Operators
Increase the Limit of File Upload Size in Heroku While Uploading to Dropbox
How to Overload Class Constructor Within Traits in PHP >= 5.4