doubledee Posted September 1, 2011 Share Posted September 1, 2011 I am getting strange behavior after a user submits a comment. After they comment on an article, I display a success message with two buttons: "Return to Article" and "Go to Home Page" Each button seems to work, and I was even able to "Return to Article" and then hit my browser's Back button to return the original "You Comment was Added" message, but if I hit the Back button after hitting "Go to Home Page" I got this error... Notice: Undefined index: pageTitle in /Users/user1/Documents/DEV/++htdocs/01_MyProject/add_comment.php on line 39 Call Stack On Line 39 I have this code... // Set Page Title. $pageTitle = $_SESSION['pageTitle']; It seems to me that if a user hits the Back button in general it will often cause problems, so is there a way to handle this?? I'm not exactly certain is causing the error above in the first place since my SESSION should be intact.... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/246152-back-button-error/ Share on other sites More sharing options...
darkfreaks Posted September 1, 2011 Share Posted September 1, 2011 are you checking to see if the session is not empty or set // Set Page Title $pageTitle = isset($_SESSION['pageTitle']) ? $_SESSION['pageTitle'] : 'The Session has not been set'; Quote Link to comment https://forums.phpfreaks.com/topic/246152-back-button-error/#findComment-1264147 Share on other sites More sharing options...
doubledee Posted September 1, 2011 Author Share Posted September 1, 2011 are you checking to see if the session is not empty or set // Set Page Title $pageTitle = isset($_SESSION['pageTitle']) ? $_SESSION['pageTitle'] : 'The Session has not been set'; No, I am not checking if it is set, but regardless, why is it becoming "un-set" after the user hits the back button? And even if your code above fixed one problem, it would create another one... Here is my code... // Verify Insert. if (mysqli_stmt_affected_rows($stmt)==1){ // Insert Succeeded. echo '<div id="box_Content_700b">'; echo '<h1>Comment Submitted</h1>'; echo '<p>Thanks for sharing your thoughts!</p> <p>Once approved, your comment will appear below the article:<br /> <span id="articleTitle">' . '"' . htmlspecialchars($pageTitle, ENT_QUOTES) . '"</p>'; echo '<ul> <li> <a href="' . WEB_ROOT . $returnToPage . '"> <img src="' . WEB_ROOT . 'buttons/ReturnToArticle.png" alt="Return to Article" /> </a> </li> <li>or</li> <li> <a href="' . WEB_ROOT . 'index.php"> <img src="' . WEB_ROOT . 'buttons/GoToHomePage.png" alt="Go to Home Page" /> </a> </li> </ul>'; echo '</div>'; }else{ // Insert Failed. echo '<div id="box_Content_700b">'; echo '<h1>Comment Creation Failed</h1>'; echo '<p>Your comment could not be added due to a system error.</p>'; echo '</div>'; }// End of VERIFY INSERT. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/246152-back-button-error/#findComment-1264150 Share on other sites More sharing options...
darkfreaks Posted September 1, 2011 Share Posted September 1, 2011 unless you plan on using globals you need to call session_start on each page or your session won't carry over from a previous page. Global session function: function global_session($var){ if(!array_key_exists($var,$_SESSION)) $_SESSION[$var]=''; $GLOBALS[$var]=&$_SESSION[$var]; } Quote Link to comment https://forums.phpfreaks.com/topic/246152-back-button-error/#findComment-1264153 Share on other sites More sharing options...
doubledee Posted September 1, 2011 Author Share Posted September 1, 2011 unless you plan on using globals you need to call session_start on each page or your session won't carry over from a previous page. I have session_start as the first line of code in "add_comment.php" Debbie Quote Link to comment https://forums.phpfreaks.com/topic/246152-back-button-error/#findComment-1264154 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.