php session error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mishrarajesh44
    New Member
    • Nov 2007
    • 23

    php session error

    i am facing problem in php session..
    my code is giving error..
    i am giving my code and error message below..


    my code:-
    [code=php]
    <?php
    if(isset($_POST['submit']))
    { if(empty($passw ord)){echo "No password specified";}

    $connection=mys ql_connect("loc alhost","wwwuse r_raja","kalika ");
    if(!$connection )
    {echo"<b>connec tion not set...</b><br>";}

    $sel=mysql_sele ct_db("wwwuser_ raja");
    if(!$sel)
    {echo"<b>databa se not selected...</b><br>";}
    $query="select password from users where username='".$us ername."'";
    $result=mysql_q uery($query);
    if($row=mysql_f etch_array($res ult))
    {
    if(!(md5($passw ord)==$row["password"]))
    {echo "Wrong Password !";}


    }
    else
    {echo "<b>User does not exists !!</b>";}
    session_start() ;
    session_registe r("username") ;
    session_encode( );
    $url="Location: loggedin.php?PH PSESSID=".$PHPS ESSID;
    header($url);

    }
    ?>


    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <meta http-equiv="Content-Language" content="en-us" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Login</title>
    <style type="text/css">
    .style1 {
    margin-left: 0px;
    }
    .style2 {
    margin-left: 248px;
    margin-top: 126px;
    }
    </style>
    </head>

    <body>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" style="height: 202px; width: 321px" class="style2">
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; Enter&nbsp; your information&nbs p; in
    the form&nbsp; below&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; <br />
    &nbsp;<br />
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
    <br />
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp; Username
    <input name="username" type="text" maxlength="20" value="<?php if(isset($_POST['username'])) echo $_POST['username'];?>" />&nbsp;&nbsp;&n bsp;&nbsp; <br />
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; <br />
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp; Password
    <input name="password" type="password" maxlength="20" />&nbsp; <br />
    <br />
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
    &nbsp;&nbsp;&nb sp;&nbsp;
    <input name="submit" type="submit" value="Login" style="width: 40px; height: 20px" class="style1" /><br />
    </form>
    </body>

    </html>
    [/code]

    Error msg:-when i am giving the correct username and password..

    Warning: session_start() : Cannot send session cookie - headers already sent by (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:3) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 25 Warning: session_start() : Cannot send session cache limiter - headers already sent (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:3) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 25 Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:3) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 29

    can anybody help me...
    Last edited by Atli; Nov 21 '07, 08:18 PM. Reason: Fixed the width of the post.
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    Hi mishrarajesh44,
    Welcome to The Scripts. You mistakenly placed your question in the PHP Articles, so I moved it to the PHP Forum where I am sure you will receive a better response.
    Motoma

    Comment

    • brettl
      New Member
      • Sep 2007
      • 41

      #3
      Note: If you are using cookie-based sessions, you must call session_start() before anything is outputted to the browser.
      just add session_start() to the very first line of your page before anything is outputted to the browers.

      php.net session_start - Manual

      Comment

      • andho
        New Member
        • Sep 2007
        • 34

        #4
        remove the lines with these code:

        Code:
        session_start();
        session_register("username");
        session_encode();
        $url="Location:loggedin.php?PHPSESSID=".$PHPSESSID;
        header($url);
        so you can see if your logic is actually working. maybe the code is echoing something even when you give the correct username and password.

        That error message is caused if any output is given (whitespace not within the <php tags included) before the call to session_start() or header() function.

        I once had this problem with the fckeditor code and couldn't find any extra whitespace in the entirety of the fckeditor library, and i have never found out what the problem was.

        EDIT:
        Quote:
        Originally Posted by
        Note: If you are using cookie-based sessions, you must call session_start() before anything is outputted to the browser.


        just add session_start() to the very first line of your page before anything is outputted to the browers.
        Brettl i think that might have been the problem that i encountered.

        Comment

        • mishrarajesh44
          New Member
          • Nov 2007
          • 23

          #5
          php session error

          i am verified my code by putting session_start() at the beginning , still getting error..


          mycode:-

          [code=php]
          <?php
          session_start() ;
          //session_registe r("username") ;
          //session_encode( );
          // $url="Location: loggedin.php?PH PSESSID=".$PHPS ESSID;
          // header($url);
          if(isset($_POST['submit']))
          { if(empty($passw ord)){echo "No password specified";}

          $connection=mys ql_connect("loc alhost","wwwuse r_raja","kalika ");
          if(!$connection )
          {echo"<b>connec tion not set...</b><br>";}

          $sel=mysql_sele ct_db("wwwuser_ raja");
          if(!$sel)
          {echo"<b>databa se not selected...</b><br>";}
          $query="select password from users where username='".$us ername."'";
          $result=mysql_q uery($query);
          if($row=mysql_f etch_array($res ult))
          {
          if(!(md5($passw ord)==$row["password"]))
          {echo "Wrong Password !";}


          }
          else
          {echo "<b>User does not exists !!</b>";}
          // session_start() ;
          // session_registe r("username") ;
          // session_encode( );
          // $url="Location: loggedin.php?PH PSESSID=".$PHPS ESSID;
          // header($url);

          }

          ?>
          [/code]

          error:-
          Code:
          Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:1) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 2 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:1) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 2
          the line 2 is ..session_start ();


          i am not getting ..the username and password r correct, i have checked it by removing sesssion parts..
          anyone can find the bogs..
          Last edited by Atli; Nov 21 '07, 10:10 PM. Reason: Added [code] tags.

          Comment

          • andho
            New Member
            • Sep 2007
            • 34

            #6
            why are you posting on a new thread??

            i would recommend editing that file with a test editor that: strips all whitespace from the start and end of the file. i dont know any free software that does that. Zend studio does it.

            Comment

            • mishrarajesh44
              New Member
              • Nov 2007
              • 23

              #7
              Originally posted by andho
              why are you posting on a new thread??

              i would recommend editing that file with a test editor that: strips all whitespace from the start and end of the file. i dont know any free software that does that. Zend studio does it.

              ok,sorry andho..

              i am giving again my previous codes..
              [code=php]
              1.
              <?php
              2.
              if(isset($_POST['submit']))
              3.
              { if(empty($passw ord)){echo "No password specified";}
              4.

              5.
              $connection=mys ql_connect("loc alhost","wwwuse r_raja","kalika ");
              6.
              if(!$connection )
              7.
              {echo"<b>connec tion not set...</b><br>";}
              8.

              9.
              $sel=mysql_sele ct_db("wwwuser_ raja");
              10.
              if(!$sel)
              11.
              {echo"<b>databa se not selected...</b><br>";}
              12.
              $query="select password from users where username='".$us ername."'";
              13.
              $result=mysql_q uery($query);
              14.
              if($row=mysql_f etch_array($res ult))
              15.
              {
              16.
              if(!(md5($passw ord)==$row["password"]))
              17.
              {echo "Wrong Password !";}
              18.

              19.

              20.
              }
              21.
              else
              22.
              {echo "<b>User does not exists !!</b>";}
              23.
              session_start() ;
              24.
              session_registe r("username") ;
              25.
              session_encode( );
              26.
              $url="Location: loggedin.php?PH PSESSID=".$PHPS ESSID;
              27.
              header($url);
              28.

              29.
              }
              30.
              ?>
              31.

              32.

              33.
              <html xmlns="http://www.w3.org/1999/xhtml">
              34.

              35.
              <head>
              36.
              <meta http-equiv="Content-Language" content="en-us" />
              37.
              <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
              38.
              <title>Login</title>
              39.
              <style type="text/css">
              40.
              .style1 {
              41.
              margin-left: 0px;
              42.
              }
              43.
              .style2 {
              44.
              margin-left: 248px;
              45.
              margin-top: 126px;
              46.
              }
              47.
              </style>
              48.
              </head>
              49.

              50.
              <body>
              51.

              52.
              <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" style="height: 202px; width: 321px" class="style2">
              53.
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; Enter&nbsp; your information&nbs p; in
              54.
              the form&nbsp; below&nbsp;&nbs p;&nbsp;&nbsp;& nbsp;
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; <br />
              55.
              &nbsp;<br />
              56.
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp;
              57.
              <br />
              58.
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp; Username
              59.
              <input name="username" type="text" maxlength="20" value="<?php if(isset($_POST['username'])) echo $_POST['username'];?>" />&nbsp;&nbsp;&n bsp;&nbsp; <br />
              60.
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; <br />
              61.
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp; Password
              62.
              <input name="password" type="password" maxlength="20" />&nbsp; <br />
              63.
              <br />
              64.
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
              &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
              &nbsp;&nbsp;&nb sp;&nbsp;
              65.
              <input name="submit" type="submit" value="Login" style="width: 40px; height: 20px" class="style1" /><br />
              66.
              </form>
              67.
              </body>
              68.

              69.
              </html>
              [/code]


              Error msg:-when i am giving the correct username and password..
              Code:
              Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:3) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 25 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:3) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 25 Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php:3) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/login.php on line 29
              waiting for your rply..
              Last edited by Atli; Nov 21 '07, 10:17 PM. Reason: Added [code] tags.

              Comment

              • mwasif
                Recognized Expert Contributor
                • Jul 2006
                • 802

                #8
                It would be better if you use PHP code tags around your code. This will enhance your code's readability. These tags are available when you post the comment. e.g.
                [PHP]<?php
                echo "It is a good idea to use PHP tags to format PHP code";
                ?>[/PHP]

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Hi mishrarajesh44.

                  Please do not double post your questions, or create new threads to respond to a question in another thread. I have merged the two threads you created to avoid any confusion.

                  Also, as mwasif already pointed out, please use &#91;code] tags when posting your code examples. (See How to ask a question)

                  &#91;code=ph p]PHP code goes here &#91;/code]

                  Thank you.

                  Comment

                  • mishrarajesh44
                    New Member
                    • Nov 2007
                    • 23

                    #10
                    Originally posted by andho
                    why are you posting on a new thread??

                    i would recommend editing that file with a test editor that: strips all whitespace from the start and end of the file. i dont know any free software that does that. Zend studio does it.
                    hiii ..

                    i was just checking to find out error in code,

                    even the only session_start( ) gives error...

                    code:-

                    [CODE=PHP]
                    <?php
                    session_start() ;
                    ?>
                    [/CODE]

                    ERROR MESSAGE:-

                    Warning: session_start() : Cannot send session cookie - headers already sent by (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/only_check.php: 1) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/only_check.php on line 2 Warning: session_start() : Cannot send session cache limiter - headers already sent (output started at /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/only_check.php: 1) in /hsphere/local/home/wwwuser/home.mishra.biz/addressbook/only_check.php on line 2

                    it may help u to find out my problem..
                    waiting for reply..

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      Is there a new-line character, a space, or anything else before you open the <?php tags?

                      The session_start() function needs to be called before anything is echoed, or added in any other way to the response. Once you start adding content to the response, the headers are locked and the session cookie can not be added, thus causing the session to fail.

                      If non of that is the problem, is it possible that your HTTP server is somehow set up to automatically include something on all pages, and that this page could be adding to the response?

                      Comment

                      • mishrarajesh44
                        New Member
                        • Nov 2007
                        • 23

                        #12
                        Originally posted by Atli
                        Is there a new-line character, a space, or anything else before you open the <?php tags?

                        The session_start() function needs to be called before anything is echoed, or added in any other way to the response. Once you start adding content to the response, the headers are locked and the session cookie can not be added, thus causing the session to fail.

                        If non of that is the problem, is it possible that your HTTP server is somehow set up to automatically include something on all pages, and that this page could be adding to the response?

                        hii.

                        i got the problem...
                        it was the text editer problem..
                        previously i was doing in "microsoft expression web",
                        now the same code does not show error when typed in "edit plus"..
                        thanks all for your support..

                        Comment

                        Working...