I am beating myself up here.
I use

include "header";

on all pages, at the top, the header contains everything within <header></header> on a html page.
But should i use


inside the header or before i include header.php on all pages?

The session on my website logs out randomly, so i know i got a problem with the session settings.

Any tip would be great!

6 Years
Discussion Span
Last Post by diafol

you have to specify the full file name inside include. If header.php is in the same directory as your other files then include 'header.php' . Otherwise, include 'your file path/header.php'

Votes + Comments
Just readdressing the neg rep. Bit harsh I thought.

Depends, session_start needs to be called before any output occurs or you'll get an error.

If your header include contains everything that would be between the header tags on the page, what is creating the container html? If your code looks like the following and you put the session start in the header include, it will error out, as the html is being sent to the browser before the session starts.

<?php include('header'); ?>

session_start(); is my first statement as a rule. UNLESS I have a common file included in every page, which is usually the case, e.g. config.php, so I put the session_start() at the top of that file instead and ensure that that file is included as a first statement:


This ensures that the session_start() is included.


My header got all from

<doc type



and then the pages that got header.php included got </body>
So i am just wondering if session_start(); should be in the header.php or before i include header.php on all pages.

Edited by Sorcher: typo


Either if it is the first thing to appear in the file - either directly in the file itself or from your include file (if the include file is the first thing to appear in the file and the session_start is the first thing to appear in the include file). It shouldn't make any difference. Technically, I don't think it matters if it is the first thing, just as long as there is no html output.


Thanks my friend. This made sense! Time to light this "one" up, get a beer and start coding hard for an article posting and category function!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.