943,681 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 681
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
May 25th, 2009
0

Error here, not there

Expand Post »
Hello everyone:
I'm a bit frustrated because I'm getting an error on one website but not on another one for the same web page. This tells me there's something different about the PHP settings that is different, but I'm not sure where to begin. This is the error I get:
Warning: Cannot modify header information - headers already sent by
Based on my reading online, it is supposedly common for this error to occur when there is extra white space showing up in the code. I'm not sure that this is my problem because, like I've already mentioned, on one site the code works and the other it doesn't. Here is the code being referenced in the error:
PHP Syntax (Toggle Plain Text)
  1. if(isset($_COOKIE[session_name()])) {
  2. setcookie(session_name(), ' ', time()-42000, '/');
  3. }
  4. session_destroy();
  5. redirect_to("index_bi.php?order=$name");
What I'm trying to do is after the order to borrow books has been placed and an email sent telling of the order, I want the session to be destroyed so that there is nothing left in the person's order list. Any ideas what's wrong? Thank you.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Pado is offline Offline
32 posts
since Jun 2008
May 25th, 2009
0

Re: Error here, not there

You have to make sure, that you definately don't have any html, echo or print before you set the session. Thats because session sends a cookie to the client via the HTTP-header. As soon you have any output, PHP sends the header and the stuff you want the browser to show.

The reason why you don't have the problem on the one server could be, that you have the error-reporting turned off.
Reputation Points: 56
Solved Threads: 29
Posting Whiz in Training
sDJh is offline Offline
255 posts
since Aug 2005
May 25th, 2009
0

Re: Error here, not there

Make sure that you have no html or other text written with echo before that or any html or text before the <?php tag. This could be something as simple as a space:
PHP Syntax (Toggle Plain Text)
  1. <?php //Notice the space here
  2. header("Location: thiswillnotwork.html");
  3. ?>
And can easily be fixed by removing the extra space or characters:
PHP Syntax (Toggle Plain Text)
  1. <?php //No spaces so the header call should work along with the use of session vars
  2. header("Location: thiswillwork.html");
  3. ?>

I also find it to be a good practice to begin the session right after the <?php tag if I am using it in that script.
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
May 25th, 2009
0

Re: Error here, not there

Thanks for the quick responses. I checked the display errors setting and it's on. And error_reporting is set for 6135. I don't know what this means, but that's the setting for it. And I checked for extra spaces and I don't think there are any. I placed the following on a separate page and I still get the error. It has to be in this script, but I can't see it:
		if(isset($_COOKIE[session_name()])) {
			setcookie(session_name(), '', time()-42000,'/');
		}
		
		// 4. Destroy the session
		session_destroy();
		redirect_to("index_bi.php?order=$name");
Especially the lines in bold are getting errors.
Last edited by Pado; May 25th, 2009 at 12:54 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
Pado is offline Offline
32 posts
since Jun 2008
May 25th, 2009
0

Re: Error here, not there

Have you started the session with session_start() elsewhere on this page?
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
May 25th, 2009
0

Re: Error here, not there

Yeah, it was started first thing. Well, I got it to work, but I'm not sure why. There was some conflict between the lines of code I showed you and code in the header. Here is the header code:

PHP Syntax (Toggle Plain Text)
  1. <link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" />
  2. <script language="javascript">
  3. function printpage()
  4. {
  5. window.print();
  6. }
  7. </script>
  8. <script language="javascript">
  9. <!--
  10. function confirmPost() {
  11. var agree=confirm("Are you sure you want to delete this student?");
  12. if (agree)
  13. return true;
  14. else
  15. return false;
  16. }
  17. //-->
  18. </script>
  19. <style>
  20. <!--
  21. A{text-decoration:none}
  22. -->
  23. </style>
  24. </head>
  25. <div id="header">
  26. <h1>Bohemia Institut</h1>
  27. </div>
  28. <div id="main">
I just cut this header code out of the next web page and it worked fine. But can anyone see the problem here? I would prefer to keep it the way I had it, but will live with it the way it is now if no one sees the problem. Thanks.
Reputation Points: 10
Solved Threads: 0
Light Poster
Pado is offline Offline
32 posts
since Jun 2008
May 25th, 2009
0

Re: Error here, not there

I'm a bit confused here, is the code above all that you have at the start of your page? No DTD? Do you have 'html' and 'body' tags? The page looks poorly formed (sorry, no offence). Could you print the page, showing the whole code from the beginning to '<div id="header">'.
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 945
Sarcastic Poster
ardav is offline Offline
6,672 posts
since Oct 2006
May 25th, 2009
0

Re: Error here, not there

Click to Expand / Collapse  Quote originally posted by Pado ...
Hello everyone:
I'm a bit frustrated because I'm getting an error on one website but not on another one for the same web page. This tells me there's something different about the PHP settings that is different, but I'm not sure where to begin. This is the error I get:
Warning: Cannot modify header information - headers already sent by
Based on my reading online, it is supposedly common for this error to occur when there is extra white space showing up in the code. I'm not sure that this is my problem because, like I've already mentioned, on one site the code works and the other it doesn't. Here is the code being referenced in the error:
PHP Syntax (Toggle Plain Text)
  1. if(isset($_COOKIE[session_name()])) {
  2. setcookie(session_name(), ' ', time()-42000, '/');
  3. }
  4. session_destroy();
  5. redirect_to("index_bi.php?order=$name");
What I'm trying to do is after the order to borrow books has been placed and an email sent telling of the order, I want the session to be destroyed so that there is nothing left in the person's order list. Any ideas what's wrong? Thank you.

I was having a similar issue the other day, but my situation was a little different. have you tried ob_start(); and ob_end_flush();?

Try putting ob_start(); athe the start of the page and ob_end_flush(); just after the header("Location: URL")

it will not send anything to the browser until it gets to the on_end_flush so it should work
Reputation Points: 19
Solved Threads: 15
Junior Poster
leviathan185 is offline Offline
105 posts
since May 2009
May 26th, 2009
0

Re: Error here, not there

This is how my typical web page starts:
PHP Syntax (Toggle Plain Text)
  1. <?php session_start();
  2.  
  3. function logged_in() {
  4. return (isset($_SESSION['user_id']));
  5. }
  6.  
  7. function confirm_logged_in() {
  8. if (!logged_in()) {
  9. redirect_to("index.php");
  10. }
  11. }
  12. ?>
  13. <?php
  14. require("constants.php");
  15. $connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
  16. if (!$connection) {
  17. die("Database connection failed: " . mysql_error());
  18. }
  19.  
  20. $db_select = mysql_select_db(DB_NAME,$connection);
  21. if (!$db_select) {
  22. die("Database selection failed: " . mysql_error());
  23. }
  24. ?>
  25. <?php require_once("includes/functions.php"); ?>
  26. <head>
  27. <title>Name of Organization</title>
  28. <link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" />
  29. <script language="javascript">
  30. function printpage()
  31. {
  32. window.print();
  33. }
  34. </script>
  35. <script language="javascript">
  36. <!--
  37. function confirmPost() {
  38. var agree=confirm("Are you sure you want to delete this student?");
  39. if (agree)
  40. return true;
  41. else
  42. return false;
  43. }
  44. //-->
  45. </script>
  46. <style>
  47. <!--
  48. A{text-decoration:none}
  49. -->
  50. </style>
  51. </head>
  52. <div id="header">
  53. <h1>Name of Organization</h1>
  54. </div>
  55. <div id="main">
The first two sections, the session and connection, are in their own include files. I guess this isn't very good, but I've always worried about the PHP on the page and not the other stuff. Could you enlighten me a bit? Thanks.
Reputation Points: 10
Solved Threads: 0
Light Poster
Pado is offline Offline
32 posts
since Jun 2008
May 26th, 2009
0

Re: Error here, not there

Click to Expand / Collapse  Quote originally posted by Pado ...
This is how my typical web page starts:
PHP Syntax (Toggle Plain Text)
  1. <?php session_start();
  2.  
  3. function logged_in() {
  4. return (isset($_SESSION['user_id']));
  5. }
  6.  
  7. function confirm_logged_in() {
  8. if (!logged_in()) {
  9. redirect_to("index.php");
  10. }
  11. }
  12. ?>
  13. <?php
  14. require("constants.php");
  15. $connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
  16. if (!$connection) {
  17. die("Database connection failed: " . mysql_error());
  18. }
  19.  
  20. $db_select = mysql_select_db(DB_NAME,$connection);
  21. if (!$db_select) {
  22. die("Database selection failed: " . mysql_error());
  23. }
  24. ?>
  25. <?php require_once("includes/functions.php"); ?>
  26. <head>
  27. <title>Name of Organization</title>
  28. <link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" />
  29. <script language="javascript">
  30. function printpage()
  31. {
  32. window.print();
  33. }
  34. </script>
  35. <script language="javascript">
  36. <!--
  37. function confirmPost() {
  38. var agree=confirm("Are you sure you want to delete this student?");
  39. if (agree)
  40. return true;
  41. else
  42. return false;
  43. }
  44. //-->
  45. </script>
  46. <style>
  47. <!--
  48. A{text-decoration:none}
  49. -->
  50. </style>
  51. </head>
  52. <div id="header">
  53. <h1>Name of Organization</h1>
  54. </div>
  55. <div id="main">
The first two sections, the session and connection, are in their own include files. I guess this isn't very good, but I've always worried about the PHP on the page and not the other stuff. Could you enlighten me a bit? Thanks.
I am a little confused? This code is different to your origional question? did you solve that issue?
Reputation Points: 19
Solved Threads: 15
Junior Poster
leviathan185 is offline Offline
105 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How use php array with javascript array
Next Thread in PHP Forum Timeline: Help adding required fields on form to email script...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC