luhan-s of Inifnity
12/18/2017 - 8:53 PM

User

User object oriented

<?php

  include('connection.php');

  require_once("class/user.php");

  $user = new User();

  $user->name = $_POST['name'];
  $user->last_name = $_POST['last_name'];
  $user->company = $_POST['company'];
  $user->email = $_POST['email'];

  $password = $_POST['password'];
  $password_conf = $_POST['password_conf'];
  $user->password_conf($password,$password_conf);

  $user->register($user,$connection);

?>
<?php

  include('connection.php');

  require_once("class/user.php");

  $user = new User();

  $user->name = $_POST['name'];
  $user->last_name = $_POST['last_name'];
  $user->company = $_POST['company'];
  $user->email = $_POST['email'];

  $password = $_POST['password'];
  $password_conf = $_POST['password_conf'];
  $user->password_conf($password,$password_conf);

  $user->register($user,$connection);

?>
<?php

  require_once("class/user.php");
  include("connection.php");

  $user = new User();

  $user->email = $_POST["email"];
  $user->password = $_POST["password"];
  $user->login($user,$connection);

  echo $_SESSION['logged_in'];


?>
<?php

session_start();

  Class User {

    public $id;
    public $name;
    public $last_name;
    public $email;
    public $password;
    public $hash;
    public $bday_date;
    public $company;
    public $permissions;
    public $last_login;
    public $regis_date;

    private function randomString() {
        $length = 16;
        $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $str = "";

        for ($i = 0; $i < $length; $i++) {
            $str .= $chars[mt_rand(0, strlen($chars) - 1)];
        }

        return $str;
    }


    public function password_conf($password,$password_conf){
        if($password === $password_conf){
            $this->password = $password;
            return true;
        } else{
            header("Location: /infinityy/register.php?register=password");
        }
    }

    public function register($user,$conection){
        $query = "select email from users where email='{$this->email}'";
        $verifica = mysqli_query($conection,$query);
        $num_rows = mysqli_num_rows($verifica);
        if($num_rows > 0){
            header("Location: /infinityy/register.php?register=email");
        }else {
            $this->id = $this->randomString();
            $this->hash = password_hash($senha, PASSWORD_BCRYPT);
            $this->regis_date = date('Y-m-d H:i');
            $query2 = "insert into users(id,name,last_name,email,hash,company,regis_date) values('{$this->id}','{$this->name}','{$this->last_name}','{$this->email}','{$this->hash}','{$this->company}','{$this->regis_date}')";
            $resultado = mysqli_query($conection,$query2);
            if($resultado == true){
              header("Location: /infinityy/login.php?register=true");
            }else {
              return $resultado;
            }

        }

    }

    public function login($user,$conection){
        $query = "select * from users where email='{$this->email}'";
        $verifica = mysqli_query($conection,$query);
        $num_rows = mysqli_num_rows($verifica);
        if($num_rows > 0){
          $select = mysqli_fetch_assoc($verifica);
          if(password_verify($this->password, $select['hash'])){
              $this->logUser($this->email);
              header("Location: /infinityy/index.php?login=true");
          } else {
              header("Location: /infinityy/login.php?login=account");
          }
        } else{
          header("Location: /infinityy/login.php?login=account");
        }
    }

    private function logUser($email){
      $_SESSION['email'] = $email;
      $_SESSION['logged_in'] = true;
      $_SESSION['timestamp'] = time();
    }

    public function logout(){
      session_start();
      session_destroy();
      header('Location: /infinityy/login.php?logout=true');
    }

    public function searchUser($email,$connection){
      $query = "select * from users where email='{$email}'";
      $result = mysqli_query($connection,$query);
      $userdb = mysqli_fetch_assoc($result);

      $user = new User();

      $user->name = $userdb['name'];
      $user->last_name = $userdb['last_name'];

      return $user;
    }

    function checkUser() {
      if(time() - $_SESSION['timestamp'] > 600) { // Subtrai timestamp atual com o armazenado em SESSION['timestamp']
          unset($_SESSION['timestamp']);
          $_SESSION['logged_in'] = false;
          header("Location: /infinityy/lock.php"); // Redireciona para a pagina login.php
          exit;
      } else {
          $_SESSION['timestamp'] = time(); // Atualiza timestamp
      }
    }




  }

?>