[RESOLVED] Warning: session_start(): Cannot send session cookie - headers already sent - Page 2
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 42

Thread: [RESOLVED] Warning: session_start(): Cannot send session cookie - headers already sent

  1. #16
    Junior Member Saviola's Avatar
    Join Date
    May 2010
    Posts
    22
    Quote Originally Posted by Weedpacket View Post
    Yes, Notepad is one of those editors that does this (unless you save the file with an ANSI character set). Visual Studio also provides the option, but if I recall, for HTML files it defaults to Latin-1 and you have to tell it to use UTF-8 (with BOM).
    Yes that's right. I had the same problem with few files created by Notepad and i was quite confuse.
    My problem came from the ANSI character set UTF-8 with BOM.
    After a lot of research i found this this information then i fixed the problem.

    I just recreated the file with different editor and everything works fine.
    I hope this information can save your time and nerves.


    --------------------------------------
    Nothing is imposible imagination is everything!
    Benchmark Software
    World's fastest database

  2. #17
    Junior Member
    Join Date
    Jun 2010
    Posts
    2
    I had this problem. Went through and cleaned up all spaces and lines. It still didn't work. Then I changed from UTF-8 to ASCII. Bingo! So now I'll try to use ASCII for php and UTF-8 for HTML

  3. #18
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,414
    @Rodney: Your problem wasn't UTF-8 itself (I use it on several projects), but rather than your text editor probably included the BOM character at the beginning of the file. Since the BOM isn't part of "<?php", PHP assumes that you meant for it to be outputted to the user.

  4. #19
    Member
    Join Date
    Jun 2010
    Posts
    87
    If you just use Notepad to write your scripts then i would highly encourage you to download Notepad++. Quite easily the best text editor for web design available. And its free!

  5. #20
    Junior Member
    Join Date
    Jun 2010
    Posts
    2
    Great! That worked as well. I downloaded notepad++ and saved as UTF-8 without BOM.
    Thanks

  6. #21
    Junior Member
    Join Date
    Aug 2010
    Posts
    1
    After scratching my head with the same problem, i found that there was a new line after the closing ?> php tag in one of my include files. Like others have mentioned, this is a definite 'gotcha'

  7. #22
    Junior Member
    Join Date
    Apr 2009
    Posts
    4

    Same Problem: "Warning: session_start() [function.session-start]: Cannot ..."

    I know this is an old thread but perhaps someone can help.

    I have created a test file with ONLY this code:

    <?php
    $value = session_start();
    echo "Session value = ".$value."<br>";
    ?>

    As suggested in this thread (or elsewhere):
    1) There are no spaces, html, etc. preceding this.
    2) The file is encoded in UTF-8 WITH BOM
    3) I've modified my php.ini file to set: session.cache_limiter = public

    And I get these errors:

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\feralpots\eNorman\test.php:1) in C:\xampp\feralpots\eNorman\test.php on line 2

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\feralpots\eNorman\test.php:1) in C:\xampp\feralpots\eNorman\test.php on line 2
    Session value = 1

    I am baffled !

    Does anyone know how to fix this ??

    Thx !

  8. #23
    Senior Member dagon's Avatar
    Join Date
    Nov 2001
    Posts
    6,178
    while it shouldn't matter, does it still happen if you don't assign the session_start return value to a variable?

  9. #24
    Bug Killer
    Join Date
    Jun 2008
    Location
    Wisconsin
    Posts
    665
    ... headers already sent by ...
    means that there is something in front of the session_start

    2) The file is encoded in UTF-8 WITH BOM
    I believe that is suppose to be without BOM. But save yourself the hassle and get anyone of the hundreds of free IDE's out there. NotePad++ is a good one to start with.
    Some Handy Resources:
    php.net for PHP
    W3Schools.com for HTML, Javascript, and CSS
    MySQL 5 Reference Manual
    And if they all fail use the
    Top Secret Web Developer Tool

  10. #25
    Junior Member
    Join Date
    Apr 2009
    Posts
    4

    Thumbs up SOLVED - Using UTF-8 WITHOUT BOM

    Kirk,

    You were right about UTF-8 WITH BOM !
    I was already using notepad++.
    When I converted to to UTF-8 WITHOUT BOM the warning disappeared !

    Thanks immensely for your help !!!

    Dagon -- thank you too for your quick reply !


  11. #26
    Rookie Coder
    Join Date
    Mar 2011
    Location
    Gold Coast australia
    Posts
    1

    As long as session_start is the first line it seems to work

    I'm using Notepad++ and had the PHP down in the body. Once I moved it to the very first line it was fine. Even the <html> tag caused it to break if it was first. This works:

    <?php
    session_start(); // initialise a session - commented out as it seems to auto start
    $_SESSION['counter']++; // increment a session counter
    ?>
    <html>
    <head>
    </head>
    <body>
    <?php echo "You have viewed this page ". $_SESSION['counter'] . " times"; ?>
    </body>
    </html>

  12. #27
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,414
    Since session_start() modifies the outgoing HTTP response headers, it doesn't have to be the 'first' line of code - it just has to be called before any output (which causes the HTTP headers to be sent at that point).

  13. #28
    Junior Member
    Join Date
    Mar 2012
    Posts
    1

    Smile RESOLUTION

    I enclosed the start_session() command in php tags at the beginning of my script and then started a new php tag for the rest of the code. This is the only solution that worked for me, as follows:

    <?php session_start();?>
    <?php
    php code here......
    ?>

  14. #29
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,414
    Quote Originally Posted by sbrichman View Post
    This is the only solution that worked for me
    Then you were doing something very, very wrong, because the call to session_start() doesn't have to be surrounded by its own set of PHP tags, nor does it need to be the first statement in the script (or even at the "beginning").

  15. #30
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    I'm getting the same error on line 1 of my code. my session starts on top and I tried saving in notepad++ in UTF without BOM. STILL no luck. this script was working, and hasn't moved servers or anything so I'm not sure what happened...could the way the file is uploaded affect it working?

    here is my code:

    Code:
    <?php session_start(); ?>
    
    
    <script src="js/jquery.validate.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" charset="utf-8">
    //<![CDATA[
    			$(document).ready(function() {
    				
    				$("#loginform").validate({
    					rules: {
    						email: {
    							required: true,
    							email:true
    						},
    						password: {
    							required: true,
    						},
    					},
    					messages: {
    						password: "Please enter your password",
    						email: "Please enter a valid email address"
    					}
    				});
    			});
    			
    			function OnLogin(){
    				var email = $('#email').val();
    				var password = $('#password').val();
    				
    				if ( email != "" && password != "" ){ 
    					
    						$.ajax({
    								url:'lib/login.php',
    								type:'POST',
    								data:"email=" + email + "&password=" + password,
    								async:false,
    								success: function(str){
    									
    									if ( str.indexOf("SUCCESS") != -1 )
    									{
    										if ( $("#redirect").length > 0 && $("#redirect").val() != ""  ) 
    											location.href = $("#redirect").val() + ".php";
    										else
    											location.reload();
    										return;
    									}
    									else
    									{
    										if ( str.indexOf("PWD") != -1 )
    										{
    											alert("Please input the correct password.");
    											$("#password").focus(); 
    										}
    										else if ( str.indexOf("UID") != -1 )
    										{
    											alert("Please input the correct email.");
    											$("#email").focus();
    										}
    									}
    								}
    							});
    					}
    						
    				}
    						
    			function OnRegister(){
    				location.href = "register.php";
    			}
    			
    			function OnLogOut(){
    				$.ajax({
    						url:'lib/logout.php',
    						type:'POST',
    						data:"logout=1",
    						async:false,
    						success: function(str){
    							
    							if ( str.indexOf("SUCCESS") != -1 )
    							{
    								location.reload();
    								return;
    							}
    						}	
    					});				
    			}
    //]]>
    </script>
    <?php if( !isset($_SESSION['PHP_AUTH_USER']) ): ?>
    <h1>User Login</h1>
    <div id="login-form">
    	
    	<form id="loginform" name="loginform" method="post" onsubmit="OnLogin();return false;">
    		<h3>Email</h3>
    		<input type="text" name="email" id="email" />
    		<h3>Password</h3>
    		<input type="password" name="password" id="password" />
    		<br />
    
    		<input type="image" class="login button" src="images/login.jpg"   />
    		<input type="image" class="register button" src="images/register.jpg" onclick="OnRegister()" />
    	</form>
    </div>
    <?php else: ?>
    	<p>Thanks <strong><?=$user_name?></strong>, You are now logged in.</p>
    	<p style="text-align:center"><input type="button" class="button" onclick="OnLogOut()" value="logout" /></p>
    <?php endif; ?>

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •