Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- if(isset($_COOKIE['username'])) {
- header('Location: account.php');
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Sample</title>
- </head>
- <body>
- <?php if(isset($_SESSION['login_error'])): ?>
- <div class="error_message">Incorrect username and password combination</div>
- <?php endif; ?>
- <form action="login.php" method="post">
- <input type="text" name="username" placeholder="Username" autocomplete="off"><br>
- <input type="password" name="password" placeholder="Password"><br>
- <input type="submit" value="Log in">
- </form>
- <?php
- session_start();
- $db = new PDO('mysql:host=127.0.0.1;dbname=sample;charset=utf8', 'root', '');
- if(isset($_POST['username'], $_POST['password'])) {
- if(!empty($_POST['username']) && !empty($_POST['password'])) {
- $query = $db->prepare("SELECT username, lastname FROM users WHERE username=:username AND password=:password");
- $query->execute([
- 'username' => $_POST['username'],
- 'password' => $_POST['password']
- ]);
- $row = $query->fetchAll(PDO::FETCH_OBJ);
- if(count($row)) {
- $_SESSION['username'] = $_POST['username'];
- setcookie('username', $_POST['username'], time()+3600);
- header('Location: account.php');
- } else {
- $_SESSION['login_error'] = 'Incorrect username and password combination';
- header('Location: index.php');
- }
- } else {
- header('Location: index.php');
- }
- }
- <?php
- session_start();
- if(!isset($_COOKIE['username'])) {
- header('Location: index.php');
- }
- $db = new PDO('mysql:host=127.0.0.1;dbname=sample;charset=utf8', 'root', '');
- $query = $db->prepare("SELECT firstname, lastname FROM users WHERE username=:username");
- $query->execute(['username' => $_SESSION['username']]);
- $row = $query->fetchAll(PDO::FETCH_OBJ);
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Account</title>
- </head>
- <body>
- <?php if(isset($_SESSION['username'])): ?>
- <div>Hello, <?php echo $row->firstname; ?></div>
- <?php endif; ?>
- <a href="logout.php">Logout</a>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement