session_start() - Output Buffing - and Session Variables

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
TechJammer
New php-forum User
New php-forum User
Posts: 15
Joined: Tue Nov 20, 2007 6:27 pm
Location: Maryland, USA
Contact:

Tue Sep 09, 2008 9:02 am

I am trying to use output buffering in my script, but I also need access to existing session variables.

When I use session_start() the variables are availible to me. However, when I use ob_start() I can no longer see the session variables.

When I try to combine the two methods, I get an error: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home/xxxxxx/xxxx_xxxx/domainname/ADSC.php:1)

I have tried various combinations of session_start and ob_start. Although the page works and displays properly, I still get the error message logged.

Here is a sample of my code:

Code: Select all

<?php
ob_start();
session_start();
if(isset($_SESSION['adminhere'])) {$AdminLoggedOn = TRUE;}else{$AdminLoggedOn = FALSE;}
ob_end_clean();

if($AdminLoggedOn) {

	ob_start(); include '../templates/header.tpl'; $header = ob_get_contents(); ob_end_clean();
	ob_start(); include '../admintemplates/adminnav.tpl'; $adminnav = ob_get_contents(); ob_end_clean();
	ob_start(); include '../templates/footer.tpl'; $footer = ob_get_contents(); ob_end_clean();
	include "../setup.php"; 
}

more code... more code...

?>
I tried
ob_start();
session_start();

and also
session_start();
ob_start();

Any combination I try results in the same error message above. I really think the basic problem is that sessions variables are not being made availible when I use ob_start. If they were, I wouldn't need session_start.

Any ideas how I can work around this issue?

Thanks!
User avatar
AeroX
php-forum Fan User
php-forum Fan User
Posts: 140
Joined: Sun May 11, 2008 1:40 am
Location: London, UK
Contact:

Tue Sep 09, 2008 12:24 pm

Check that there is no text before the <php tags and that the file is not included by any other files.
AeroX.

When posting code to be reviewed please enclose it in the [ code ] [ /code ] tags as it makes it a lot easier for people to read as it correctly formats itself on screen.

Image
    Click Here to Visit[/url]
    User avatar
    TechJammer
    New php-forum User
    New php-forum User
    Posts: 15
    Joined: Tue Nov 20, 2007 6:27 pm
    Location: Maryland, USA
    Contact:

    Tue Sep 09, 2008 2:13 pm

    AeroX,

    Thanks for the reply. Spaces has been a common suggestion to check, but I have already been there. There are no spaces, and I'm not sure that is really an issue anyway. I have other scripts that run where there are spaces and lines before the session_start() and it doesn't seem to hurt them.

    Regardless, I don't have any spaces there now. As a matter of fact, I have tried so many things that I just decided to try a new 'thing'.

    I removed EVERYTHING from my script except <?php session_start(); ?>

    I still get the same error.
    I closed my web browser and tried again: Same error
    I moved the script to a different directory: Same error

    I am baffled... How can this be possible? I've spent TWO days with this issue already!
    User avatar
    gesf
    Moderator
    Moderator
    Posts: 1716
    Joined: Sun Dec 29, 2002 5:03 am
    Location: Portugal / Sweden
    Contact:

    Wed Sep 10, 2008 4:32 am

    Hello TechJammer.

    - First of all, try using ob_end_flush() instead of ob_end_clean() -
    (Personally, don't believe this is the problem but... you knows :$)

    Now let's try something...
    As said in the manual page, you can have multiple ob_start() activities. However, i believe (for this case) only one is needed/enought.

    (and Yes, ob_start() should or can be first of session_start())

    So, that will make or code something like this:

    Code: Select all

    <?php
    
    ob_start();
    session_start();
    if(isset($_SESSION['adminhere'])) {$AdminLoggedOn = TRUE;}else{$AdminLoggedOn = FALSE;}
    
    if($AdminLoggedOn) {
       include '../templates/header.tpl';
       include '../admintemplates/adminnav.tpl';
       include '../templates/footer.tpl';
       include "../setup.php";
    }
    
    // more code... more code...
    
     $everything = ob_get_contents(); 
    ob_end_clean();
    print  $everything;
    
    ?>
    This is what i'll ask you to test.
    I also believe that your code might work WITH the other ob_start() / ob_end_clean() instances inside the required/included files... at least for the .php ones :)

    Hope it helps!
    Sincerely,
    Gonçalo "gesf" Fontoura
    User avatar
    TechJammer
    New php-forum User
    New php-forum User
    Posts: 15
    Joined: Tue Nov 20, 2007 6:27 pm
    Location: Maryland, USA
    Contact:

    Wed Sep 10, 2008 9:30 am

    gesf... Thanks for the suggestions! I tried both, and my error continued!

    However, I found an obscure post about a php bug from back in May 2001 at:
    http://bugs.php.net/bug.php?id=11213

    In the comments it stated:

    Code: Select all

    I found out that this is because the PHP file is saved with UTF-8
    encoding, i tried to save it with different encodings (using Windows Notepad). Only when I save the file with ANSI encoding it will work
    
    I saved my script as ANSI encoding, and the error disappeared....
    User avatar
    TechJammer
    New php-forum User
    New php-forum User
    Posts: 15
    Joined: Tue Nov 20, 2007 6:27 pm
    Location: Maryland, USA
    Contact:

    Wed Sep 10, 2008 12:45 pm

    I went back and took a deeper look into this problem. I was able to verify with another script, that the session_start() issue was not a global problem. It was definitely related to this single script that I was trying to implement.

    It's hard for me to believe that the file format (UTF-8) was causing a problem with the session statement. Everything else in the script was working properly. The commands were all executed, the screens displayed properly, and the script did what it was supposed to do.

    I want to make sure I have not overlooked a possible server setting that could cause script problems in the future if the scripts are using UTF-8 encoding. I don't know how this particluar script was changed to UTF-8. I guess I caused that problem inadvertently. All the other scripts I have created are encoded as ASCII.

    Does anyone have any insight into how the file encoding could have impacted just the session_start() function?
    User avatar
    AeroX
    php-forum Fan User
    php-forum Fan User
    Posts: 140
    Joined: Sun May 11, 2008 1:40 am
    Location: London, UK
    Contact:

    Thu Sep 11, 2008 12:21 am

    The UTF-8 file format might prefix a tag at the start of the file which doesn't show in a UTF-8 compatible editor, but which php reads and belives is the start of data output. This would stop session_start() from working because session_start() requires that no data has been output before it is called as it needs to be able to set & send http headers.
    AeroX.

    When posting code to be reviewed please enclose it in the [ code ] [ /code ] tags as it makes it a lot easier for people to read as it correctly formats itself on screen.

    Image
      Click Here to Visit[/url]
      User avatar
      TechJammer
      New php-forum User
      New php-forum User
      Posts: 15
      Joined: Tue Nov 20, 2007 6:27 pm
      Location: Maryland, USA
      Contact:

      Thu Sep 11, 2008 5:03 am

      Thats an interesting thought AeroX. And probably entirely correct!

      Thanks for helping me through this issue...! I'm just glad the problem is resolved now!
      celcomgirl
      php-forum Active User
      php-forum Active User
      Posts: 29
      Joined: Tue Sep 02, 2008 7:16 pm

      Tue Sep 16, 2008 12:41 am

      TechJammer wrote:Thats an interesting thought AeroX. And probably entirely correct!

      Thanks for helping me through this issue...! I'm just glad the problem is resolved now!
      i... TechJammer i already go through with your topic. and i have same problem with you. my program successfully run at my pc.but after i put on server, i got that warning. can you explain little bit more i still not catch it.
      User avatar
      TechJammer
      New php-forum User
      New php-forum User
      Posts: 15
      Joined: Tue Nov 20, 2007 6:27 pm
      Location: Maryland, USA
      Contact:

      Tue Sep 16, 2008 1:32 am

      celcomgirl,

      When you save a script file using whatever coding software you use (windows notepad or whatever), there are usually options you can select for the output file format. For example: If you open windows notepad, then select FILE, then SAVE AS, you will see the ENCODING near the bottom of the screen. Make sure you have selected ANSI, and not UTF-8.

      If you are using another program for editing your scripts, the option for changing the file encoding may be located elsewhere, usually through the menu options or browsing through the preferences. Whatever program you are using to edit your script files, it should have a file format option that you can modify.

      Does that help? If not, tell me which editor you use and I will see if I can locate the option.
      celcomgirl
      php-forum Active User
      php-forum Active User
      Posts: 29
      Joined: Tue Sep 02, 2008 7:16 pm

      Wed Sep 17, 2008 4:59 pm

      TechJammer wrote:celcomgirl,

      When you save a script file using whatever coding software you use (windows notepad or whatever), there are usually options you can select for the output file format. For example: If you open windows notepad, then select FILE, then SAVE AS, you will see the ENCODING near the bottom of the screen. Make sure you have selected ANSI, and not UTF-8.

      If you are using another program for editing your scripts, the option for changing the file encoding may be located elsewhere, usually through the menu options or browsing through the preferences. Whatever program you are using to edit your script files, it should have a file format option that you can modify.

      Does that help? If not, tell me which editor you use and I will see if I can locate the option.
      i...see again

      i have try as you said. before this i got 2 warning which are:

      Code: Select all

      
      Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\wamp\www\XXXX\XXXX.php:4) in C:\wamp\www\XXXX\XXXX.php on line 49
      
      Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\XXXX\XXX.php:4) in C:\wamp\www\XXXX\XXXX.php on line 49
      
      after i try it still same but when i log out from the system and then log on again only the second warning appear. why it happen??? ???
      Last edited by celcomgirl on Wed Sep 17, 2008 5:38 pm, edited 1 time in total.
      User avatar
      TechJammer
      New php-forum User
      New php-forum User
      Posts: 15
      Joined: Tue Nov 20, 2007 6:27 pm
      Location: Maryland, USA
      Contact:

      Wed Sep 17, 2008 5:37 pm

      Since the second error occurs at line 49, Can you post the php code up to that point?
      celcomgirl
      php-forum Active User
      php-forum Active User
      Posts: 29
      Joined: Tue Sep 02, 2008 7:16 pm

      Wed Sep 17, 2008 6:08 pm

      TechJammer wrote:Since the second error occurs at line 49, Can you post the php code up to that point?
      line 49 is session_start(); line 1-48 only css and java script

      Code: Select all

      
      <html>
      <head>
      <title>MEC</title>
      <style type="text/css">
      .tajuk{
      	font-size: 10pt; 
      	font-family:verdana;
      	color: #FF3399;
      }
      
      .tajukkecil{
      	font-size: 10pt; 
      	font-family:verdana;
      	color: #000066; 
      }
      
      .button{
      	background-color: #000066; 
      	color: #ffffff; 
      	font-weight: bold; 
      	font-size: 10pt; 
      	font-family: verdana;
      }
      
      td{
      	font-size: 10pt; 
      	font-family:verdana;
      }
      
      .special{
      	border:1px solid #B0D96A; 
      	background-color: #FFFFFF; 
      	color: #000000;  
      	font-size: 10pt; 
      	font-family:verdana;
      }
      </style>
      
      <script type="text/javascript">
      function jump()
      {
      window.location ='index.php';
      }
      </script>
      </head>
      
      <?php
      include 'db_connect.php';
      session_start();   
      
      User avatar
      TechJammer
      New php-forum User
      New php-forum User
      Posts: 15
      Joined: Tue Nov 20, 2007 6:27 pm
      Location: Maryland, USA
      Contact:

      Wed Sep 17, 2008 7:27 pm

      Great, well that is your problem.

      session_start() must occur before ANY output to the web browser. So, it should be located in the first few lines of code. You will have to rearrange your script, to place the session_start at the very beginning of the script.

      example:

      Code: Select all

      <?php
      session_start()
      other php code...
      ...
      ?>
      
      <html>
      <head>
      <title>MEC</title>
      <style type="text/css">
      any other html code...
      
      celcomgirl
      php-forum Active User
      php-forum Active User
      Posts: 29
      Joined: Tue Sep 02, 2008 7:16 pm

      Wed Sep 17, 2008 7:58 pm

      TechJammer wrote:Great, well that is your problem.

      session_start() must occur before ANY output to the web browser. So, it should be located in the first few lines of code. You will have to rearrange your script, to place the session_start at the very beginning of the script.

      example:

      Code: Select all

      <?php
      session_start()
      other php code...
      ...
      ?>
      
      <html>
      <head>
      <title>MEC</title>
      <style type="text/css">
      any other html code...
      

      yes.. i got it. thanks ya..... :D
      celcomgirl
      php-forum Active User
      php-forum Active User
      Posts: 29
      Joined: Tue Sep 02, 2008 7:16 pm

      Thu Sep 18, 2008 4:18 pm

      ii TechJammer see again. thanks for the info. i think maybe i have miss something. i have done all those this that u said before this.actually the system i have done can run by using xampp at my pc. so now it put in server.then that warning appear.i confius because at my pc using xampp it can run but why not at server. after that i change xampp with wamp. and then same warning appear as at server. i go through with your topic to solve the problem. after that warning at my pc that use wamp can solve and i put the edited sytem to server. but at server the warning still appear. is it possible because of server is not same version with apache that i use in my pc.??
      User avatar
      TechJammer
      New php-forum User
      New php-forum User
      Posts: 15
      Joined: Tue Nov 20, 2007 6:27 pm
      Location: Maryland, USA
      Contact:

      Thu Sep 18, 2008 6:48 pm

      I am not familiar with xampp or wamp. However, the session_start has been in php for quite a while. I don't believe there should be any compatibility issues.

      Paste the error message you are receiving into this forum. Also, provide the php coding just as you did before.

      Also, make sure you uploaded the file to your server as text and not binary
      celcomgirl
      php-forum Active User
      php-forum Active User
      Posts: 29
      Joined: Tue Sep 02, 2008 7:16 pm

      Thu Sep 18, 2008 7:35 pm

      ii... TechJammer as you said before
      session_start() must occur before ANY output to the web browser. So, it should be located in the first few lines of code. You will have to rearrange your script, to place the session_start at the very beginning of the script.
      i found some white space at next page which index.php after i make changes, all warning disappear. thank god. i already tired to solve this problem and now its working. thank you very much :D :)
      Post Reply