Also I find it easier to have a header.inc.php (that includes a navigational menu) and a footer (that includes my JQuery (Javascript)).
Here’s an example of what I’m talking about:
header.inc.php
[php]<?php
$phpSelf = filter_input(INPUT_SERVER, ‘PHP_SELF’, FILTER_SANITIZE_URL);
$path_parts = pathinfo($phpSelf);
$basename = $path_parts[‘basename’];
$filename = $path_parts[‘filename’];
$className = NULL;
$my_links = [
‘login.php’ => “Home”,
‘profile.php’ => “Profile”
];
function clearSession() {
unset($_SESSION[‘first_name’]);
unset($_SESSION[‘last_name’]);
unset($_SESSION[‘username’]);
unset($_SESSION[‘email’]);
}
clearSession();
$data = [];
$errMsg = [];
$spambotError = NULL;
$submit = filter_input(INPUT_POST, ‘submit’, FILTER_SANITIZE_SPECIAL_CHARS);
if (isset($submit) && $submit === ‘login’) {
$data[‘username’] = filter_input(INPUT_POST, ‘username’, FILTER_SANITIZE_SPECIAL_CHARS); //Why wouldn’t we allow special chars in username and password
$data[‘password’] = filter_input(INPUT_POST, ‘password’, FILTER_SANITIZE_SPECIAL_CHARS); // In fact we want special chars in password to make it complex
$user = $login->read($data);
if ($user === TRUE) {
header(‘Location: index.php’);
exit();
}
}
$logout = filter_input(INPUT_GET, ‘logout’, FILTER_SANITIZE_SPECIAL_CHARS);
if (isset($logout) && $logout === ‘yes’) {
$login->delete();
}
?>
<!doctype html>
The Virtual Bar
The Virtual Bar
<?php echo ($user === FALSE) ? 'Username or Password is incorrect, please re-enter!' : NULL; ?>
- Home
<?php echo (!$user) ? '-
Register' . "\n" : NULL; ?>
<?php echo ($user) ? '
- Edit Profile
' . "\n" : NULL; ?>
<?php echo ($user) ? '- Logoff
' . "\n" : NULL; ?>
<?php
if ($user) {
echo '
Welcome, ' . $user->username . '!' . "\n";
} else {
?>
Username:
Password:
<?php } ?>
[/php]
That way if I have to make any change to the menu (navigational) then I just have to change the header.inc.php file and not every page.