PHP-View icon indicating copy to clipboard operation
PHP-View copied to clipboard

$_SESSION global variable is not available when rendering $content

Open scsmash3r opened this issue 4 years ago • 1 comments

How to reproduce:

$_SESSION['user_id'] = 'some_value';

$phpView = new PhpRenderer("path/to/templates", ["title" => "My App"]);
$phpView->setLayout("layout.php");

//...

$phpview->render($response, "hello.php", ["title" => "Hello - My App", "name" => "John"]);

In hello.php:

<?php var_dump($_SESSION); // Will not be set ?>
Hello <?=$name?>! Your id is: <?= $_SESSION['user_id'] ?>

In layout.php:

<?php var_dump($_SESSION); // Will be filled as intended ?>
<html><head><title><?=$title?></title></head><body><?=$content?></body></html>

So, in both hello.php and layout.php you will be able to get $title var. But, in hello.php global $_SESSION var will be empty. Expected behavior would be leaving global variables intact.

scsmash3r avatar Jul 07 '20 10:07 scsmash3r

If you need the session in whole or in part you should send it as a parameter to the render method.

twogood avatar Dec 28 '20 17:12 twogood