PHP Tutorial Register and Login
Posted: Sun Nov 18, 2012 5:24 pm
Hi, please help me i have this error:
Warning: Cannot modify header information - headers already sent by (output started at C: \ Downloads \ domains \ Registration.local \ Core \ inc \ user.inc.php: 1) in C: \ Downloads \ domains \ Registration.local \ register.php on line 22
and
Warning: Cannot modify header information - headers already sent by (output started at C:\Downloads\domains\Registration.local\Core\inc\user.inc.php:1) in C:\Downloads\domains\Registration.local\login.php on line 20
Register.php
Warning: Cannot modify header information - headers already sent by (output started at C: \ Downloads \ domains \ Registration.local \ Core \ inc \ user.inc.php: 1) in C: \ Downloads \ domains \ Registration.local \ register.php on line 22
and
Warning: Cannot modify header information - headers already sent by (output started at C:\Downloads\domains\Registration.local\Core\inc\user.inc.php:1) in C:\Downloads\domains\Registration.local\login.php on line 20
Register.php
<?php
include('Core/init.inc.php');
if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password']))
{
if (empty($_POST['username'])){
$errors[] = 'The username cannot be empty.';
}
if (empty($_POST['password']) || empty($_POST['repeat_password'])){
$errors[] = 'The password cannot be empty.';
}
if ($_POST['password'] !== $_POST['repeat_password']){
$errors[] = 'Password verfication failed.';
}
if (user_exists($_POST['username'])){
$errors[] = 'The username you enetered is already taken.';
}
if (empty($errors) == false){
add_user($_POST['username'], $_POST['password']);
$_SESSION['username'] = htmlentities($_POST['username']);
header('Location: protected.php');
die();
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<link href="Ext/Css/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div>
<?php
if (empty($errors) === false){
?>
<ul>
<?php
foreach($errors as $error){
echo"<li>{$error}</li>";
}
?>
</ul>
<?php
}
?>
</div>
<form action="" method="post">
<p>
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="<?php if(isset($_POST['username'])) echo htmlentities ($_POST['username']); ?>"/>
</p>
<p>
<label for="username">Password:</label>
<input type="password" name="password" id="password"/>
</p>
<p>
<label for="username">Repeat Password:</label>
<input type="password" name="repeat_password" id="repeat_password"/>
</p>
<p>
<input type="submit" value="Register" />
</p>
</form>
</body>
</html>
login.php<?php
include('Core/init.inc.php');
$errors = array ();
if(isset($_POST['username'], $_POST['password'])){
if (empty($_POST['username'])){
$errors[] = 'The username cannot be empty.';
}
if (empty($_POST['password'])){
$errors[] = 'The password cannot be empty.';
}
if (valid_credentials($_POST['username'],sha1($_POST['password']))=== false){
$errors[] = 'Username/Password incorect.';
}
if (empty($errors)){
if (isset($_POST['set_cookie']) && $_POST['set_cookie'] =='1'){
set_cookie('username', $_POST['username'], time()+604800);
set_cookie('password', sha1($_POST['password']), time()+604800);
}
$_SESSION['username'] = htmlentities($_POST['username']);
header('Location:protected.php');
die();
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<link href="Ext/Css/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div>
<?php
if (empty($errors) === false){
?>
<ul>
<?php
foreach ($errors as $error){
echo "<il>{$error}</il>";
}
?>
</ul>
<?php
}else{
echo 'Need an account? <a href="register.php">Register here</a>';
}
?>
</div>
<form action="" method="post">
<p>
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="<?php if(isset($_POST['username'])) echo htmlentities ($_POST['username']); ?>"/>
</p>
<p>
<label for="username">Password:</label>
<input type="text" name="password" id="password"/>
</p>
<p>
<label for="set_cookie">Remeber Me:</label>
<input type="checkbox" name="set_cookie" id="set_cookie" value="1"/>
</p>
<p>
<input type="submit" value="Login">
</p>
</form>
</body>
</html>