0
0

Prepared website with even cleaner file structure and layout

This commit is contained in:
Bryan
2019-05-29 19:33:35 +02:00
parent c399808706
commit 6294ae5d50
4 changed files with 2 additions and 2 deletions

View File

@@ -0,0 +1,39 @@
<?php
if ( ! isset ( $_SESSION [ 'user_id' ] ) ) {
header ( 'Content-Type: application/json' );
http_response_code ( 401 );
$response = [
'status' => 401,
'error_message' => 'You either are not logged in or you do not have permissions to insert new URLs.'
];
echo ( json_encode ( $response ) );
exit;
}
if ( ! isset ( $_POST [ 'url' ] ) ) {
header ( 'Content-Type: application/json' );
http_response_code ( 400 );
$response = [
'status' => 400,
'error_message' => 'You either did not provide a URL or you provided an invalid one.'
];
echo ( json_encode ( $response ) );
exit;
}
$database = new Database ( $GLOBALS [ 'config' ] [ 'db' ] );
$database->connect ( );
$url = $_POST [ 'url' ];
$database->prepare ( "INSERT INTO `links` ( `URL`, `created_by` ) VALUES ( :url, " . $_SESSION [ 'user_id' ] . " );" );
$database->bind ( [ ':url' => $url ] );
$database->execute ( );
$database->prepare ( "SELECT MAX( links.ID ) as `last` FROM links;");
$database->execute ( );
$result = $database->get_result ( );
$row = $result->fetchAll ( ) [ 0 ];
header ( 'Content-Type: application/json' );
$response = [
'status' => 200,
'message' => 'URL inserted correctly.',
'last_insert' => $row [ 'last' ],
];
echo ( json_encode ( $response ) );
exit;

View File

@@ -0,0 +1,3 @@
<?php
$_SESSION [ 'user_id' ] = NULL;
header ( "Location: " . substr ( $_SERVER [ 'SCRIPT_NAME' ], 0, -10 ) . "/" );