-1

I'm trying to include a header file but its not working like this:

<?php
include 'includes/header.php';
?>

Thats what I have above in my page and I'm getting this error on every page of my website:

Notice: Undefined index: logged in C:\xampp\htdocs\deWaai\includes\header.php on line 14

This is the code in header.php:

<!DOCTYPE html>
<html>

<?php
    include 'head.php';
?>

<body>
<div>

<?php
include "./functions/connection.php";
//kijken of de gebruiker is ingelogd
if($_SESSION['logged'] == 1) {
    //admin
    if($_SESSION['userlevel'] == 1) {
        include 'includes/adminmenu.php';
    //medewerker
    } elseif ($_SESSION['userlevel'] == 2) {
        include 'includes/medewerkermenu.php';
    //cursist
    } else {
        include 'includes/cursistmenu.php';
    }
} else {
    include 'includes/menu.php';
}
?>

</div>

php code in head.php:

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zeilschool De Waai</title>
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.3.1/css/swiper.min.css">
    <link rel="stylesheet" href="assets/css/styles.css">
    <link rel="stylesheet" href="assets/css/Navigation-Clean1.css">
    <link rel="stylesheet" href="assets/css/Navigation-with-Button1.css">
    <link rel="stylesheet" href="assets/css/Simple-Slider.css">
    <link rel="stylesheet" href="assets/fonts/ionicons.min.css">

    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.3.1/js/swiper.jquery.min.js"></script>
    <script src="assets/js/Simple-Slider.js"></script>

</head>

What am I doing wrong and why can't I include header.php on all my pages on my website? Any kind of help is appreciated I'm using Xampp apache server. (deWaai is the name of my project) (head and header.php are both in my include folder like this: htdocs -> deWaai -> includes >

Any kind of help is appreciated, thx

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Jeremy
  • 35
  • 7
  • 1
    The issue has nothing to do with including files. The error is here: `if ($_SESSION['logged'] == 1)`. You're trying to fetch a key that doesn't exist from the session array. You need to check if it exists first (using isset()). Don't forget to put a `session_start()` in the top of the file as well. – M. Eriksson Apr 23 '18 at 11:30
  • you have too many unclosed questions, IMHO. – Funk Forty Niner Apr 23 '18 at 11:52

1 Answers1

2

Replace your header.php file with this:

<!DOCTYPE html>
<html>

<?php
    include 'head.php';
?>

<body>
<div>

<?php
include "./functions/connection.php";
//kijken of de gebruiker is ingelogd
if(isset($_SESSION['logged']) && $_SESSION['logged'] == 1) {
    //admin
    if($_SESSION['userlevel'] == 1) {
        include 'includes/adminmenu.php';
    //medewerker
    } elseif ($_SESSION['userlevel'] == 2) {
        include 'includes/medewerkermenu.php';
    //cursist
    } else {
        include 'includes/cursistmenu.php';
    }
} else {
    include 'includes/menu.php';
}
?>

</div>

Always check whether a session is set or not, before checking its value, to prevent errors like these.

131
  • 1,363
  • 1
  • 12
  • 32