0

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!

4
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by diafol
0

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.
0

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>
1

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.

0

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.

Edited by Sorcher: typo

0

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.

0

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

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.