HTML file, how to include header.html, menu.html, session php file and php code?

Hello. I am having a hard time trying to figure out how to structure my code inside pages, what to include and where. Basically I have a typical html page with a form in it. I also have a php file, that checks for sessions, and along this I have header.html, and menu.html
My form page looks like this:

<html>
<head> here I have some files linked that I would like put them in a file and include them on the pages i need. Should I put them in a html file and include them in every head page where I need them?
</head>
</body>
</html>

This if my php session file, where I check for sessions:

session_start();
if  (isset($_SESSION['session_user_first_name'])) 
{
if( isset($_SESSION[‘last_acted_on’]) && (time() - $_SESSION[‘last_acted_on’] > 60*5) )
  {
    session_unset();     // unset $_SESSION variable for the run-time
    session_destroy();   // destroy session data in storage
    header('Location: index.html');
  }
else 
  {
   session_regenerate_id(true);
    $_SESSION[‘last_acted_on’] = time();
   
    require_once ('database_initialisation.php');
  }
}

I also have some php code that would need to be executed on this html page:

$u_password = $_POST['passwordInput'];
$u_name = $_POST['nameInput'];
$u_l_name = $_POST['surnameInput'];
 ..... more code ...
$admin_query->addEmployerData($u_password, $u_name, $u_l_name, $u_DOB, $u_address,    $u_email, $u_phone, $u_gender, $u_position, $u_contract_type, $u_contract_hours);

I have been struggling with this files to make them work together, and I would really be grateful if you guys could help me a bit understanding how would be best to put them together.
What should I put in the head, what in the body, and also where should I check for the session?

Let’s say your session file is called sessions.php, and that final PHP snippet you gave will be given from another page right? This should be what you use

<?php
include "sessions.php"
?>
<html>
<head> here I have some files linked that I would like put them in a file and include them on the pages i need. Should I put them in a html file and include them in every head page where I need them?
</head>
<body>
$u_password = $_POST['passwordInput'];
$u_name = $_POST['nameInput'];
$u_l_name = $_POST['surnameInput'];
 ..... more code ...
$admin_query->addEmployerData($u_password, $u_name, $u_l_name, $u_DOB, $u_address,    $u_email, $u_phone, $u_gender, $u_position, $u_contract_type, $u_contract_hours);
</body>
</html>

Ok. Cool, so session file is right at the top before HTML. And for the header, and menu? Body, right?
And the PHP that I will be using for this page include from another file. At the top, or in the body?

That sessions file must be at the very top since you are also using PHP headers. Headers cannot be used once content is given to the browser so it needs to be at the top.

So yes, sessions/headers should be before anything else on your page. Thus, at the top of your file.

To clarify, headers can be anywhere in your file, AS LONG as it’s before any output to the browser (HTML, etc.)

This is what I have for now. The menu and the header is not loading:

It’e menu.html, but still not loading.

Why are you loading those pages before <body>? Include function in PHP will basically copy/paste whatever file you are loading, and put it into your page.

So get a regular page working without PHP includes

Then when that works, cut/paste a section of code into a PHP include file (menu.php for example) and EXACTLY WHERE YOU CUT/PASTE the file, put <?php include "menu.php"?>

Think of PHP includes as copy/pasting whatever is in that file and putting it into the new file.

Thinking of this, can you honestly say what you are doing is valid? No. Not close :slight_smile: .

The form is loading correctly with the right css style, but even if I include the header and menu at the beginning of the body, it is not loading…

Are you getting any errors? Check your error logs.

And are those header/menu files in the same directory as this page?

I took the content of the header and put it right after <body> and it was loading. When I do <body><?php include("header.php"); ?> it is not loading…

Paste your main page code inside of three back ticks (to the left of the 1 on your keyboard)

Then give us header.php’s code as well.

Also what errors are you getting? Any?

<?php 
  //include("sessions.php");
  ?>

  <html>
  <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
      <meta name="description" content="">
      <meta name="author" content="">
      <link rel="icon" href="../../favicon.ico">
      <title>Dashboard Template for Bootstrap</title>
      <!-- Bootstrap core CSS -->
      <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
      <link href="bootstrap/css/bootstrapValidator.min.css" rel="stylesheet">
      <link href="bootstrap/css/template_theme.css" rel="stylesheet">
      <script src = "bootstrap/js/jquery_min.js" type = "text/javascript"></script>
      <script src = "bootstrap/js/bootstrap.min.js" type = "text/javascript"></script>
      <script src = "bootstrap/js/bootstrapValidator.min.js" type = "text/javascript"></script>
  </head>
  <body>
  <?php include("header.php"); ?>


    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">

    <legend>Add Employer</legend>

    <form class="form-horizontal" method="post" >
      
                <div class="form-group">
                    <label for="name" class="col-md-2 control-label">Name</label>
                    <div class="col-sm-6">
                          <input type="text" class="form-control" name = "nameInput"  id="nameInput" placeholder="Name" required>
                    </div>
                </div>
          
                
        </form>
    </div>

  </body>
  </html>

   <?php
   <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
           <a class="navbar-brand"> 
           <?php 
           if (isset($_SESSION['session_user_first_name'])) { echo "Welcome: " . $_SESSION['session_user_first_name']; }
           else if (isset($_SESSION['session_member_first_name'])){echo "Welcome: " . $_SESSION['session_member_first_name']; } 
          ?>
            </a>
          </div>
          <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
            
              <li><a href="logout.php"> Logout </a></li>
            </ul>
            <form class="navbar-form navbar-right">
            </form>
          </div>
        </div>
  </nav>
  ?>
<?php
   <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
           <a class="navbar-brand"> 
           <?php 
           if (isset($_SESSION['session_user_first_name'])) { echo "Welcome: " . $_SESSION['session_user_first_name']; }
           else if (isset($_SESSION['session_member_first_name'])){echo "Welcome: " . $_SESSION['session_member_first_name']; } 
          ?>
            </a>
          </div>
          <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
            
              <li><a href="logout.php"> Logout </a></li>
            </ul>
            <form class="navbar-form navbar-right">
            </form>
          </div>
        </div>
  </nav>
  ?>

What is this? Is this your menu.html? If so, remove hte <?php and ?> at the top /end of the file

I siad PHP includes copy/paste whatever is in the file and put it in the main page. What you have in this code is invalid. All of that HTML is being parsed as PHP!!

I tried with and whiteout php before, still nothing…There is a lot of stuff there that i dont need, I know, but i didnt have the chance to take it out…

You still are refusing to tell me what errors you are getting. I have asked 3 times. Please look in your error log. This should be your main page.

<?php 
  include("sessions.php");
  ?>

  <html>
  <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
      <meta name="description" content="">
      <meta name="author" content="">
      <link rel="icon" href="../../favicon.ico">
      <title>Dashboard Template for Bootstrap</title>
      <!-- Bootstrap core CSS -->
      <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
      <link href="bootstrap/css/bootstrapValidator.min.css" rel="stylesheet">
      <link href="bootstrap/css/template_theme.css" rel="stylesheet">
      <script src = "bootstrap/js/jquery_min.js" type = "text/javascript"></script>
      <script src = "bootstrap/js/bootstrap.min.js" type = "text/javascript"></script>
      <script src = "bootstrap/js/bootstrapValidator.min.js" type = "text/javascript"></script>
  </head>
  <body>
  <?php include("header.php"); ?>


    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">

    <legend>Add Employer</legend>

    <form class="form-horizontal" method="post" >
      
                <div class="form-group">
                    <label for="name" class="col-md-2 control-label">Name</label>
                    <div class="col-sm-6">
                          <input type="text" class="form-control" name = "nameInput"  id="nameInput" placeholder="Name" required>
                    </div>
                </div>
          
                
        </form>
    </div>

  </body>
  </html>

This should be your menu.html (change it to menu.php actually.)

<nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
           <a class="navbar-brand"> 
           <?php 
           if (isset($_SESSION['session_user_first_name'])) { echo "Welcome: " . $_SESSION['session_user_first_name']; }
           else if (isset($_SESSION['session_member_first_name'])){echo "Welcome: " . $_SESSION['session_member_first_name']; } 
          ?>
            </a>
          </div>
          <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
            
              <li><a href="logout.php"> Logout </a></li>
            </ul>
            <form class="navbar-form navbar-right">
            </form>
          </div>
        </div>
  </nav>

This should be your sessions.php

<?php
session_start();
if  (isset($_SESSION['session_user_first_name'])) 
{
if( isset($_SESSION[‘last_acted_on’]) && (time() - $_SESSION[‘last_acted_on’] > 60*5) )
  {
    session_unset();     // unset $_SESSION variable for the run-time
    session_destroy();   // destroy session data in storage
    header('Location: index.html');
  }
else 
  {
   session_regenerate_id(true);
    $_SESSION[‘last_acted_on’] = time();
   
    require_once ('database_initialisation.php');
  }
}
?>

If this does not work, please TELL US THE ERRORS :slight_smile: .

I don’t see what your header.php contains. You didn’t post that.

I don’t know where to check the errors… :frowning:
This is actually the header or black bar at the top, has a lot of stuff that i don’t need, it was a theme from bootstrap.
Header:

<nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container-fluid"> <div class="navbar-header">… and more stuff.

Ok and that’s the header.php? That’s fine then.

Now, your code in all your files should match what I just posted. Is that correct? Does what you have in your files match what I posted in post 15?

Errors can be found in the error log file. Look in your directory (same one with these pages) and you should see an error_log file (that exact name.)

[10-Apr-2015 20:40:14 Europe/Berlin] PHP Notice: Use of undefined constant ‘last_acted_on’ - assumed ‘‘last_acted_on’’ in /Applications/MAMP/htdocs/mysqli/welcome_employer.php on line 5
[10-Apr-2015 20:40:14 Europe/Berlin] PHP Notice: Use of undefined constant ‘last_acted_on’ - assumed ‘‘last_acted_on’’ in /Applications/MAMP/htdocs/mysqli/welcome_employer.php on line 14

I used the code from post 15, and i still get just this…

None of the error refer to this page…