PHP error

Associate
Joined
19 Oct 2005
Posts
528
Im trying to create a log in script but when i run it i get this error.

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/monkey/public_html/login.php:8) in /home/monkey/public_html/login.php on line 9

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/monkey/public_html/login.php:8) in /home/monkey/public_html/login.php on line 9


The code i am using is this i have changed it to refer to my database and forms. thankyou for any help.

Code:
<?php
// we must never forget to start the session
session_start();

$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
   include 'library/config.php';
   include 'library/opendb.php';

   $userId = $_POST['txtUserId'];
   $password = $_POST['txtPassword'];

   // check if the user id and password combination exist in database
   $sql = "SELECT user_id
           FROM tbl_auth_user
           WHERE user_id = '$userId'
                 AND user_password = PASSWORD('$password')";

   $result = mysql_query($sql)
             or die('Query failed. ' . mysql_error());

   if (mysql_num_rows($result) == 1) {
      // the user id and password match,
      // set the session
      $_SESSION['db_is_logged_in'] = true;

      // after login we move to the main page
      header('Location: main.php');
      exit;
   } else {
      $errorMessage = 'Sorry, wrong user id / password';
   }

   include 'library/closedb.php';
}
?>
 
ferretmaster said:
Im trying to create a log in script but when i run it i get this error.

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/monkey/public_html/login.php:8) in /home/monkey/public_html/login.php on line 9

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/monkey/public_html/login.php:8) in /home/monkey/public_html/login.php on line 9


The code i am using is this i have changed it to refer to my database and forms. thankyou for any help.

the first line of this script should be ob_Start();

and right before the header you should do ob_end_clean();

basically you cannot do header after you have output HTML. so if you rewrote the script by moving header line right after session start, it would work. bit pointless but thats just showing where the error is
 
Last edited by a moderator:
Code:
<?php
// we must never forget to start the session
//HERE would go ob_start(); without comments
session_start();

$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
   include 'library/config.php';
   include 'library/opendb.php';

   $userId = $_POST['txtUserId'];
   $password = $_POST['txtPassword'];

   // check if the user id and password combination exist in database
   $sql = "SELECT user_id
           FROM tbl_auth_user
           WHERE user_id = '$userId'
                 AND user_password = PASSWORD('$password')";

   $result = mysql_query($sql)
             or die('Query failed. ' . mysql_error());

   if (mysql_num_rows($result) == 1) {
      // the user id and password match,
      // set the session
      $_SESSION['db_is_logged_in'] = true;

      // after login we move to the main page
//here would go ob_end_clean();
      header('Location: main.php');
      exit;
   } else {
      $errorMessage = 'Sorry, wrong user id / password';
   }

   include 'library/closedb.php';
}
?>
 
Back
Top Bottom