1+ class UserInterface{
2+ private $host;
3+ private $user;
4+ private $pass;
5+ private $db;
6+ private $mysqli;
7+
8+
9+ public function __construct(){
10+ $this->host='localhost';
11+ $this->user='root';
12+ $this->pass-'';
13+ $this->db='accounts';
14+ $this->mysqli=new mysqli($this->host,$this->user,$this->pass,$this->db) or die($this->mysqli->error);
15+ }
16+
17+ public function login(){
18+ $email=$this->mysqli->escape_sstring($_POST['email']);
19+ $result=$this->mysqli->query("SELECT * FROM users WHERE email='email'");
20+ if($result->num_rows == 0)(
21+ $_SESSION['message'] = "User with that email doesnt exist!";
22+ header("location: error:php");
23+ )
24+ else{
25+ $user = $result->fetch_assoc();
26+ if(password_verify($_POST['password'],$user['password'])){
27+ $_SESSION['email']=$user['email'];
28+ $_SESSION['first_name']=$user['first_name'];
29+ $_SESSION['last_name']=$user['last_name'];
30+ $_SESSION['active']=$user['active'];
31+ $_SESSION['logged_in']= true;
32+ header("location: landingpage.php");
33+ }
34+ else{
35+ $_SESSION['message']="You have entered wrong password, try again!"
36+ header("location: error.php");
37+ }
38+ }
39+
40+ }
41+ public function register(){
42+ $_SESSION['email']=$_POST['email'];
43+ $_SESSION['first_name']=$_POST['firstname'];
44+ $_SESSION['last_name']=$_POST['lastname'];
45+
46+ $fistname= $this->mysqli->escape_string($_POST['fisrtname']);
47+ $lastname= $this->mysqli->escape_string($_POST['lastname']);
48+ $email= $this->mysqli->escape_string($_POST['email']);
49+ $password= $this->mysqli->escape_string($_POST['password'],PASSWORD_BCRYPT);
50+ $hash= $this->mysqli->escape_string(md5(rand(0,1000)));
51+
52+ $result = $this->mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());
53+ if($result->num_rows >0){
54+ $_SESSION['message']="Korisnik vec postoji!"
55+ header("location: error.php");
56+ }
57+ else{
58+ $sql="INSERT INTO users(first_name,last_name, email, password, hash)"."VALUES('$first_name','$last_name','$email','$password','$hash')";
59+ if($this->mysqli->query($sql)){
60+ $_SESSION['active']= 1;
61+ $_SESSION['logged_in']=true;
62+ $_SESSION['message']='You have successfully registerd!';
63+
64+ header("location:success.php");
65+ }
66+ else{
67+ $_SESSION['message']='Registration failed';
68+ header("location: error.php");
69+ }
70+ }
71+
72+ }
73+ }
0 commit comments