Hello!
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

session_start();

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!

Recommended Answers

All 8 Replies

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'

commented: Just readdressing the neg rep. Bit harsh I thought. +9

You did not understand the question

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.

<html>
<head>
<?php include('header'); ?>
</head>
<body>
</body>
</html>
Member Avatar for diafol

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:

include("includes/config.php");

This ensures that the session_start() is included.

My header got all from

<doc type

to

<body>

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.

Member Avatar for diafol

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!
#over&out

Member Avatar for diafol

OK we solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.