Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbsrvprg/exercises/projekt/classes/

card.php
collection.php
filtersort.php
lineup.php
user.php

user.php

111 lines UTF-8 Windows (CRLF)
<?php
include("./incl/default.php");
class 
User
{
    private 
$db;

    public function 
__construct($dbconn)
    {
        
$this->db $dbconn;
        
//gör så att man inte behöver ha db hela tiden, man skapar en user med db
    
}
    public function 
login($email$password)
    {
        try {
            
$sql "SELECT email, password FROM projektusers";
            
$stmt $this->db->prepare($sql);
            
$stmt->execute();
            while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
                if (
$email === $res['email'] && $password === $res['password']) {
                    
$this->autologin($email$password);
                } else
                    echo 
"Fel uppgifter!<br>";
            }
        } catch (
PDOException $e) {
            echo 
$sql "<br />" $e->getMessage();
        }
    }

    public function 
register($name$email$password)
    {
        
$name htmlspecialchars($_POST["name"], ENT_QUOTES'UTF-8');
        
$_SESSION["register-date"] = time();
        
$_SESSION["register-name"] = $name;
        
$_SESSION["register-email"] = $email;
        
$_SESSION["register-password"] = $password;
        
$meddelande "Ditt namn är: " $name "\nDitt epostadress är: " $email "\nKlicka på länken nedan för att verifera detta \nhttps://labb.vgy.se/~danieleh/webbsrvprg/exercises/projekt/verify.php?email=" $email;
        
//länk med get (epost i länken som då verifierar att du vill skapa nytt)
        
$mottagare $email;
        
$rubrik "Verifiera ditt konto";
        
$mejlhuvud "From: " $email;
        
mail($mottagare$rubrik$meddelande$mejlhuvud);
        echo 
"<p>Veriferingslänk skickad till " $email "</p>";
        echo 
"<p>================================</p>";
    }

    public function 
logout()
    {
        unset(
$_SESSION["logIn"]);
        unset(
$_SESSION["email"]);
        unset(
$_SESSION["userid"]);
        
header("Location: index.php");
    }
    public function 
forgot($email$password)
    {
        
$email htmlspecialchars($_POST["email"], ENT_QUOTES'UTF-8');
        
$password htmlspecialchars($_POST["password"], ENT_QUOTES'UTF-8');
        if (isset(
$_POST["forgot"])) {
            
$_SESSION["forgot-date"] = time();
            
$_SESSION["forgot-email"] = $email;
            
$_SESSION["forgot-password"] = crypt($password'$1$somethin$');
            
$meddelande "För att skapa ett nytt lösenord till kontot med epostadressen: \n" $email "\nKlicka på länken nedan för att välja ett nytt lösenord detta \nhttps://labb.vgy.se/~danieleh/webbsrvprg/exercises/projekt/newpass.php?email=" $email;
            
$mottagare $email;
            
$rubrik "Verifiera ditt konto";
            
$mejlhuvud "From: " $email;
            
mail($mottagare$rubrik$meddelande$mejlhuvud);
            echo 
"<p>Veriferingslänk skickad till " $email "</p>";
            echo 
"<p>================================</p>";
        }
    }
    private function 
autologin($email$password){
        
$sql "SELECT id FROM projektusers
        WHERE email = ? AND password = ?"
;
        
$stmt $this->db->prepare($sql);
        
$data = array($email$password);
        
$stmt->execute($data);
        while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
            
$_SESSION["logIn"] = true;
            
$_SESSION["userid"] = $res["id"];
            
header("Location: index.php");
        }
    }
    public function 
newpass($email$password)
    {
        try {
            
$email htmlspecialchars($_SESSION["forgot-email"], ENT_QUOTES'UTF-8');
            
$password htmlspecialchars($_SESSION["forgot-password"], ENT_QUOTES'UTF-8');
            
$sql "UPDATE projektusers SET password = ?
        WHERE email = ?"
;
            
$stmt $this->db->prepare($sql);
            
$data = array($password$email);
            
$stmt->execute($data);
            
$this->autologin($email$password);
        } catch (
PDOException $e) {
            echo 
$sql "<br />" $e->getMessage();
        }
    }
    public function 
verify($name$email$password)
    {
        try {
            
$sql "INSERT INTO projektusers (name, email, password) 
            VALUES (?, ?, ?)"
;
            
$stmt $this->db->prepare($sql);
            
$data = array($name$email$password);
            
$stmt->execute($data);
            
$this->autologin($email$password);
        } catch (
PDOException $e) {
            echo 
$sql "<br />" $e->getMessage();
        }
    }
}