0
0

Refactored database, refactored config file, corrected login redirection

Moved database function in class "Database";
Corrected database-using webpages accordingly;
Moved config file from PHP to JSON with more config variables;
Corrected login page redirection on wrong username or password.
This commit is contained in:
Bryan Pedini
2019-03-21 12:07:23 +01:00
parent 4eaf1d0829
commit 4a6630bacb
10 changed files with 143 additions and 104 deletions

View File

@@ -1,29 +1,5 @@
<?php
require_once ( 'config.php' );
function db_connect ( ) {
global $config;
$connection = new MySQLi ( $config [ 'db' ] [ 'host' ], $config [ 'db' ] [ 'username' ], $config [ 'db' ] [ 'password' ], $config [ 'db' ] [ 'name' ], $config [ 'db' ] [ 'port' ] );
if ( $connection->connect_errno ) {
die ( "Database connection failed." );
}
return $connection;
}
function db_prepare ( MySQLi $connection, string $query ) {
if ( ! ( $statement = $connection -> prepare ( $query ) ) ) {
die ( "Prepare failed: (" . $connection->errno . ") " . $connection->error );
}
return $statement;
}
function db_bind ( MySQLi_stmt $statement, array $params ) {
if ( ! call_user_func_array ( array ( $statement, "bind_param" ), array_merge ( $params [ 0 ], $params [ 1 ] ) ) ) {
die ( "Binding parameters failed: (" . $statement->errno . ") " . $statement->error );
}
}
function db_execute ( $statement ) {
if ( ! $statement->execute ( ) ) {
die ( "Execute failed: (" . $statement->errno . ") " . $statement->error );
}
}
require_once ( 'lib/php/classes/Database.php' );
function split_uri_array ( string $php_self, string $request_uri ) : Array {
$uri = substr ( $request_uri, strlen ( substr ( $php_self, 0, strrpos ( $php_self, 'index.php' ) ) ), strlen ( $request_uri ) - strlen ( substr ( $php_self, 0, strrpos ( $php_self, 'index.php' ) ) ) );
$arr = Array ( );
@@ -37,4 +13,10 @@
$arr [ ] = substr ( $uri, $last_arg_pos, $i - $last_arg_pos + 1);
return $arr;
}
function load_config ( ) : array {
$config = file_get_contents ( 'config.json' );
$config = json_decode ( $config, true );
$config [ 'installation_path' ] = $_SERVER [ 'DOCUMENT_ROOT' ] . $config [ 'installation_path' ];
return $config;
}
?>