Reputation: 2759
I'm searching for the best way to supply the page after a redirect with a success messages. I think redirecting after a form was submitted is the best way to prevent a form to be submitted multiple times.
The following possibilites are some that I got in my mind. What do you think is the best of them and why or do you have other possibilities?
header('Location: index.php?success='.urlencode($message));
Supply more than the Location
header and read them on the target page like this
header('Location: index.php');
header('X-Success-Message: ' . urlencode($message));
Upvotes: 3
Views: 859
Reputation: 599
Keep your messages stored in an array or config file.
If you prefer to use sessions (or your framework's flash data), just store the key of the message you want to show.
If you prefer the GET solution, pass a parameter with the key of the message you want to show.
If you use GET and want to avoid users to bookmark a page with messages, you can use a boolean variable in session so you show the message only when this variables is set to 1.
Upvotes: 0
Reputation: 558
This is my opinion:
Session.
All data that you ar storing in session will be stored on the server. The default location is disk. IO - is a very limited resource. Do not start sessions when you do not need to. Do not store data in sessions when you do not need to.
Supply the new page with some GET parameters.
It is a good practice, but has one lack. If the client stores links in his favorites (and etc...) gives it to another user and he opens this link later, then he will see the success message.
Supply more than the Location header and read them on the target page.
It is a good method, but it depends on the server configuration and proxies in the middle of application and client. Sometimes the server configuration (proxies) kill non standard headers. But usually it is not a problem.
Store this data in cookies.
Lacks:
Store this data on the server.
Storing in database, memcache, NoSQL and etc. If this does not create additional load on the server and is ideally suited for your application, why not? And you need not necessarily to store the message itself, but only a state flag.
In practice I use all this methods. Which method should be used at the moment - look at the requirements for the application and its future use.
Upvotes: 1