saamde Posted October 20, 2013 Share Posted October 20, 2013 (edited) 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 October 20, 2013 by saamde Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted October 20, 2013 Solution Share Posted October 20, 2013 (edited) 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 October 20, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
saamde Posted October 20, 2013 Author Share Posted October 20, 2013 Thanks! for the help! Muchly appreciated! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.