-2

I have created a php login form, which works fine using xampp & phpmyadmin,

but when I upload it to my server, I can register a user but get the following error when trying to log in.

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /php/index.php:9) in /php/login.php on line 24

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /php/index.php:9) in /php/login.php on line 24

Warning: Cannot modify header information - headers already sent by (output started at /php/index.php:9) in /php/login.php on line 28

i have php included the login form on the index.php page

the login.php code is...

    <?php include("dbconnect.php"); ?>

<?php
if(isset($_POST["submit1"])){

if(!empty($_POST['user']) && !empty($_POST['pass'])) {
    $user=$_POST['user'];
    $pass=$_POST['pass'];



    $query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'");
    $numrows=mysql_num_rows($query);
    if($numrows!=0)
    {
    while($row=mysql_fetch_assoc($query))
    {
    $dbusername=$row['username'];
    $dbpassword=$row['password'];
    }

    if($user == $dbusername && $pass == $dbpassword)
    {
    session_start();
    $_SESSION['sess_user']=$user;

    /* Redirect browser */
    header("Location: member.php");
    }
    } else {
    echo "Invalid username or password!";
    }

} else {
    echo "All fields are required!";
}
}
?>

Thank you

DevStacker
  • 675
  • 10
  • 23
  • 1
    Are you sure that this is all the code in that page? The errors means that your output something (like `echo`) prior to the `session_start()` line. – Ofir Baruch Apr 21 '15 at 06:46
  • Add your code as text to your question not as a link – Jens Apr 21 '15 at 06:47
  • Yes, that is everything on that page, I do have this page included on index.php, I also have another 5 or 6 different includes on index.php (if that has anything to do with it) – DevStacker Apr 21 '15 at 06:47
  • 1
    Empty line at the beginning? http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – baf Apr 21 '15 at 06:48

2 Answers2

1

use ob_start() - Turn on output buffering

This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

Bushra Shahid
  • 781
  • 7
  • 20
1

session_start(); should be first line of your code file.

 <?php



    if(isset($_POST["submit1"])){
    session_start(); 
    include("dbconnect.php"); 

    if(!empty($_POST['user']) && !empty($_POST['pass'])) {
        $user=$_POST['user'];
        $pass=$_POST['pass'];



        $query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'");
        $numrows=mysql_num_rows($query);
        if($numrows!=0)
        {
        while($row=mysql_fetch_assoc($query))
        {
        $dbusername=$row['username'];
        $dbpassword=$row['password'];
        }

        if($user == $dbusername && $pass == $dbpassword)
        {

        $_SESSION['sess_user']=$user;

        /* Redirect browser */
        header("Location: member.php");
        }
        } else {
        echo "Invalid username or password!";
        }

    } else {
        echo "All fields are required!";
    }
    }
kamlesh.bar
  • 1,774
  • 19
  • 38
  • I have tried this but it still does not work, i am getting the same errors, but this time, before i press login, – DevStacker Apr 21 '15 at 07:19
  • okay can you please paste login form code and dbconnect file code. also make sure there should be no space before php start tag php – kamlesh.bar Apr 21 '15 at 07:25
  • I have added all code to this url http://notepad.cc/wieveko89, there is the login.php, dbconnect.php & member.php – DevStacker Apr 21 '15 at 07:33
  • login.php remove space before – kamlesh.bar Apr 21 '15 at 07:36
  • all spaces have been removed, the error still occurs Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /php/index.php:9) in /hp/login.php on line 2 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /php/index.php:9) in /php/login.php on line 2 – DevStacker Apr 21 '15 at 07:39
  • @user2103072 just edited my code can you please try now – kamlesh.bar Apr 21 '15 at 07:46
  • The error is still coming up, when the login button is pressed, the errors appear. – DevStacker Apr 21 '15 at 07:51