Log System

Part 1: index.php

  1. <html>

  2. <head>

  3. <title>PHP login system</title>

  4. // insert style.css file inside index.html

  5. <link rel = "stylesheet" type = "text/css" href = "style.css">

  6. </head>

  7. <body>

  8. <div id = "frm">

  9. <h1>Login</h1>

  10. <form name="f1" action = "authentication.php" onsubmit = "return validation()" method = "POST">

  11. <p>

  12. <label> UserName: </label>

  13. <input type = "text" id ="user" name = "user" />

  14. </p>

  15. <p>

  16. <label> Password: </label>

  17. <input type = "password" id ="pass" name = "pass" />

  18. </p>

  19. <p>

  20. <input type = "submit" id = "btn" value = "Login" />

  21. </p>

  22. </form>

  23. </div>

  24. // validation for empty field

  25. <script>

  26. function validation()

  27. {

  28. var id=document.f1.user.value;

  29. var ps=document.f1.pass.value;

  30. if(id.length=="" && ps.length=="") {

  31. alert("User Name and Password fields are empty");

  32. return false;

  33. }

  34. else

  35. {

  36. if(id.length=="") {

  37. alert("User Name is empty");

  38. return false;

  39. }

  40. if (ps.length=="") {

  41. alert("Password field is empty");

  42. return false;

  43. }

  44. }

  45. }

  46. </script>

  47. </body>

  48. </html>


Part 2: style.css

  1. body{

  2. background: #eee;

  3. }

  4. #frm{

  5. border: solid gray 1px;

  6. width:25%;

  7. border-radius: 2px;

  8. margin: 120px auto;

  9. background: white;

  10. padding: 50px;

  11. }

  12. #btn{

  13. color: #fff;

  14. background: #337ab7;

  15. padding: 7px;

  16. margin-left: 70%;

  17. }


Part III: authentication.php

  1. <?php

  2. include('connection.php');

  3. $username = $_POST['user'];

  4. $password = $_POST['pass'];

  5. //to prevent from mysqli injection

  6. $username = stripcslashes($username);

  7. $password = stripcslashes($password);

  8. $username = mysqli_real_escape_string($con, $username);

  9. $password = mysqli_real_escape_string($con, $password);

  10. $sql = "select *from login where username = '$username' and password = '$password'";

  11. $result = mysqli_query($con, $sql);

  12. $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

  13. $count = mysqli_num_rows($result);

  14. if($count == 1){

  15. echo "<h1><center> Login successful </center></h1>";

  16. }

  17. else{

  18. echo "<h1> Login failed. Invalid username or password.</h1>";

  19. }

  20. ?>