-2

I have html and php code and database and php code to connect the database. When I make the register page the code working, and data can be saved into the database. but when I made the login page, after I input username and password the page can't change to another page, and the browser don't show any error in code

login.php file

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="css/log.css">
  <style>
    body {
      background: url("img/caption.jpg");
      background-size: cover;
    }
  </style>
  <title>doc</title>
</head>

<body>

  <header>
    <!--set nav or another header here-->
    <h1 id="Great" align="center">Welcome Ambarawa Classsic Train Trip </h1>
  </header>

  <!--set main content between header and footer-->
  <div class="Container">
    <div class="log">
      <div class="title">
        <h3 id="setTitle">Login</h3>
      </div>
      <div class="contenbox">
        <form action="utama.php" method="post">
          <label for="user" style="font-family: serif;">Username:</label> <br> <input type="email" name="username" id="user" placeholder="username"><br>
          <br>
          <label for="passwordu" style="font-family: serif;">Password:</label> <br> <input type="password" name="userpassword" id="passwordu" placeholder="password"><br>
          <!--<input type="submit" value="Login" id="send">-->
          <input type="submit" value="login" name="excecution" id="ss">
        </form>
      </div>
      <a href="forget.html" id="forget">Forgot password</a>
    </div>
    <div class="foot`">
      <p>don't have any account? <a href="regis.php" id="new">sign up here</a></p>
    </div>
  </div>
  
  <script src="tag.js"></script>
</body>

</html>

<?php 
include "db.php";


if($_SERVER['REQUEST_METHOD'] == 'POST'){//requst method
  $id= isset($_POST['username']) ? $_POST['username'] :'';
  $pas=isset($_POST['userpassword']) ? $_POST['userpassword'] :'';

  
  if(isset($_POST['execution'])==true){
    $capture= "SELECT * FROM penguna WHERE Email='$id' AND pass='$pas'";
    $result = mysqli_query($con,$capture);
    
    if($result>0){
      $kolom=mysqli_fetch_assoc($result);
      $_SESSION['masuk'] = true;
      header("Location:utama.php");
    }
    
  }
}



    






?>

and this's terget code name is utama.php

<?php 
session_start();

if (isset($_SESSION['masuk']) && $_SESSION['masuk'] ==true){
  echo"wellcomme";
}
else{
 header("Location:login.php");
 
}

?>

<!doctype html>
<html lang="en">

<head>
  <title>Title</title>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS v5.2.1 -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">

</head>

<body>
  <button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#slide" aria-controls="Id1">Enable both scrolling & backdrop</button>
  
  <div class="offcanvas offcanvas-start bg-dark text-light" data-bs-scroll="true" tabindex="-1" id="slide" aria-labelledby="Enable both scrolling & backdrop">
    <div class="offcanvas-header">
      <h5 class="offcanvas-title" id="Enable both scrolling & backdrop">welcome</h5>
      <button type="button" class="btn btn-close bg-danger" data-bs-dismiss="offcanvas" aria-label="Close"></button>
    </div>
    <div class="offcanvas-body">
      <p>Try scrolling the rest of the page to see this option in action.</p>
    </div>
  </div>
  <br>

  <div class="catch">
    <form action="" method="get">
      <input type="text" placeholder="Judul">
      <textarea name="" id="" cols="30" rows="10" placeholder="Diskusi"></textarea>
      <input type="submit" value="+">
    </form>
  </div>
  




  <!-- Bootstrap JavaScript Libraries -->
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"
    integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3" crossorigin="anonymous">
  </script>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.min.js"
    integrity="sha384-7VPbUDkoPSGFnVtYi0QogXtr74QeVeeIs99Qfg5YCF+TidwNdjvaKZX19NZ/e6oz" crossorigin="anonymous">
  </script>
</body>

</html>
  • 1
    Make sure that you have no data output before you trigger `header location` redirection – Ken Lee Aug 08 '23 at 06:17
  • Hm, `utama.php` doesn't do much logicwise, wouldn't you want to check username and password somewhere? – brombeer Aug 08 '23 at 06:18
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Aug 08 '23 at 11:31
  • **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Aug 08 '23 at 11:31

1 Answers1

-2

You need to change file name for action attribute.It should same page(login.php) for login page.Because you are checking username and password in same page.Here is updated code..

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="css/log.css">
  <style>
    body {
      background: url("img/caption.jpg");
      background-size: cover;
    }
  </style>
  <title>doc</title>
</head>

<body>

  <header>
    <!--set nav or another header here-->
    <h1 id="Great" align="center">Welcome Ambarawa Classsic Train Trip </h1>
  </header>

  <!--set main content between header and footer-->
  <div class="Container">
    <div class="log">
      <div class="title">
        <h3 id="setTitle">Login</h3>
      </div>
      <div class="contenbox">
        <form action="<?php echo htmlspecialchars($_SERVER[‘PHP_SELF’]); ?>" method="post">
          <label for="user" style="font-family: serif;">Username:</label> <br> <input type="email" name="username" id="user" placeholder="username"><br>
          <br>
          <label for="passwordu" style="font-family: serif;">Password:</label> <br> <input type="password" name="userpassword" id="passwordu" placeholder="password"><br>
          <!--<input type="submit" value="Login" id="send">-->
          <input type="submit" value="login" name="excecution" id="ss">
        </form>
      </div>
      <a href="forget.html" id="forget">Forgot password</a>
    </div>
    <div class="foot`">
      <p>don't have any account? <a href="regis.php" id="new">sign up here</a></p>
    </div>
  </div>
  
  <script src="tag.js"></script>
</body>

</html>

<?php 
include "db.php";


if($_SERVER['REQUEST_METHOD'] == 'POST'){//requst method
  $id= isset($_POST['username']) ? $_POST['username'] :'';
  $pas=isset($_POST['userpassword']) ? $_POST['userpassword'] :'';

  
  if(isset($_POST['execution'])==true){
    $capture= "SELECT * FROM penguna WHERE Email='$id' AND pass='$pas'";
    $result = mysqli_query($con,$capture);
    
    if($result>0){
      $kolom=mysqli_fetch_assoc($result);
      $_SESSION['masuk'] = true;
      header("Location:utama.php");
    }
    
  }
}

?>

   
Tejas Prajapati
  • 115
  • 1
  • 6