Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by [duplicate]

Tags:

php

session

Possible Duplicate:
“Warning: Headers already sent” in PHP

I have an error that displays on my PHP login form, when I upload it to my server. It was working perfectly on my localhost, but when I upload it to server it gives me this error:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/shopping/public_html/biblioteka/index.php:10) in /home/shopping/public_html/biblioteka/index.php on line 45

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/shopping/public_html/biblioteka/index.php:10) in /home/shopping/public_html/biblioteka/index.php on line 45

Warning: Cannot modify header information - headers already sent by (output started at /home/shopping/public_html/biblioteka/index.php:10) in /home/shopping/public_html/biblioteka/index.php on line 49

And here is my PHP code:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="http://localhost/Shopping_biblioteka/css/style.css">
        <title>Title</title>
    </head>
    <body align="center">
        <div id="login">
        <?php
    
        if (isset($_POST['username']) && isset($_POST['password'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            mysql_connect("localhost", "user", "pass") or die(mysql_error());
            mysql_select_db("db") or die(mysql_error());
        mysql_query("SET CHARACTER SET utf8");
        mysql_query("SET NAMES utf8");
            $result = mysql_query("SELECT password,id FROM x9qg6_users
            where username='" . $username . "'");
            if (!$result) {
                echo 'Could not run query: ' . mysql_error();
                exit;
            }
            $row = mysql_fetch_row($result);
            $test = explode(":", $row[0]);
            $user_id=$row[1];
            $userhash = md5($password . $test[1]);
            if ($test[0] === $userhash) {       
                session_start();
                $_SESSION['login_user'] = $user_id;
                $_SESSION['username'] = $username;
                $url = "biblioteka.php";
                header("Location: $url");
            }
        } else {
            echo 'Внесете ги вашите податоци во полињата!';
        }
        // Connects to your Database 
        ?> 
        <form action="" method="POST" accept-charset="UTF-8"> 
            Корисничко име:<br/>
            <input name="username" id="username" type="text"/><br/>
            Лозинка:<br/>
            <input type="password" id="password" name="password"/><br/>
            <input type="submit" value="Логирај се!"/>
        </form>

        </div>
    </body>
</html>
like image 390
Chris Avatar asked Dec 11 '22 20:12

Chris


1 Answers

Just add ob_start() after session_start()

<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
    <head>
       ...
       ...
like image 175
Pintu Francis Avatar answered Feb 08 '23 22:02

Pintu Francis