Error here, not there

Thread Solved

Join Date: Jun 2008
Posts: 27
Reputation: Pado is an unknown quantity at this point 
Solved Threads: 0
Pado Pado is offline Offline
Light Poster

Error here, not there

 
0
  #1
May 25th, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 158
Reputation: sDJh is an unknown quantity at this point 
Solved Threads: 13
sDJh sDJh is offline Offline
Junior Poster

Re: Error here, not there

 
0
  #2
May 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 197
Reputation: FlashCreations is an unknown quantity at this point 
Solved Threads: 17
FlashCreations's Avatar
FlashCreations FlashCreations is offline Offline
Junior Poster

Re: Error here, not there

 
0
  #3
May 25th, 2009
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:
  1. <?php //Notice the space here
  2. header("Location: thiswillnotwork.html");
  3. ?>
And can easily be fixed by removing the extra space or characters:
  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.
FlashCreations
(aka PhpMyCoder)

About Me | My Blog | Contact Me
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 27
Reputation: Pado is an unknown quantity at this point 
Solved Threads: 0
Pado Pado is offline Offline
Light Poster

Re: Error here, not there

 
0
  #4
May 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 197
Reputation: FlashCreations is an unknown quantity at this point 
Solved Threads: 17
FlashCreations's Avatar
FlashCreations FlashCreations is offline Offline
Junior Poster

Re: Error here, not there

 
0
  #5
May 25th, 2009
Have you started the session with session_start() elsewhere on this page?
FlashCreations
(aka PhpMyCoder)

About Me | My Blog | Contact Me
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 27
Reputation: Pado is an unknown quantity at this point 
Solved Threads: 0
Pado Pado is offline Offline
Light Poster

Re: Error here, not there

 
0
  #6
May 25th, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,093
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 138
ardav's Avatar
ardav ardav is online now Online
Veteran Poster

Re: Error here, not there

 
0
  #7
May 25th, 2009
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">'.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 101
Reputation: leviathan185 is an unknown quantity at this point 
Solved Threads: 14
leviathan185's Avatar
leviathan185 leviathan185 is offline Offline
Junior Poster

Re: Error here, not there

 
0
  #8
May 25th, 2009
Originally Posted by Pado View 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:
  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
@ I'm gonna live forever, or die trying.

@ A wise man once told me, in order to understand recursion, you first must understand recursion.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 27
Reputation: Pado is an unknown quantity at this point 
Solved Threads: 0
Pado Pado is offline Offline
Light Poster

Re: Error here, not there

 
0
  #9
May 26th, 2009
This is how my typical web page starts:
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 101
Reputation: leviathan185 is an unknown quantity at this point 
Solved Threads: 14
leviathan185's Avatar
leviathan185 leviathan185 is offline Offline
Junior Poster

Re: Error here, not there

 
0
  #10
May 26th, 2009
Originally Posted by Pado View Post
This is how my typical web page starts:
  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?
@ I'm gonna live forever, or die trying.

@ A wise man once told me, in order to understand recursion, you first must understand recursion.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum


Views: 449 | Replies: 13
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC