Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // start session
- session_start();
- // connect to database
- include 'config/db_connect.php';
- // set page title
- $page_title="Welcome";
- // page header html
- include 'layout_head.php';
- include 'layout_foot.php';
- //home.php
- include 'database.php';
- $pdo = Database::connect();
- //If the POST var "login" exists (our submit button), then we can
- //assume that the user has submitted the login form.
- if(isset($_POST['login'])){
- //Retrieve the field values from our login form.
- $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
- $passwordAttempt = !empty($_POST['password']) ? trim($_POST['password']) : null;
- //Retrieve the user account information for the given username.
- $sql = "SELECT id, username, password FROM users WHERE username = :username";
- $stmt = $pdo->prepare($sql);
- //Bind value.
- $stmt->bindValue(':username', $username);
- //Execute.
- $stmt->execute();
- //Fetch row.
- $user = $stmt->fetch(PDO::FETCH_ASSOC);
- //If $row is FALSE.
- if($user === false){
- //Could not find a user with that username!
- //PS: You might want to handle this error in a more user-friendly manner!
- die('Incorrect username / password combination!');
- } else{
- //User account found. Check to see if the given password matches the
- //password hash that we stored in our users table.
- //Compare the passwords.
- $validPassword = password_verify($passwordAttempt, $user['password']);
- //If $validPassword is TRUE, the login has been successful.
- if($validPassword){
- //Provide the user with a login session.
- $_SESSION['user_id'] = $user['id'];
- $_SESSION['logged_in'] = time();
- //Redirect to our protected page, which we called home.php
- header('Location: products.php');
- exit;
- } else{
- //$validPassword was FALSE. Passwords do not match.
- die('Incorrect username / password combination!');
- }
- }
- }
- Database::disconnect();
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Login</title>
- </head>
- <body>
- <h3 align="center">Thank you for visiting the ScotBooks ecommerce website.</h3>
- <form action="login.php" method="post">
- <table border="0" cellpadding="10" cellspacing="1" width="500" align="center">
- <tr class="tableheader">
- <td align="center" colspan="2">Please enter your login details</td>
- </tr>
- <tr class="tablerow">
- <td align="center"><label for="username">Username</label>
- <input type="text" id="username" name="username"><br></td></tr>
- <tr class="tablerow">
- <td align="center"><label for="password">Password</label>
- <input type="password" id="password" name="password"><br></td></tr>
- <tr class="tablerow">
- <td align="center"><input type="submit" name="login" value="Login"></td>
- </tr>
- </table>
- <br></br>
- <p align="center">If you do not have an active account, please register <a href="register.php">Here</a></p>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement