Создать простой сайт, где будет страница с формой для авторизации и страница для авторизованного пользователя.
<?php
class Database
{
private $hostname;
private $username;
private $password;
private $dbName;
public function __construct()
{
$config = new Configuration();
$this->hostname = $config->getDbHostname();
$this->username = $config->getDbUsername();
$this->password = $config->getDbPassword();
$this->dbName = $config->getDbName();
}
public function getServerConnection()
{
try
{
$db_connect = new mysqli($this->hostname, $this->username, $this->password);
if ($db_connect->connect_error)
{
die("Database connection failed: ".$db_connect->connect_error);
}
return $db_connect;
}
catch (Exception $e)
{
echo "Caught exception: ", $e->getMessage(), "\r\n";
}
return false;
}
public function getConn()
{
try
{
$db_connect = new mysqli($this->hostname, $this->username, $this->password, $this->dbName);
if ($db_connect->connect_error)
{
return false;
//die("Database connection failed: ".$db_connect->connect_error);
}
mysqli_select_db($db_connect, $this->dbName);
return $db_connect;
}
catch (Exception $e)
{
echo "Caught exception: ", $e->getMessage(), "\r\n";
throw new Exception("Database not exists.");
}
return false;
}
// public function getConn()
// {
// try
// {
// $db_connect = new mysqli($this->hostname, $this->username, $this->password, $this->db);
// if ($db_connect->connect_error)
// {
// die("Database connection failed: " . $db_connect->connect_error);
// }
// }
// catch (Exception $e)
// {
// echo "Caught exception: ", $e->getMessage(), "\r\n";
// }
//
// return $db_connect;
// }
public function createDatabase($sql)
{
return mysqli_query($this->getServerConnection(), $sql);
}
public function query($sql)
{
return mysqli_query($this->getConn(), $sql);
}
public function executeSql($sql)
{
try
{
mysqli_query($this->getConn(), $sql);
}
catch (Exception $e)
{
echo "Caught exception: ", $e->getMessage(), "\r\n";
}
}
public function sanitize($var)
{
$var = str_replace("<", "<", $var);
$var = str_replace(">", ">", $var);
return mysqli_real_escape_string($this->getConn(), $var);
}
}