Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
thomascawthorn
22,986 PointsUsing Sessions without session_start()
I've created a really simple success/error message array for a form validation. When a validation fails, its specific message gets added to the end of the $error_messages array. The $error_messages array is then set as a session variable $_SESSION['error_messages'].
So far so good and when I echo out each $error_message in a foreach(), everything displays as I'd hoped.
Then I realised I never started the session! But the error messages were displaying as I wanted:
- Reset for each form submission
- Didn't persist between pages (i.e. I want the messages to clear when the user moves away from the form having successfully, or unsuccessfully completed the form).
Thinking about it, I didn't really need to use session variables for this... But I want to learn how to use them ;)
Now when the user submits the form successfully, I want to go back to the 'show all' page or homepage with a success message saying "Added successfully". This works but any old error messages also show alongside this success message and none of the messages clear when I change to any other page (if session_start() is turned on).
Can anyone offer any advice and maybe tell me how to clear a session variable messages between pages?
Maybe after every display I should clear the array.. Possible, but in my head it seems a little unsophisticated.
2 Answers
Aaron Graham
18,033 PointsMaybe use array_pop() to get the messages out of your session variable.
<?php
$length = count($error_messages);
for($i = 0; $i <= $length; $i++) {
echo array_pop($error_messages);
}
?>
That will print any messages that have been added to the array but haven't been cleared, and will clear them in the process. Do you need to persist the error messages beyond them being delivered to the user for any reason?
**Not sure if you saw all the edits. Sorry for so many. I just couldn't get this right... **
thomascawthorn
22,986 PointsOh that's wicked! Totally forgot about this function!
I would mark this as best answer but not sure you can do it on general discussion/I've totally lost where the button is?!
I think the error messages only need to be shown to the user once so removing the messages and displaying them simultaneously is perfect.
I can't think of a example at the moment of how this would be useful - but would you be able to persist a message for x number of pages? I guess I would set a new session var called 'message seen' which incremented for each 'view' of the message? Or set a session that died after a very short amount of time?
Aaron Graham
18,033 PointsIf you wanted to persist these messages for several pages, I think your idea for adding a session variable to track page views since message seen is probably the way to go. It could get kind of clumsy though, since you might be tracking any number of messages that have been seen over the span of several different page views. But really I think the best thing would be to think about whether you should be persisting these messages at all. I can't really think of a reason to do it. If you have some very specific reason to keep these around, then by all means implement this. Otherwise, I don't even think I would persist them past the page view after they are generated. i.e. user does something to generate a message -> user is redirected to another page and shown the message -> message is deleted. I just can't imagine a scenario where a user would need a message that was generated two or more page views ago. i.e. user does something to generate a message -> user is redirected to another page -> user visits another page -> user visits another page and is shown the message. In your original post you mentioned validation success or error messages. To me, these really don't make sense to keep around. What you are working on sound a lot like what a lot of frameworks call "flash messages". Usually, these don't persist past the next page view... I think... (I know we have had the framework discussion before, and for the record I think you are doing things the right way by experimenting with some of these ideas on your own. Building some of this stuff yourself gives you a lot of insight into how it works and gives you a lot of respect for how much a framework really does. That said, in production code, for anything but a very simple site, I recommend finding a framework you can work with and trust. When the complexity increases, there are just too many places to go wrong.)
As far as the sessions go, I probably wouldn't expire or unset a session just to get rid of a variable I don't want. You could have a lot of other things in your $_SESSION that you might not want to get rid of and would have to try and copy it into a new session. Seems kind of hacky. I would probably do something like $_SESSION['messages'] = array(); to just reinitialize your array. That way you only touch one variable and leave the rest of the session intact. There are good reasons for expiring a session though. Mostly security related. If you are interested, you should read this: Session Management Cheat Sheet. Actually, if you plan to do any production work with sessions, you should definitely read that.
-- Cheers!
thomascawthorn
22,986 PointsAwesome! Yeah, I couldn't think of a valid reason other than to have some fun with sessions :p
You will be pleased to hear I'm picking up Zend first thing next week! It looks awesome. From what I've seen on their documentation, it looks pretty similar to how I've been working with my none-framework sites. Flash messages are working like an absolute treat now!
Thanks for the info on session management, that cheat sheet is really interesting!
thomascawthorn
22,986 Pointsthomascawthorn
22,986 PointsHello again!
This is great because it takes off each error message from $error_messages. But I have of course, managed to find a spanner and throw it in the works.
I've created a utils file to create standard functions like setsessionvar, getsessionvar etc. So when I get the array through it's the return of a function. i.e. the array_pop isn't actually popping the session var directly, but it is popping the array created when I call the function getErrorMessages(). This means on every page refresh, everything persists and multiplies haha!
So now I've realized this, I maneuvered the code to pop the session array directly and it's working :)
Is there any good practice with sessions? Should you rarely 'unset' a session? Or does it really not matter at all..