Session_start() result in headers already sent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • The1corrupted
    New Member
    • Feb 2007
    • 134

    Session_start() result in headers already sent

    Alright, I seriously do NOT understand sessions what so ever. Whenever I try to transfer data from page to page, the [PHP]session_start() ;[/PHP] is always killed because of some sort of cookie/cache limiter. I even tried the [PHP]session_registe r();
    session_is_regi stered();[/PHP] script and it still blows up. I'm totally lost.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    What is the error returned by the server, what is blown up and how?

    Ronald :cool:

    Comment

    • The1corrupted
      New Member
      • Feb 2007
      • 134

      #3
      Oh, it just loves to blow up on my log in.

      [PHP]//Compile it in one triple if-statement, comparing the username, password, and type
      if ($array[2] == $pass) {
      //Success for the first if statement
      echo "Username is clear!<br>";
      if ($array[1] == $user) {
      //Success for the second if statement
      echo "Password is clear!<br>";
      if ($array[15] == 1) {
      //If the user is type 1...
      echo "Type 1 is clean!<br>
      <META HTTP-EQUIV='refresh' content='30; url=more1.php'> ";
      } elseif ($array[15] == 2) {
      //If the user is type 2...
      echo "Type 2 is clean!<br>
      mysql_close($ta blesummon);
      <META HTTP-EQUIV='refresh' content='5; url=more2.php'> ";
      } else {
      //If it all fails and dies (bad user)...
      echo "User type FAILED<br><br>U ser Type: ".$type."<b r>
      User Name: ".$user."<b r>
      User Password: ".$pass."<b r>";
      }
      }
      }
      [/PHP]
      Whenever I try to start a session in one of those if statements, it comes back with..

      Warning: session_start() [function.sessio n-start]: Cannot send session cache limiter - headers already sent (output started at C:\Vardaes\user _conn.php:9) in C:\Vardaes\pass check.php on line #

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        You get this message because you have already (somewhere earlier in your script) outputted something to the screen. The following extract article will explain how this can happen.

        I will show the text of an article by wildteen88, june 2006, who can explain this a lot better then I can.

        The reason why you are getting this message is because you have may have/be:
        • Whitespace before the opening php tag <?php
        • Outputting something to the browser before you use session_start, header, setcookie etc

        session_start, setcookie, header and a few other functions write header information to the web server/browser what to do. Such as when you use session_start it requests the browser to create a cookie which stores the PHPSESSID.

        If you have output before you use these functions then they are unable to send new header information as it has already been sent in the form text/html, and so you get the headers already sent error message.

        You can easily find the source of the problem by looking at the error message. As the answer is actully in there, but most people dont notice it as they may not understand what the error means. So lets take this error message as an example:

        Warning: Cannot modify header information - headers already sent by (output started at C:\server\www\t est.php:6) in C:\server\www\t est.php on line 8
        Now PHP has given us a clue here as it has told use where the output has started! Have look where it says output started at and it gives you the full path to the file and the line number. Now the line number that it stats is not usually where the output has started but where the output has ended, becuase the output could be above that line.
        So lets look at test.php:
        Code:
         1 <html>
         2 <head>
         3 <title>Header test</title>
         4 </head>
         5 <body>
         6 <?php
         7
         8 header("Location: http://www.google.com");
         9
        10 ?>
        11 </body>
        12 </html>
        As you can see line 6 is the opening PHP tag (< ?php) this isn't the output but look above that line you'll notice it has some HTML code. This html code is the cause of the error!

        So when you get this error message again look for the clue where it says output started at and goto the file and line number it says. Look above the line number and find where your output is located to.

        Hope that helps you understand why your are getting this error message. (Thanks to wildteen88)

        In your case: why don't you just put the session_start() statement at the very beginning of your PHP script?

        Ronald :cool:

        Comment

        • The1corrupted
          New Member
          • Feb 2007
          • 134

          #5
          That killed the error message, now how do I use sessions to transfer data from one page to the next?

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Just the standard way:

            [php]
            To store values
            session_start() ;
            $_SESSION['name'] = $whatever;

            to read it out in another page:

            session_start() ;
            $var = $_SESSION['name'];
            [/php]

            Ronald :cool:

            Comment

            • The1corrupted
              New Member
              • Feb 2007
              • 134

              #7
              Sweet! Thanks a ton!

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                You are welcome.

                Ronald :cool:

                Comment

                • The1corrupted
                  New Member
                  • Feb 2007
                  • 134

                  #9
                  Great... um.. relavent question... How do I change data from page to page? For instance...
                  [PHP]
                  if ($dir == 1) {
                  ++$x;
                  echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
                  }
                  elseif ($dir == 3) {
                  --$x;
                  echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
                  }
                  //Changing the Y coord
                  if ($dir == 2) {
                  ++$y;
                  echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
                  }
                  elseif ($dir == 4) {
                  --$y;
                  echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
                  }
                  //Changing the Z coord
                  if ($dir == 5) {
                  ++$z;
                  echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
                  }
                  elseif ($dir == 6) {
                  --$z;
                  echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
                  }
                  [/PHP]
                  The numbers, somehow, don't change even though those equal the session variables...

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    The numbers, somehow, don't change even though those equal the session variables...
                    You lost me. What is the relation of the $x and $y numbers to the data that is stored in the $_SESSION array?

                    Ronald :cool:

                    Comment

                    • The1corrupted
                      New Member
                      • Feb 2007
                      • 134

                      #11
                      Originally posted by ronverdonk
                      You lost me. What is the relation of the $x and $y numbers to the data that is stored in the $_SESSION array?

                      Ronald :cool:
                      [PHP]$x=$_SESSION['xcoord'];
                      $y=$_SESSION['ycoord'];
                      $z=$_SESSION['zcoord'];[/PHP]

                      So do I have to input this
                      [PHP]
                      ++$_SESSION['xcoord'];
                      [/PHP]

                      in order to simply add one to the $x or can I do it like so:

                      [PHP]
                      ++$x[/PHP]

                      Oh, and these numbers have everything to do with everything working right.

                      Comment

                      • ronverdonk
                        Recognized Expert Specialist
                        • Jul 2006
                        • 4259

                        #12
                        You mix up the 2 completely different variables that are used.

                        $x is a local (local to your script) variable and can only be addressed within that script.
                        $_SESSION['xcoord'] is a variable in a global array and is accessable to every script that has access to that instance of the array (e.g. after doing a session_start() ).

                        The statement [php]$x=$_SESSION['xcoord'];[/php] establishes no relation between the two. It only assigns the value of (independent) array variable $_SESSION['xcoord'] to the independent variable $x.
                        So when you increase variable $x, array variable $_SESSION['xcoord'] is not affected and vice versa!

                        When you want to keep 1 central x-coord. counter for all pages, you better use the ++$_SESSION['xcoord'].

                        Ronald :cool:

                        Comment

                        • The1corrupted
                          New Member
                          • Feb 2007
                          • 134

                          #13
                          It LIIVES!!

                          Thanks again.

                          Now.. new question on sessions... Can I use them to make a list of active users to be echoed on a new page (a who-is-online list)?

                          Comment

                          • ronverdonk
                            Recognized Expert Specialist
                            • Jul 2006
                            • 4259

                            #14
                            Of course you can. The $_SESSION array can be used for anything (variable wise) you like. It can hold a lot of data!

                            Ref. your question: ..... a more practical issue: how do you know that a user is gone? What if he does not log out but just closes his browser?

                            Ronald :cool:

                            Comment

                            • The1corrupted
                              New Member
                              • Feb 2007
                              • 134

                              #15
                              I'm going to have a session idle out jobby using time stamps when I get everything else set up.

                              Comment

                              Working...