Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include 'resource/session.php';
- include 'resource/database.php';
- include 'resource/utlities.php';
- if (isset($_POST['loginBtn'])){
- $form_errors = array();
- //validate
- $required_fields = array('username', 'password');
- $form_errors = array_merge($form_errors, check_empty_fields($required_fields));
- if (empty($form_errors)){
- //check user exist
- //collect data
- $user = $_POST['username'];
- $password = $_POST['password'];
- $sqlQuery = "SELECT * FROM my_db user2 WHERE username = :username";
- $statement = $db->prepare($sqlQuery);
- $statement->execute(array(':username'=> $user));
- while($row = $statement->fetch()){
- $id = $row['id'];
- $hashed_password = $row['password'];
- $username = $row['username'];
- if (password_verify($password, $hashed_password)){
- $_SESSION['id'] = $id;
- $_SESSION['username'] = $username;
- header('Location: index.php');
- }else{
- $result = "<p style='padding: 20px; color: red; border: 1px solid gray;'>Invalid username or password</p>";
- }
- }
- }else{
- if(count($form_errors) == 1){
- $result = "<p style='color:red;'>There was 1 error in the form</p>";
- }else{
- $result = "<p style='color:red;'>There were ".count($form_errors)." errors in the form</p>";
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head lang = "en">
- <meta charset = "UTF-8">
- <title>Loginpage</title>
- </head>
- <body>
- <h2>User Authentication System</h2><br>
- </body>
- <h3>login Form</h3>
- <?php
- if (isset($result)) echo $result;
- ?>
- <?php
- if (!empty($form_errors))
- echo show_errors($form_errors);
- ?>
- <form method = "post" action = "">
- <table>
- <tr>
- <td>Username: </td>
- <td><input type = "text" value = "" name="username"></td>
- </tr>
- <tr>
- <td>Password: </td>
- <td><input type = "password" value = "" name="password"></td>
- </tr>
- <tr><td></td><td><input style = "float:right;" type = "submit" name="loginBtn" value = "Signin"></td></tr>
- </table>
- </form>
- <p><a href = "index.php">Back</a></p>
- </html>
Add Comment
Please, Sign In to add comment