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...

6
  • 1
    Are you including this file anywhere? You need to make sure there is no output sent before session_start. Your Windows server probably had output buffering configured, that's why it didn't give an error. Commented Sep 15, 2011 at 15:07
  • 3
    You're probably dealing with a BOM: stackoverflow.com/questions/2558172/… Commented Sep 15, 2011 at 15:10
  • 1
    SOLVED ! The problem was a space/newline before the script Commented Sep 15, 2011 at 15:12
  • @Quasdunk with a BOM output is started on line 0 Commented Sep 15, 2011 at 15:12
  • @user947093 post this as an answer Commented Sep 15, 2011 at 15:12

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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...

Comments

-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 :)

1 Comment

ob_end_flush() is enough, no need to call ob_get_contents(), which returns the ob content

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.