0

I'm doing my homework that a webpage. I created the index page as a login page that have a username input, password input and a login button.

When I click on the button, a php code (it have wroten below) have executed: it uses MySQL for login.

My problem with the login that the redirect. If the typed username and password are okay, the page should go to the next page (for example the next page's link is http://stackoverflow.com).

I use header("location:http://stackoverflow.com"); to go to the next page, but I have got the following error: Warning: Cannot modify header information - headers already sent by (output started at /users/kzr/www/index.php:60) in /users/kzr/www/index.php on line 14

I've already tried ob_start() with ob_end_flush(), but they didn't changed the situation: the error was same.

Can anybody help me to get work this code?

Note: the PHP and the HTML codes are in once file, but I separated them.

The PHP code:

if(isset($_POST["login"])){
    $username=$_POST["username"];
    $password=$_POST["password"];
    $connect=mysql_connect("127.0.0.1","kzr_zetr","350adagsertesborda");
    if($connect){
        $db=mysql_select_db("kzr_zetr");
        if($db){
            $query=mysql_query("SELECT * FROM login WHERE username = '$username' AND password = '$password'");
            $rowcount=mysql_num_rows($query);
            if($rowcount>0){
                $uri="http://kzr.bplaced.de/session/?username=".$username;
                header('Location:'.$uri);
                print 'sucess';
            }else{
                echo "<script type='text/javascript'>login_f_val_phperr();</script>";
                print 'fail';
            }
        }
    }
}

The HTML code:

<!DOCTYPE html>
<html>
<head>
    <title>Bejelentkezés | ZETR</title>
    <meta charset="utf-8" />
    <meta name="author" content="Kókai Z. Ronald" />
    <meta name="contact" content="zr.kokai@gmail.com" />
    <meta name="description" content="(Konrad) Zuse Elektronikus Tanulmányi Rendszer" />
    <meta name="keywords" content="konrad, zuse, konrad zuse, etr, rendszer, tanulmányi rendszer" />
    <meta name="subject" content="Alkalmazott Informatika Házi Feladat Varga István Tanár Úrnak" />
    <base href="http://kzr.bplaced.de" />
    <link rel="stylesheet" type="text/css" href="/f/font/ubuntu/ubuntu.css" />
    <link rel="stylesheet" type="text/css" href="/f/css/zetr.css" />
    <link rel="stylesheet" type="text/css" href="/f/css/login.css" />
    <link rel="shortcut icon" type="image/x-icon" href="/f/img/favicon.ico" />
    <script type="text/javascript" src="/f/script/login-f.js"></script>
</head>
<body>
<div id="container">
    <h1>Zuse</h1>
    <h2>Elektronikus Tanulmányi Rendszer</h2>
    <div id="loginbox">
        <form name="login-f" action="" onsubmit="return login_f_val()" method="post">
            <input type="text" name="username" id="username" placeholder="Felhasználónév" autocomplete="off" />
            <input type="password" name="password" id="password" placeholder="Jelszó" />
            <input type="submit" name="login" id="login" value="Bejelentkezés" />
        </form>
    </div>
</div>
</body>
</html>
Sam
  • 7,252
  • 16
  • 46
  • 65
Kokai Zoltan
  • 13
  • 1
  • 3
  • 2
    1. Your code is very vulnerable to SQL injection. Try to use `admin' --` as a username. Sudden you're logged in as admin (or any other user) without knowing the password. 2. Don't use `mysql_` - they've been deprecated for years. Use PDO or MySQLi with prepared statements and bind variables. 3. Don't use cleartext passwords in your database. Use `password_hash` instead (do **not** use `md5()` either) – h2ooooooo Apr 24 '14 at 13:04
  • 2
    **why can't you google fella?? 100 of solution for same error is present => http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php** – NoobEditor Apr 24 '14 at 13:07

3 Answers3

1

Try to put an exit after your header like this :

header(...);exit;

Goku
  • 2,157
  • 2
  • 16
  • 31
  • 2
    are you sure this is a solution??? error caused is due to `output`ed data before `header` i guess...and once `header` is sent, control won't go to `exit`, unless you disable `header` function via some hacking method!! – NoobEditor Apr 24 '14 at 13:13
  • The error comes from `print`. So 2 solutions : header();exit; or delete print. – Goku Apr 24 '14 at 13:25
0

headers need to be way above any HTML (even before doctype) and also before echoing ANY html. You will need to do it as high up as possible. And remove PRINT success and fail.

Peter Bennett
  • 184
  • 1
  • 2
  • 15
0

It means on line 60 of index.php something is already causing the headers to be sent; make sure the headers() call is the first thing you do before sending any HTML (or plaintext) output.

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111