Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host = "localhost";
- $dbname = "tennis";
- $username = "root";
- $password = "";
- //Start the Session
- session_start();
- $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
- //3. If the form is submitted or not.
- //3.1 If the form is submitted
- if (isset($_POST['email']) and isset($_POST['password'])) {
- // later add more validation - filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)
- $errors = [];
- if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
- $errors['email'] = 'Email ni pravilen.';
- }
- if (empty($_POST['email']) || empty($_POST['password'])) {
- $errors['empty'] = 'Email in geslo sta obvezna.';
- }
- //3.1.1 Assigning posted values to variables.
- $email = $_POST['email'];
- $password = $_POST['password'];
- //3.1.2 Checking the values are existing in the database or not
- if (count($errors) > 0) {
- $errors = ['email' => $email, 'password' => $password];
- require 'login.phtml';
- die;
- }
- $result = $pdo->query("SELECT * FROM users WHERE email = :email and password = :password")->fetch(PDO::FETCH_ASSOC);
- if ($result == 1) {
- $_SESSION['username'] = $username;
- } else {
- $fmsg = "Invalid Login Credentials.";
- }
- }
- if (empty($result)) {
- $errors['result'] = 'Uporabnik s tem emailom in geslom v bazi ne obstaja.';
- die;
- }
- // poglej, da result ni prazen (da si iz baze dobila uporabnika)
- // poglej kako sva errorje prikazovala in dodajala
- // shrani ime uporabnika v session
- //3.1.2 If the posted values are equal to the database values, then session will be created for the user.
- //3.1.3 If the login credentials doesn't match, he will be shown with an error message.
- //3.1.4 if the user is logged in Greets the user with message
- if (isset($_SESSION['username'])) {
- $username = $_SESSION['username'];
- echo "Živjo, " . $username;
- echo "This is the Members Area";
- echo "<a href='logout.php'>Logout</a>";
- } else {
- //3.2 When the user visits the page first time, simple login form will be displayed.
- require 'login.phtml';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement