<?php $conn = new PDO('mysql:host=localhost;dbname=db_pdologin', 'root', ''); if(!$conn){ } ?>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a> </div> </nav> <div class="col-md-3"></div> <div class="col-md-6 well"> <h3 class="text-primary">PHP - Simple Login Using PDO</h3> <hr style="border-top:1px dotted #ccc;"/> <div class="col-md-3"></div> <div class="col-md-6"> <form method="post" action="login.php"> <div class="form-group"> <label>Username</label> <input type="text" name="username" class="form-control"/> </div> <div class="form-group"> <label>Password</label> <input type="password" name="password" class="form-control"/> </div> <center><button class="btn btn-primary" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button></center> </form> </div> </div> </body> </html>
<?php } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a> </div> </nav> <div class="col-md-3"></div> <div class="col-md-6 well"> <h3 class="text-primary">PHP - Simple Login Using PDO</h3> <hr style="border-top:1px dotted #ccc;"/> <div class="col-md-3"></div> <div class="col-md-6"> <center><h2>This is Home Page</h2></center> <center><a href="logout.php">Logout</a></center> </div> </div> </body> </html>
<?php ?>
<?php require_once 'conn.php'; if($_POST['username'] != "" || $_POST['password'] != ""){ $username = $_POST['username']; $password = $_POST['password']; $sql = "SELECT * FROM `user` WHERE `username`=? AND `password`=? "; $query = $conn->prepare($sql); $row = $query->rowCount(); $fetch = $query->fetch(); if($row > 0) { $_SESSION['user'] = $fetch['user_id']; } else{ echo " <script>alert('Invalid username or password')</script> <script>window.location = 'index.php'</script> "; } }else{ echo " <script>alert('Please complete the required field!')</script> <script>window.location = 'index.php'</script> "; } } ?>