4

I've changed my hosting server from a Windows to a Linux system. But when I run my PHP program, I get this errors:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/p/y/c/francis/html/login/login.php:2) in /home/content/p/y/c/francis/html/login/login.php on line 4

and

 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/p/y/c/francis/html/login/login.php:2) in /home/content/p/y/c/francis/html/login/login.php on line 4

This is the code of my program:

<?php

session_start();

$username  = $_POST['username'];
$password  = $_POST['password'];

if ($username && $password)
{

$connect = mysql_connect(***,***,***);
mysql_select_db("phploginregister") or die("Couldn't find db");

$query = mysql_query("SELECT * FROM users WHERE username='$username'");

$numrows = mysql_num_rows($query);

if ($numrows != 0)
{

    while ($row = mysql_fetch_assoc($query))
    {
    $dbusername = $row['username'];
    $dbpassword = $row['password'];
    }

    //check to see if they match!
    if ($username == $dbusername && md5($password) == $dbpassword)
    {
    echo "You're in! <a href='member.php'>Click</a> here to enter the member page.";
    $_SESSION['username'] = $dbusername;    
    }

    else
        echo "Incorrect password";
}
else
    die("That user doens't exist!");

}
else
    die("Please enter an username and password");



?>

What is wrong in the code, because it workend fine on a Windows host...

francisMi
  • 925
  • 3
  • 15
  • 31

3 Answers3

4

You get the error because there are some output before you have initiated session_start(); This could be caused because of your editor that include a BOM character in the beginning of your file. Try open the code in notepad and see if there are any lines before session_start(), (spaces) or things like that and remove them.

To fix your editor if it add a bom in your file, you need to go to your settings and turn it off.

John
  • 2,900
  • 8
  • 36
  • 65
2

You have a leading BOM, new line or other whitespace character before the opening <?php tag.

The errors talk about line 2 and line 4, but in the actual code above session_start() is called on line 3. Therefore, leading whitespace is the problem...

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
-1

i think you should add

ob_start();

in the first of your code

and in the bottom add

ob_get_contents(); ob_end_flush();

because of

session send headers to server , also you added echo ( this also tell server its html with headers )

server now has to headers so use the ob_start(); and ob_end_flush(); to work :)

SamarLover
  • 182
  • 10