43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|
class Token {
|
|
private $conn;
|
|
private $table_name = "token";
|
|
|
|
public $userId;
|
|
public $user;
|
|
public $token;
|
|
public $created;
|
|
public $expires;
|
|
|
|
// constructor with $db as database connection
|
|
public function __construct($db){
|
|
$this->conn = $db;
|
|
}
|
|
|
|
function create(){
|
|
$params = "user_id=:userId, token=:token, created=:created, expires=:expires";
|
|
$query = "INSERT INTO " . $this->table_name . "
|
|
SET " . $params;
|
|
|
|
// prepare query
|
|
$stmt = $this->conn->prepare($query);
|
|
|
|
$this->created=(new \DateTime())->format('Y-m-d H:i:s');
|
|
$this->expires=(new \DateTime())->add(new DateInterval('P1Y3M'))->format('Y-m-d H:i:s');
|
|
$this->token=md5(sprintf('%d-%s', $this->userId, $this->expires));
|
|
|
|
// bind values
|
|
$stmt->bindParam(":userId", $this->userId);
|
|
$stmt->bindParam(":token", $this->token);
|
|
$stmt->bindParam(":created", $this->created);
|
|
$stmt->bindParam(":expires", $this->expires);
|
|
|
|
// execute query
|
|
if($stmt->execute()){
|
|
return true;
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
} |