0

I know there was a similar question posted before and I already go through it, here's the link Login code doesn't work, stay stuck on log in page but our code is totally different and I already go through it for 3 hours but come with no solution. Here's what my code initially look like before I style it.

<body>
<?php
 require('db.php');
 session_start();
    // If form submitted, insert values into the database.
    if (isset($_POST['username'])){
  
  $username = stripslashes($_REQUEST['username']); // removes backslashes
  $username = mysqli_real_escape_string($con,$username); //escapes special characters in a string
  $password = stripslashes($_REQUEST['password']);
  $password = mysqli_real_escape_string($con,$password);
  
 //Checking is user existing in the database or not
        $query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'";
  $result = mysqli_query($con,$query) or die(mysql_error());
  $rows = mysqli_num_rows($result);
        if($rows==1){
   $_SESSION['username'] = $username;
   header("Location:../user/page/emergency.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>

<br /><br />

</div>
<?php } ?>


</body>

and here is what it look like after I try to style it using css and javascript.

<body>

<h2> Selamat Datang, Sila Log Masuk Untuk Kemaskini</h2>

<?php
 require('db.php');
 session_start();
    // If form submitted, insert values into the database.
    if (isset($_POST['username'])){
  
  $username = stripslashes($_REQUEST['username']); // removes backslashes
  $username = mysqli_real_escape_string($con,$username); //escapes special characters in a string
  $password = stripslashes($_REQUEST['password']);
  $password = mysqli_real_escape_string($con,$password);
  
 //Checking is user existing in the database or not
        $query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'";
  $result = mysqli_query($con,$query) or die(mysql_error());
  $rows = mysqli_num_rows($result);
        if($rows==1){
   $_SESSION['username'] = $username;
   header("Location:../user/page/emergency.php"); // direct user to update page
            }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="container">
 <section id="content">
  <form action="" method="post" name="login">
   <h1>Daftar Masuk</h1>
   
   <div>
   <input type="text" placeholder= "Username" required ="" id ="username"/>
   </div>
   
   <div>
   <input type="password" placeholder= "Password" required ="" id ="password"/>
   </div>
   
   
     <input name="submit" type="submit" value="Login" />
   
   
   </form>
  </section> 
 </div>
<?php } ?>
  <script  src="js/index.js"></script>
 
</body>

and once I style it, the form stay stuck on the login page. Help me, what should I do? Any suggestion and guidance is greatly appreciated.

updated:

okay guys, I found the source of the problem already, I just need to remove on the second form.

WanHazyan
  • 257
  • 1
  • 12
  • 1
    Possible duplicate of [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) –  Dec 04 '17 at 04:39
  • sorry, found the error already. – WanHazyan Dec 04 '17 at 04:45
  • your php is still wrong, you cant call `session_start();` after output –  Dec 04 '17 at 04:50
  • oh, really? where should I put it then? because I'm very new, not familiar with the structure – WanHazyan Dec 04 '17 at 04:58

1 Answers1

0

Turns out, on the form where I do my styling, I just need to remove the tag

so from

<div class="container">
 <section id="content">
  <form action="" method="post" name="login">
   <h1>Daftar Masuk</h1>
   
   <div>
   <input type="text" placeholder= "Username" required ="" id ="username"/>
   </div>
   
   <div>
   <input type="password" placeholder= "Password" required ="" id ="password"/>
   </div>
   
   
     <input name="submit" type="submit" value="Login" />
   
   
   </form>
  </section> 
 </div>

to

<div class="container">
 <section id="content">
  <form action="" method="post" name="login">
   <h1>Daftar Masuk</h1>
   
   
   <input type="text" placeholder= "Username" required ="" id ="username"/>
   
   
   
   <input type="password" placeholder= "Password" required ="" id ="password"/>
  
   
   
     <input name="submit" type="submit" value="Login" />
   
   
   </form>
  </section> 
 </div>
WanHazyan
  • 257
  • 1
  • 12