Created
June 8, 2020 23:02
-
-
Save LucianoCharlesdeSouza/33376a5653c4b2189a8540c9312c381e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Models; | |
class DataBaseModel | |
{ | |
private static $pdo; | |
private static $Host = 'localhost'; | |
private static $User = 'root'; | |
private static $Password = ''; | |
private static $DataBase = '++php'; | |
private static function Connect() | |
{ | |
if (self::$pdo == null) { | |
try { | |
self::$pdo = new \PDO('mysql:host=' . self::$Host . ';dbname=' . self::$DataBase, self::$User, self::$Password, | |
array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); | |
self::$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); | |
} catch (\Exception $e) { | |
echo $e; | |
} | |
} | |
return self::$pdo; | |
} | |
public function getInstance() | |
{ | |
return self::Connect(); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function shortenUrl(){ | |
if(isset($_POST['acao']) && !empty($_POST['url']) ){ | |
$url = $_POST['url']; | |
$filterUrl = strip_tags($url, FILTER_SANITIZE_SPECIAL_CHARS); | |
$cleanUrl = html_entity_decode($filterUrl, FILTER_SANITIZE_STRING); | |
$hash = \mt_rand(10000,99999)-1; | |
$register = new \Models\HomeModel; | |
$ultimoId = $register->registerUrl($cleanUrl,$hash); | |
var_dump($ultimoId); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Models; | |
class HomeModel extends DataBaseModel | |
{ | |
private $pdo; | |
public function __construct() | |
{ | |
$this->pdo = DataBaseModel::getInstance(); | |
} | |
public function registerUrl($cleanUrl, $hash) | |
{ | |
$sql = $this->pdo->prepare("INSERT INTO `short` (cleanurl, hash) VALUES (?,?)"); | |
$sql->execute(array($cleanUrl, $hash)); | |
return $this->pdo->lastInsertId(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment