Iam getting an errors like session_start[function.session-start]: How to solve this?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lkpsrinivasan
    New Member
    • Sep 2010
    • 2

    #1

    Iam getting an errors like session_start[function.session-start]: How to solve this?

    (lines from 40 to 71)

    My error while running the program is
    (Warning: session_start() [function.sessio n-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs \sri1\login.php :40) in C:\xampp\htdocs \sri1\login.php on line 40

    Warning: session_start() [function.sessio n-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs \sri1\login.php :40) in C:\xampp\htdocs \sri1\login.php on line 40)

    _______________ _______________ _______________ ____________

    coding:

    Code:
    <?php session_start();
       
       	include "connect.php";
    	
    	if(isset($_POST['b1']))
    	{
    	 $a1=$_POST["t1"];
    	 $a2=$_POST["t2"];
    	 
    	 if($a1!="" && $a2!="")
    	 {
    	  $query="SELECT * FROM user where name='$a1' && pwd='$a2' ";
    	  $result=mysql_query($query,$con);
    	  
    	  $fetch=mysql_fetch_array($result);
    	  $count=mysql_num_rows($result);
    	  
    	  if($count)
    	  {
    	   $_SESSION['uid']=$fetch[0];
    	   $_SESSION['pwd']=$fetch[1];
    	   header("location:ok.php");
    	  }
    	  
    	  else
    	  {
    	   echo "<script language='Javascript' type='text/javascript'> alert(Invalid User') </script>";
    	  }
    	  }
    	  }
    	  
    	  ?>
    Last edited by Niheel; Sep 3 '10, 05:42 AM. Reason: added code tags, please use code tags to display code
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    What are you doing between lines 1-39?

    session_start() ; needs to come before any output is sent to the browser.

    Comment

    • lkpsrinivasan
      New Member
      • Sep 2010
      • 2

      #3
      My full program from lines 1 to 77. Now reply me the solution...

      Code:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Untitled Document</title>
      </head>
      
      <script>
      function one()
      {
       var a1,a2;
       a1=f1.t1.value;
       a2=f1.t2.value;
       var statement="";
       var i=0;
       
       if((a1=="") | (a2==""))
       {
        i++;
        statement = i + ")* No Fields should be Empty \n"
       }
       
       if(statement!="")
       {
        alert(statement);
       }
       }
       </script>  
      <body>
      <p>&nbsp;</p>
      <p>Login Form</p>
      <form name=f1 method=post>
      Username <input type=text name=t1 /><br /><br />
        Password 
      <input type=text name=t2 /><br /><br />
         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input name="b1" type="button" id="b1" value="Login" onclick="one()"/>
         </form>
         
         
         <?php session_start();
         
         	include "connect.php";
      	
      	if(isset($_POST['b1']))
      	{
      	 $a1=$_POST["t1"];
      	 $a2=$_POST["t2"];
      	 
      	 if($a1!="" && $a2!="")
      	 {
      	  $query="SELECT * FROM user where name='$a1' && pwd='$a2' ";
      	  $result=mysql_query($query,$con);
      	  
      	  $fetch=mysql_fetch_array($result);
      	  $count=mysql_num_rows($result);
      	  
      	  if($count)
      	  {
      	   $_SESSION['uid']=$fetch[0];
      	   $_SESSION['pwd']=$fetch[1];
      	   header("location:ok.php");
      	  }
      	  
      	  else
      	  {
      	   echo "<script language='Javascript' type='text/javascript'> alert(Invalid User') </script>";
      	  }
      	  }
      	  }
      	  
      	  ?>
      	   
      	   
      	   
      	 
      </body>
      </html>
      Last edited by Niheel; Sep 3 '10, 05:42 AM. Reason: please use code tags to display code

      Comment

      • JKing
        Recognized Expert Top Contributor
        • Jun 2007
        • 1206

        #4
        As I said in my earlier post "session_start() ; needs to come before any output is sent to the browser. "

        You need to place your php code above your html.

        Comment

        • mwasif
          Recognized Expert Contributor
          • Jul 2006
          • 802

          #5
          session_start() must be placed before HTML. You other HTML may come after it but wit one exception i.e. header() should not be called after any output (the same rule as we had for session_start() ).

          Comment

          • mwasif
            Recognized Expert Contributor
            • Jul 2006
            • 802

            #6
            Moved to PHP forum.

            Comment

            • Angel Zois
              New Member
              • Sep 2010
              • 2

              #7
              Same problem , but i don't have anything before session-start.

              The Error: Warning: session_start() [function.sessio n-start]: Cannot send session cache limiter - headers already sent (output started at C:\webs\fr\new_ ma.php:1) in C:\webs\fr\new_ ma.php on line 2

              Code:
              <?php
              session_start();
              if (!isset($_SESSION['myusername']))
                header("Location: main_login.php");
              include('variables.php'); 
              include('header_ajax.php');
              if(isset($_POST['add']))
              {
              $conn = mysql_connect($dbhost, $dbuser, $dbpass);
              if(! $conn )
              {
                die('Error: ' . mysql_error());
              }
              .
              .
              .

              Comment

              • Angel Zois
                New Member
                • Sep 2010
                • 2

                #8
                I have found why i have the problem. I am using PSPAd Editor and i found this on www :

                If you are saving the file in UTF-8 format, then your editor may be saving a BOM (byte order mark) at the start of the file. Either change encoding or see if your editor has a setting to not include the BOM.

                Yes i am saving at UTF-8 and i had to disable the setting on Program setting "Ident.byte s in UTF-8 coding"

                So is not the code the proble , is the editor. If you still have the same problem and you have the session_start() ; at the first line without spaces check for you editor.

                Comment

                • kovik
                  Recognized Expert Top Contributor
                  • Jun 2007
                  • 1044

                  #9
                  Maybe for you, yeah. The OP's problem is probably the loads of HTML before session_start() . lol

                  If you want my opinion, use Notepad++. It's simple, extensible, and has a small footprint.

                  Comment

                  Working...