session_start() placement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

session_start() placement

Post by Steveo31 »

This has to be in the top of the page, start of the first <?php right? For example:

Code: Select all

<?php

session_start();
include ('header.php');

$var1 = $_POST['var'];

// so on
I've seen it in various places in other's code...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It just needs to come before any output so the first line of the script makes it a logical choice.
I usually have it after all the defines, requires etc.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

:)
aleigh
Forum Commoner
Posts: 26
Joined: Thu Mar 25, 2004 11:06 am
Location: Midwestern United States
Contact:

Post by aleigh »

markl999 wrote:It just needs to come before any output so the first line of the script makes it a logical choice.
I usually have it after all the defines, requires etc.
If I am building a larger system I tend to put it in a site-wide include, up at the top. The earlier the better, because invariably there will be some file that has a blank line at the buttom that constitutes "output" haha especially if multiple people are working on the project.
Post Reply