flunn Posted May 24, 2007 Share Posted May 24, 2007 I am learning php because I want to make interactive quizzes etc for my educational website. I have been using Larry Ullman's book "PHP for the World Wide Web." I've written some code -- based on code I've found in LU's book. The code uses "sessions_start()". When I test it on my local server it works fine, but when I upload it to my shared server I get "headers already sent" error messages. These can be seen at http://adrawingblog.net/new_banner.php LU discusses "ob_start()" briefly in Chapter 8 and explains how using it helps to avoid "headers already sent" messages. I hadn't included this function in the code however, because as long as I was working locally, it wasn't necessary. When I uploaded it and saw the errors, I inserted "ob_start()" and uploaded again but it didn't help. (Following LU's instructions, I also inserted "ob_end_flush()".) I experimented with putting the "ob_start()" call in various places and putting it before and after the "sessions_start()" call, but this didn't help either. (As far as I can see, LU discusses output buffering only once in his book, before he discusses "sessions." He says that the "ob_start()" call should come at the "very top of the page" but that's all.) Any help solving this problem would be much appreciated. regards to all from flunn Here is the code <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN"> <?php session_start(); ob_start(); ?> <html> <head> <title>Verb Tense Quiz</title> <style media="screen" type="text/css"> .text { color:black; font-size: 12px; font-family: Arial; margin-top: 0px; } .text14 { color:black; font-size: 14px; font-family: Arial; margin-top: 0px; } .text14_marg { color:black; font-size: 14px; font-family: Arial; margin-top: 50px; line-height: 225%; margin-left: 10%; margin-right: 10%; border: 1px solid black; } .bnr_box { position: absolute; top:0px; left: 72px; width: 624px; height: 82px; border: 1px solid black; padding: 2px; } .bnr_links1 { position: absolute; top: 0px; right: 0px; width: 472px; height: 25px; text-align: right; border: 1px solid black; background: white } .main_box { position: absolute; left: 72px; top: 90px; width: 624px; height: 800px; border: 1px solid black; padding: 2px; } </style> </head> <body> <div class="bnr_box"> <p class="text"> Interactive Grammar <br /> Verb Tense Quiz</p> <div class="bnr_links1"> <p class="text"> general links</p> </div> </div> <div class="main_box"> <?php //flesl_script 1.0 basic_quiz.php //Address error handling ini_set('display_errors', 1); error_reporting(E_ALL & ~E_NOTICE); if (empty ($_SESSION['total_right'])) { $_SESSION['total_right']= 0; } //Check to see if the form has been submitted if (isset($_POST['submit'])) { //if the submit button has been clicked if (empty($_POST['answer'])) { //if the question has not been answered $response = "You have not answered the question yet."; } else { if ($_POST['answer'] == "swims") { $response = "Right"; $_SESSION['total_right'] = $_SESSION['total_right'] +1; $_SESSION['total_done'] = $_SESSION['total_done'] + 1; } else { $response = "Wrong"; $_SESSION['total_done'] = $_SESSION['total_done'] + 1; } } }//End of checking IF //display option #1: question not answered so full form displayed again if (!isset($_POST['submit']) || (empty($_POST['answer']))) { print '<p class="text14">Type the correct form of the verb "swugs" in the box.</p>'; print '<form action="new_banner.php" method="post">'; print '<p class="text14_marg">Harry can\'t play tennis anymore because he has a bad back but he still <input type="text" name="answer" size="20" value="' .$_POST['answer']. '"/> in his pool three times a week.</p><br />'; print '<input type="submit" name="submit" value="Submit your Answer"/> '; print '</form>'; print $response; } else { // display option #2: question answered //display option #2a: test not finished if ($_SESSION['total_done']<=2) { print '<p>Type the correct form of the verb "swim" in the box.</p>'; print '<form action="basic_quiz(2).php" method="post">'; print 'Harry can\'t play tennis anymore because he has a bad back but he still <input type="text" name="answer" size="20" value="' .$_POST['answer']. '"/ > in his pool three times a week.<br />'; print '</form> <br />'; print $response; print '<br />'; print "<p>Your score so far is {$_SESSION['total_done']} answered and {$_SESSION['total_right']} right.</p>"; print '<br />'; print '<a href="new_banner.php"><button>Go to Next Question</button></a> ' ; } //display option #2b: test finished else { print '<form action="basic_quiz(2).php" method="post">'; print 'Harry cannot play tennis anymore because he has a bad back but he still <input type="text" name="answer" size="20" value="' .$_POST['answer']. '"/> three times a week.<br />'; print '</form> <br />'; print $response; print '<br />'; print "<p>You have finished Verb Tense Test #3</p> Your final score is {$_SESSION ['total_right']} out of 10 <br />" ; unset($_SESSION); session_destroy(); print '<a href="new_banner.php"><button>Do The Test Again</button></a> '; } } print '<br />'; ; ?> </div> </body> </html> <?php ob_end_flush(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52831-solved-problem-with-headers-already-sent-errors/ Share on other sites More sharing options...
MadTechie Posted May 24, 2007 Share Posted May 24, 2007 the problem is here <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN"> <?php session_start(); ob_start(); ?> read the pinned topic for more info..(as you should of before posting) easy to resolve, but i have explained this so many times now.. yet people don't search the forum! Quote Link to comment https://forums.phpfreaks.com/topic/52831-solved-problem-with-headers-already-sent-errors/#findComment-260799 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.