Page 1 of 1

SESSION_START() Cannot send session cookie, ERROR

Posted: Mon Oct 19, 2009 4:11 pm
by sumittyagi
I am trying to use sessions in my program, but I got these errors

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/schedfox/public_html/test/Login.php:2) in /home/schedfox/public_html/test/Login.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/schedfox/public_html/test/Login.php:2) in /home/schedfox/public_html/test/Login.php on line 2

any idea why I am getting this error, any help is highly appreciated.
Below is my code.

Code: Select all

 
<?php  session_start(); ?>
<!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">
<script language="Javascript">
function LoginAction()
{
    document.form1.action = "Login.php?action=loginuser";
    //alert ("hi");
    document.form1.submit();
    
   
}
</script>
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
 
<body>
 
  <?php
  $connection = pg_connect("hostaddr=FOOBAR port=5432 dbname=FOOBAR user=FOOBAR password=FOOBAR");
    if (!$connection) {
        echo("Connection Failed.");
        exit;
    }
  $action = trim($_GET['action']);
  
  
  ?>
 
<form id="form1" name="form1" method="post">
  <p>
    <label>UserName
      <input type="text" name="UserName" id="UserName" />
    </label>
  </p>
  <p>
    <label>Password
      <input type="password" name="Password" id="Password" />
    </label>
  </p>
  <p>
    <label>Submit
      <input type="submit" name="Submit" id="Submit" value="Submit" onClick="LoginAction()" />
    </label>
  </p>
  <?php
 
  if($action=="loginuser"){
    $user = $_POST['UserName'];
    $pass = $_POST['Password'];
    $encPassword = md5($pass);
    $query = "select user_password from control_db.user where user_login = '". $user . "' ";
    $query_user = "select user_login from control_db.user where user_login = '". $user . "' ";
    $result_user = pg_query($query_user);
    $i =0;
    while($myrow_user = pg_fetch_assoc($result_user))
    {
        $i++;
    }
    if($i== 0){
        echo "username does not exist";
    }
    else{
        
    
    
    $result = pg_query($query);
    while($myrow = pg_fetch_assoc($result)) { 
    $storedPassword = $myrow['user_password'];
    
    if($storedPassword == $encPassword){
        echo "correct username and password";
        $_SESSION['user_login'] = $_POST['UserName'];
        Header("Location: MainPage.php");
    }
    else {
        echo "Wrong password";
    }
    
    
    }
    
    }
      
  }
  
  
  ?>
  
  
  <p>&nbsp;</p>
  <p>&nbsp;</p>
</form>
</body>
</html>
 
 
Thanks in advance.

Re: Php SESSION_START() error

Posted: Mon Oct 19, 2009 4:34 pm
by John Cartwright
Moved to PHP-Code.

Re: Php SESSION_START() error

Posted: Mon Oct 19, 2009 4:52 pm
by sumittyagi
what do you mean, I am new to this forum, but can you tell clearly please!!

Re: Php SESSION_START() error

Posted: Mon Oct 19, 2009 4:53 pm
by John Cartwright
sumittyagi wrote:what do you mean, I am new to this forum, but can you tell clearly please!!
You posted in the incorrect forum.. I have moved it to the appropriate one, "PHP-Code".

Please read the forum descriptions before posting. Hint, the General Discussion forum clearly states that forum is NOT for programming related questions.

Re: SESSION_START() Cannot send session cookie, ERROR

Posted: Mon Oct 19, 2009 4:59 pm
by sumittyagi
ok..I am sorry, thanks for your guidance on that, but by any chance do you really know the problem I asked in the question

Re: SESSION_START() Cannot send session cookie, ERROR

Posted: Mon Oct 19, 2009 5:05 pm
by John Cartwright
No problem, just try and follow the guidelines in the future :)

Try removing any extra whitespace before session_start() and before the opening php tag. Absolutely nothing can be sent to the browser prior to starting a session.

Re: SESSION_START() Cannot send session cookie, ERROR

Posted: Mon Oct 19, 2009 5:24 pm
by sumittyagi
I tried it, their is nothing before those tags, is there any special way to remove the white spaces, coz, I tried to open it in VI editor, and their is no white space I can see, but may be I dont know how to remove the white space, ANY idea....