Jump to content

Login page help - header('Location: index.php');


saamde
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hello!

 

So iv almost finished my login page, when i went to test the page i'm getting this error. 

 

Warning: Cannot modify header information - headers already sent by (output started at /var/sites/c/cheatbook.co.uk/public_html/test/login.php:4) in/var/sites/c/cheatbook.co.uk/public_html/test/login.php on line 29

 

Heres my code for the login page.

<?php include 'core/init.php';?>
<!DOCTYPE html>
<html lang="en">
<?php include 'includes/head.php';?>
<body>
<?php include 'includes/header.php'; ?> 

<div class="container">
<h2>Login</h2>
<hr>

<?php
if (empty($_POST) === false) { 
	$username = $_POST['username']; 
	$password = $_POST['password']; 
	
	if (empty($username) === true || empty($password) === true) {
		$errors[] = '<div class="alert alert-info"> You need to enter a username and password </div>';
	}	else if (user_exists($username) === false) {
		$errors[] = '<div class="alert alert-danger">We can\'t find that username. Have you registered?</div>';
	} else if (user_active($username) === false) {
		$errors[] = '<div class="alert alert-warning">You haven\'t activated your account!</div>';
	} else {
		$login = login($username, $password);
		if ($login === false) {
			$errors[] = '<div class="alert alert-info"> That username/password combination is incorrect. </div>';
		} else {
			$_SESSION['user_id'] = $login;
			header('Location: index.php');
			exit();
		}
	}	
	print_r($errors);
}
?>
<form action="login.php" method="post" class="form-horizontal" role="form">
  <div class="form-group">
    <label for="username" class="col-lg-1 control-label">Username:</label>
    <div class="col-lg-10">
      <input name="username"  type="username" class="form-control" id="username" placeholder="Username">
    </div>
  </div>
  <div class="form-group">
    <label for="Password" class="col-lg-1 control-label">Password:</label>
    <div class="col-lg-10">
      <input name="password" type="password" class="form-control" id="password" placeholder="Password">
    </div>
  </div>
  <div class="form-group">
    <div class="col-lg-offset-1 col-lg-10">
      <div class="checkbox">
        <label>
          <input type="checkbox"> Remember me
        </label>
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="col-lg-offset-1 col-lg-10">
      <button type="submit" class="btn btn-default">Login</button>
    </div>
	<br>
	<div class="col-lg-offset-1 col-lg-10">
	<a href="register.php">Register</a>
	</div>
  </div>
</form>
		    <div class="container">

      <hr>

      <footer>
        <div class="row">
          <div class="col-lg-12">
            <p>Copyright © Company 2013</p>
          </div>
        </div>
      </footer>

    </div><!-- /.container -->

    <!-- Bootstrap core JavaScript -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="js/jquery.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/modern-business.js"></script>
  </body>
</html>

Im kinda new to PHP and this is starting to baffle me...:/

 

Any Help would be muchly appreciated, thanks!

Edited by saamde
Link to comment
Share on other sites

  • Solution

You cannot all header() after you have sent any output to the browser.
 
You should rearrange your code so you process the login before your output anything to the browser

<?php
include 'core/init.php';

if (empty($_POST) === false) { 
  $username = $_POST['username']; 
  $password = $_POST['password']; 
  
  if (empty($username) === true || empty($password) === true) {
    $errors[] = '<div class="alert alert-info"> You need to enter a username and password </div>';
  } else if (user_exists($username) === false) {
    $errors[] = '<div class="alert alert-danger">We can\'t find that username. Have you registered?</div>';
  } else if (user_active($username) === false) {
    $errors[] = '<div class="alert alert-warning">You haven\'t activated your account!</div>';
  } else {
    $login = login($username, $password);
    if ($login === false) {
      $errors[] = '<div class="alert alert-info"> That username/password combination is incorrect. </div>';
    } else {
      $_SESSION['user_id'] = $login;
      header('Location: index.php');
      exit();
    }
  } 
}

?>
<!DOCTYPE html>
<html lang="en">
<?php include 'includes/head.php';?>
<body>
<?php include 'includes/header.php'; ?> 
 
<div class="container">
<h2>Login</h2>
<hr>
 
<?php
	print_r($errors);
?>
<form action="login.php" method="post" class="form-horizontal" role="form">
  <div class="form-group">
    <label for="username" class="col-lg-1 control-label">Username:</label>
    <div class="col-lg-10">
      <input name="username"  type="username" class="form-control" id="username" placeholder="Username">
    </div>
  </div>
  <div class="form-group">
    <label for="Password" class="col-lg-1 control-label">Password:</label>
    <div class="col-lg-10">
      <input name="password" type="password" class="form-control" id="password" placeholder="Password">
    </div>
  </div>
  <div class="form-group">
    <div class="col-lg-offset-1 col-lg-10">
      <div class="checkbox">
        <label>
          <input type="checkbox"> Remember me
        </label>
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="col-lg-offset-1 col-lg-10">
      <button type="submit" class="btn btn-default">Login</button>
    </div>
	<br>
	<div class="col-lg-offset-1 col-lg-10">
	<a href="register.php">Register</a>
	</div>
  </div>
</form>
		    <div class="container">
 
      <hr>
 
      <footer>
        <div class="row">
          <div class="col-lg-12">
            <p>Copyright © Company 2013</p>
          </div>
        </div>
      </footer>
 
    </div><!-- /.container -->
 
    <!-- Bootstrap core JavaScript -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="js/jquery.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/modern-business.js"></script>
  </body>
</html>
Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.