PHP Session Question

If I have session_start();at the top of my header page, is it ok to include $page_titlebefore the header?

$page_title = 'Some Title';
include ('../includes/header.php');
+3
source share
3 answers

Yes. As long as you don't bring anything out in front of you, you should be fine ...

The reason is that it session_startsends multiple HTTP headers. Therefore, if you already output something (including errors), it will not be executed, and as such will not start.

, . ob_start(); . , , ( ), ( "" , , comamnd)...

: :

<?php
echo 'foo';
include '../includes/header.php';

, - ...

<?php
include 'non/existant/file.php';
include '../includes/header.php';

, include , ...

FooBar<?php
include '../includes/header.php';

, - ( <?php)...

<?php
ob_start();
echo 'foo';
include '../includes/header.php';

, ...

FooBar<?php
ob_start();
include '../includes/header.php';

, ...

<?php
include 'some/valid/file.php';
include '../includes/header.php';

. , . , ( , , ob_start())...

, , , include require, , ...

+4

session_start(); . , .., , .

+1

.

.

.

echo "This will break";
session_start();

, , / , , .

0

Source: https://habr.com/ru/post/1762478/


All Articles