/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // start session // user already authenticated } if ($_POST) { require_once('codes/dal.php'); $dal = new DataAccessLayer(); // join the 'users' and 'roles' tables $sql = 'select ' . 'u.id ' . ',u.username ' . ',u.password ' . ',u.role_id ' . ',r.name ' . ',u.full_name ' . ',u.email ' . ',u.description ' . 'from users as u ' . 'join roles as r on u.role_id = r.id ' . 'where u.username = \'' . $user . '\''; $result = $dal->query($sql); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); if ($pass == $row['password']) { // create session variables $_SESSION['user_id'] = $row['id']; $_SESSION['username'] = $row['username']; $_SESSION['full_name'] = $row['full_name']; $_SESSION['role_id'] = $row['role_id']; $_SESSION['role_name'] = $row['name']; $_SESSION['email'] = $row['email']; $_SESSION['password'] = $row['password']; // check if password is default $_SESSION['default'] = true; // login successful - but password needs to be changed } else { // login successful - redirect to home page } } else { $err = '<tr><td colspan="2">' . '<div class="error-message">The username and/or password you entered is invalid.</div>' . '</td></tr>'; } } else { $err = '<tr><td colspan="2">' . '<div class="error-message">The username and/or password you entered is invalid.</div>' . '</td></tr>'; } } } ?>