I am new to HTML and programming in general and am currently working on setting up my first website.
For this I created a couple of PHP pages with a separate file for each of them and in order to avoid duplicating code I tried to remove as much as possible from the pages header and saved this in a separate header file.
The header file (header.php) is then included on all pages through the following which is the first part of code on each page file:
<!-- header -->
<?php
require_once("includes/header.php");
?>
Now so far all my pages are working but I am not sure if the structure of my header is ideal and if there is anything I should add or change there - especially regarding the part where I include PHP as I heard that there can be issues with the structure when trying to set up a session etc.
Would be great if someone could have a look at this and let me know your feedback or suggestions.
My complete header file:
<!DOCTYPE html>
<html>
<head>
<?php
define("someUnguessableVariable", "anotherUnguessableVariable");
session_start();
if(!isset($_SESSION["login"]) && $_SESSION["login"] == ""){
header("location: login.php");
exit;
}
include "system/config.php";
$pageURL = basename($_SERVER["REQUEST_URI"]);
$pageName = pathinfo(parse_url($pageURL, PHP_URL_PATH), PATHINFO_FILENAME);
$selectedLang = $_GET["lang"];
if(!isset($selectedLang)){
$selectedLang = "de";
}
$langURL = "?lang=" . $selectedLang;
$conn = new mysqli($dbServer, $dbUser, $dbPass, $dbName);
$conn->set_charset("utf8");
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
// fetch main translations
$location = "%main%";
$stmt = $conn->prepare("SELECT tID, " . $selectedLang . " FROM TranslationsMain WHERE location LIKE ? ORDER BY tID");
$stmt->bind_param("s", $location);
$stmt->execute();
$result = $stmt->get_result();
while($arrTranslations = $result->fetch_assoc()){
$trans[] = array("ID" => $arrTranslations["tID"], "trans" => $arrTranslations[$selectedLang]);
}
$conn->close();
// get main translations by ID
function fetchTransMain($trans, $itemID){
foreach($trans as $key => $val){
if($val["ID"] == $itemID){
return $val["trans"];
}
}
}
?>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="author" content="Some author" />
<meta name="description" content="Created: 2015-06" />
<base href="http://www.myurl.de" target="_self" />
<title>Some title</title>
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="includes/styles.css" />
<!-- CSS - Font Awesome -->
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" />
<!-- favicon -->
<link rel="shortcut icon" href="images/favicon/favicon.ico" type="image/x-icon" />
<link rel="icon" href="images/favicon/favicon.png" type="image/png" />
<link rel="icon" sizes="32x32" href="images/favicon/favicon-32.png" type="image/png" />
<link rel="icon" sizes="64x64" href="images/favicon/favicon-64.png" type="image/png" />
<link rel="icon" sizes="96x96" href="images/favicon/favicon-96.png" type="image/png" />
<link rel="icon" sizes="196x196" href="images/favicon/favicon-196.png" type="image/png" />
<link rel="apple-touch-icon" sizes="152x152" href="images/favicon/apple-touch-icon.png" />
<link rel="apple-touch-icon" sizes="60x60" href="images/favicon/apple-touch-icon-60x60.png" />
<link rel="apple-touch-icon" sizes="76x76" href="images/favicon/apple-touch-icon-76x76.png" />
<link rel="apple-touch-icon" sizes="114x114" href="images/favicon/apple-touch-icon-114x114.png" />
<link rel="apple-touch-icon" sizes="120x120" href="images/favicon/apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon" sizes="144x144" href="images/favicon/apple-touch-icon-144x144.png" />
<meta name="msapplication-TileImage" content="favicon-144.png" />
<meta name="msapplication-TileColor" content="#ffffff" />
<script>
var baseURL = '<?php echo $baseURL; ?>';
var pageURL = '<?php echo $pageURL; ?>';
var pageName = '<?php echo $pageName; ?>';
var selectedLang = '<?php echo $selectedLang; ?>';
</script>
</head>
<body>
Note: jQuery and JavaScript are included separately through a footer file to improve loading of the pages.