do not understand Cannot send session cookie - headers already sent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • craigtomo
    New Member
    • Feb 2008
    • 8

    do not understand Cannot send session cookie - headers already sent

    i have changed my login .php file to the following

    this file is opened directly and is not called from any other file
    [PHP]<?php

    // dBase file
    $host=""; // Host name
    $username=""; // Mysql username
    $password=""; // Mysql password
    $db_name=""; // Database name
    $tbl_name=""; // Table name
    // Connect to server and select databse.
    mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
    mysql_select_db ("$db_name") or die("cannot select DB");
    session_start() ;

    if ($_GET["op"] == "login")
    {
    if (!$_POST["username"] || !$_POST["password"])
    {
    die("You need to provide a username and password.");
    }

    // Create query
    $q = "SELECT * FROM `dbUsers` "
    ."WHERE `username`='".$ _POST["username"]."' "
    ."AND `password`=PASS WORD('".$_POST["password"]."') "
    ."LIMIT 1";
    // Run query
    $r = mysql_query($q) ;

    if ( $obj = @mysql_fetch_ob ject($r) )
    {
    // Login good, create session variables
    $_SESSION["valid_id"] = $obj->id;
    $_SESSION["valid_user "] = $_POST["username"];
    $_SESSION["valid_time "] = time();

    // Redirect to member page
    Header("Locatio n: members.php");
    }
    else
    {
    // Login not successful
    die("Sorry, could not log you in. Wrong login information.");
    }
    }
    else
    {
    //If all went right the Web form appears and users can log in
    echo "<form action=\"?op=lo gin\" method=\"POST\" >";
    echo "Username: <input name=\"username \" size=\"15\"><br />";
    echo "Password: <input type=\"password \" name=\"password \" size=\"8\"><br />";
    echo "<input type=\"submit\" value=\"Login\" >";
    echo "</form>";
    }
    ?>[/PHP]

    this is the screen i am getting when i acces the page


    Warning: session_start() [function.sessio n-start]: Cannot send session cookie - headers already sent by (output started at D:\virtualserve rs\182763\root\ test\login\lo\l ogin.php:1) in D:\virtualserve rs\182763\root\ test\login\lo\l ogin.php on line 12

    Warning: session_start() [function.sessio n-start]: Cannot send session cache limiter - headers already sent (output started at D:\virtualserve rs\182763\root\ test\login\lo\l ogin.php:1) in D:\virtualserve rs\182763\root\ test\login\lo\l ogin.php on line 12


    i would like some help please in locateing the sourtce of the errors

    i know the errors are from the <?php in line one but therfe are NO spaces after this line only a return

    i get the error when i enter a correct or a false user name

    i know the database info i put in is correct as i can add data to the table fine

    thanks
  • craigtomo
    New Member
    • Feb 2008
    • 8

    #2
    i was also wondering if it could be a setup fault on my server as i have been reciveing the same sort of errors useing differant code

    all i am trying to do is create a database login

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      session_start() needs to be at the very top of your page! ALL THE TIME!
      [php]
      <?php
      session_start() ;

      // rest of code

      [/php]

      Comment

      • craigtomo
        New Member
        • Feb 2008
        • 8

        #4
        i have put the session start in as you suggested and this is now the error i am reciveng


        Warning: session_start() [function.sessio n-start]: Cannot send session cookie - headers already sent by (output started at D:\virtualserve rs\182763\root\ test\login\lo\l ogin.php:1) in D:\virtualserve rs\182763\root\ test\login\lo\l ogin.php on line 2

        Warning: session_start() [function.sessio n-start]: Cannot send session cache limiter - headers already sent (output started at D:\virtualserve rs\182763\root\ test\login\lo\l ogin.php:1) in D:\virtualserve rs\182763\root\ test\login\lo\l ogin.php on line 2

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Are you sure that there is no blank following the <?php. If so take it out.

          Ronald

          Comment

          • craigtomo
            New Member
            • Feb 2008
            • 8

            #6
            there is deffinetly no blanks after or before the <?php command

            could it be somethingto do with the setup on my server as i have been getting similar problems with lots of different types of script i have been trying?

            all i want is a simple login process

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              Something must have been outputted before the session_start() was executed. You sure that was all the code?

              Ronald

              Comment

              • craigtomo
                New Member
                • Feb 2008
                • 8

                #8
                Originally posted by ronverdonk
                Something must have been outputted before the session_start() was executed. You sure that was all the code?

                Ronald
                there is no other code


                that is why i think it might be on my server that is the problem

                the file is located at http://equifor.soon2b. net/test/login/lo/login.php

                please can you try and run it and see if you are getting the same errors i am

                it is really frustrateing me

                thanks

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  Originally posted by craigtomo
                  there is deffinetly no blanks after or before the <?php command

                  could it be somethingto do with the setup on my server as i have been getting similar problems with lots of different types of script i have been trying?

                  all i want is a simple login process
                  No line break too?

                  This error is only shown if you have sent something before the header is set.

                  Comment

                  • craigtomo
                    New Member
                    • Feb 2008
                    • 8

                    #10
                    there is nothing else in the file apart from what is shown

                    as i have said i have had similar problems with various files all with the same responce

                    Comment

                    • cyberforester
                      New Member
                      • Mar 2008
                      • 1

                      #11
                      Same thing is happening to me and it has me tearing my hair out.

                      I have downloaded and customised a script that allows users to enter a password in order to move to a part of the site I want protected from the general public.

                      I debugged the script and had it working on my development machine (fedora 7, apache and php 5.x). I thought i was doing great, and then I uploaded it to the production machine which is Windows 2k3, IIS 6 and Php 5.x. That's when the wheels came off and i start getting this error.

                      What is going on? I always seem to add extra to my development time by having to work around the Windows insanity.

                      Comment

                      • hsriat
                        Recognized Expert Top Contributor
                        • Jan 2008
                        • 1653

                        #12
                        Originally posted by cyberforester
                        Same thing is happening to me and it has me tearing my hair out.

                        I have downloaded and customised a script that allows users to enter a password in order to move to a part of the site I want protected from the general public.

                        I debugged the script and had it working on my development machine (fedora 7, apache and php 5.x). I thought i was doing great, and then I uploaded it to the production machine which is Windows 2k3, IIS 6 and Php 5.x. That's when the wheels came off and i start getting this error.

                        What is going on? I always seem to add extra to my development time by having to work around the Windows insanity.
                        If you are using Notepad, then open the file in another editor and remove the stuff before sending header.

                        Regards

                        Comment

                        Working...