This question already has an answer here:
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>
</div>