0

I am learning php and MySQL to do a course project, but I got some problem here.

Here is my code in index.php (referenced from https://htmlcssphptutorial.wordpress.com/2015/07/07/simple-user-registration-login-script-in-php-and-mysql/):

<?php
session_start();
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>

<?php
require('db.php');
// If form submitted, insert values into the database.
if (isset($_POST['username'])){
  $username = $_POST['username'];
  $password = $_POST['password'];
  $username = stripslashes($username);
  $username = mysql_real_escape_string($username);
  $password = stripslashes($password);
  $password = mysql_real_escape_string($password);
  //Checking is user existing in the database or not
  $query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'";
  $result = mysql_query($query) or die(mysql_error());
  $rows = mysql_num_rows($result);
  if($rows==1){
    $_SESSION['username'] = $username;
    header("Location: index.php"); // Redirect user to index.php
  }else{
    echo "<div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
  }
}else{
  ?>
  <div class="form">
  <h1>Log In</h1>
  <form action="" method="post" name="login">
  <input type="text" name="username" placeholder="Username" required />
  <input type="password" name="password" placeholder="Password" required />
  <input name="submit" type="submit" value="Login" />
  </form>
  <p>Not registered yet? <a href='registration.php'>Register Here</a></p>
  </div>
<?php } ?>
</body>
</html>

And it gives me error message:

Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/login.php:11) in /Library/WebServer/Documents/login.php on line 27

I googled it and it might be the html between two phps. however, if I put the html first and than a big chunk of php code, it gives me error

Warning: session_start(): Cannot send session cache limiter – headers already sent (output started at /website/login.php:15) in /website/login.php on line 17

Any idea how to get around with it? Thanks in advance!

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
Vicky
  • 1,465
  • 2
  • 12
  • 21

0 Answers0