1

I'm new to PHP. So i was wondering, is it a good idea to use $_SESSION to display errors/alerts?

For example: on submit.php you can set $_SESSION['error']="The error Text..." and on form.php you can use if isset($_SESSION['error']) then echo $_SESSION['error'] and then just unset the session. unset($_SESSION['error']).

It obviously works but is it a good idea? Can i face any problems in the future?

4
  • depends. do you need to check if an error has occurred in one page from another page? Commented Mar 11, 2014 at 9:54
  • why do you want this? you have to keep the error message all the way in session with you even if you dont need it in every page always.. Commented Mar 11, 2014 at 9:55
  • See stackoverflow.com/questions/7514172/… Commented Mar 11, 2014 at 9:55
  • It's save until your project raises to a multi-server environment without shared sessions ;-) But shared sessions isn't a problem for your admin of choice. Commented Mar 11, 2014 at 9:56

3 Answers 3

1

Yes this always helps me when ever I have to post the form to other .php file in custom PHP applications, I never faced any problem in it but I have left this practice a long ago because now a day with modern MVC framework you don't have to manage all such stuff as they are being managed by the Frameworks it self.

I will suggest you start using frameworks rather of managing each and everything your self, Code Igniter is a good getting started frameworks for the newbies...

Sign up to request clarification or add additional context in comments.

Comments

1

What if it's a log in error?

(Example:) When you go to another page which has the isset($_SESSION['error']) check, it will display the log-in error.

Unless you have different names for different errors ($_SESSION['error-login']), but this is not very conventional.

You could unset the session variable if it was successful, meaning that $_SESSION['error'] is no longer set, so that it doesn't appear on another page when the check is called.

There are plenty of frameworks out there that you can use that have built in error handling to make a job like this easier.

Here is a list of PHP frameworks that you could use.

Comments

1

There is a pattern called Flash Messages. Is implemented as a helper in most PHP Frameworks.

You can find many implementations everywhere.

https://github.com/plasticbrain/PHP-Flash-Messages

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.