added logging out

This commit is contained in:
Zdenek Borovec 2024-05-05 21:51:21 +02:00
parent ca7ee9fba2
commit 1d28eda86a
3 changed files with 55 additions and 4 deletions

View file

@ -116,6 +116,10 @@ ul {
display: block;
}
#header table {
width: 100%;
}
#footer {
border-top: double;
padding: 1em;
@ -123,7 +127,6 @@ ul {
.noborder-table td, .noborder-table th {
border: none;
text-align: left;
}
.image-container-right {

View file

@ -40,6 +40,45 @@ function attempt_sql_connect() {
}
}
/**
* If user logged out, destroy the session data and delete cookie.
*/
if (isset($_POST["logout"])) {
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Redirect to this page with GET
header(sprintf("Location: %s?%s", sanitize_input($_SERVER["PHP_SELF"]),
sanitize_input($_POST["get_params"])));
}
/**
* Construct the available account actions for the header depending on whether
* a user is logged in or not and return it as string.
*/
function construct_account_section(){
if(isset($_SESSION["current_user"])) {
return sprintf("Logged in as %s <br>
<form method=\"post\" action=%s>
<input name=\"get_params\" type=\"hidden\" value=\"%s\">
<input name=\"logout\" type=\"submit\" value=\"logout\">
</form>
<a href=\"http://www.zdenekborovec-dev.cz/deleteaccount/\">
Delete account</a>", $_SESSION["current_user"]->user_name,
sanitize_input($_SERVER["PHP_SELF"]),
sanitize_input($_SERVER["QUERY_STRING"]));
}
else {
return sprintf("<a href=\"http://www.zdenekborovec-dev.cz/login\">Login</a>
<br><a href=\"http://www.zdenekborovec-dev.cz/register\">Register</a>");
}
}
/**
* Start the html document, set headers, begin body and display the default
* navbar.
@ -62,7 +101,16 @@ function display_header($pageTitle) {
<body>
<main>
<div id=\"header\">
<h1> Zdenek Borovec Home Page </h1>
<table class=\"noborder-table\">
<tr>
<td>
<h1> Zdenek Borovec Home Page </h1>
</td>
<td style=\"text-align: right; padding-right: 2em;\">
%s
</td>
</tr>
</table>
<ul>
<li>
<a href=\"http://www.zdenekborovec-dev.cz\">
@ -83,6 +131,6 @@ function display_header($pageTitle) {
</a>
</li>
</ul>
</div>", $pageTitle);
</div>", $pageTitle, construct_account_section());
}
?>

View file

@ -403,7 +403,7 @@ $blogPost = load_blog($conn, $blogId);
// If no ID was entered, display warning and die.
if(!$blogPost) {
display_header("Article not found");
display_header("Article not found");
printf("
<article>
<h2>The article you requested doesn't exist.</h2>