surreal5335 Posted January 13, 2010 Share Posted January 13, 2010 I have created a database that will allow users to create posts and currently see all the posts that have been created so far. My problem is I have a message meant to display after creating a post that will dissapear when page is refreshed. As of now though that message is not displaying at all. The code used to set the message is: function flash_notice($msg) { if (!$msg) { return false; } $_SESSION['flash']['notice'] = $msg; return true; } which is well actually set here on another file: if(create_post ($params['post'])) { flash_notice('Successfully created post!'); redirect_to('controller_posts.php/'); } the problem I am having is that my syntax: $_SESSION['flash']['notice'] = ''; is still being executed despite my function that occurs before the last piece of code I just shown: function redirect_to($address) { header('location:'.WEBSITE.APP_ROOT.$address); exit; } All this data is finally displayed to the user with this: <?php if($_SESSION['flash']['notice']): ?> <span class="notice"> <?php echo $_SESSION['flash']['notice']; ?> </span> <?php endif; ?> If I enter a string into: $_SESSION['flash']['notice'] = ''; It will print out fine. But its purpose is to set the string to blank, so it will make the other message dissapear upon a page refresh. From what I have been learning the exit; function allows the code to stop there so the code below it will not execute. But so far, it is executing just fine. Could someone tell me how I can go about fixing this? I appreciate the help Link to comment https://forums.phpfreaks.com/topic/188297-_sessionflashnotice-message-not-displaying/ Share on other sites More sharing options...
Zyx Posted January 13, 2010 Share Posted January 13, 2010 For me it seems that you clear the flash notice too quick. You should clear it on another page, AFTER the redirect and AFTER you display it: 1. Set a flash message. 2. Redirect to another page. 3. Display it. 4. Clear. In my code I wrote a class, which has two basic methods: 1. setMessage() - sets a message and performs an immediate redirect. 2. getMessage() - reads the message from a session, saves into an internal class field and resets the session data. So I clear the message right after the first read. If I need to display it twice on the same page, it is still stored in the flash message object. Link to comment https://forums.phpfreaks.com/topic/188297-_sessionflashnotice-message-not-displaying/#findComment-994062 Share on other sites More sharing options...
surreal5335 Posted January 15, 2010 Author Share Posted January 15, 2010 Thanks for the suggestion, would you happen to have a good article explaining setters and getters for php? I am having a hard time finding a good tutorial explaining it. Also is it possible to check if an element has been echoed? Thanks a lot for the help Link to comment https://forums.phpfreaks.com/topic/188297-_sessionflashnotice-message-not-displaying/#findComment-995396 Share on other sites More sharing options...
surreal5335 Posted January 16, 2010 Author Share Posted January 16, 2010 well I decided I would try another way to go abou this problem using a suggestion you made. I will make a temp detour to a page telling them creation of post has been made. then redirect to a page with all the posts made so far. My problem is I am having a trouble using set_time_limit()... I am not sure how to fire a redirect upon time out. Here is my code so far: include_once('config.php'); <?php echo 'Successfully created post!<br/><br/>'; echo 'you will be automatically redirected in a few seconds.'; set_time_limit(5) ?> I appreciate your help Link to comment https://forums.phpfreaks.com/topic/188297-_sessionflashnotice-message-not-displaying/#findComment-996002 Share on other sites More sharing options...
Zyx Posted January 16, 2010 Share Posted January 16, 2010 Such redirecting should be done with HTML. You cannot fire a redirect once you sent some HTML code - this is how the HTTP protocol works. First, you send headers, then the content. Once the browser starts displaying it, the PHP script has already stopped working. And a server redirect is a header. <meta http-equiv="refresh" content="5;url=page.php"> The use of setters and getters can be found in every good object-oriented programming course. Link to comment https://forums.phpfreaks.com/topic/188297-_sessionflashnotice-message-not-displaying/#findComment-996012 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.