[php] Undefined variable: $_SESSION

I'm getting E_NOTICE errors in a core CakePHP file when it tries to reference a never-set or unset session (cake/libs/cake_session.php line 372):

function read($name = null) {
    if (is_null($name)) {
        return $this->__returnSessionVars();
    }
    if (empty($name)) {
        return false;
    }
    $result = Set::classicExtract($_SESSION, $name);
}

I've done a search through my code (in the app/ directory) and I can't find references to $_SESSION or session_destroy. Am I missing anything?

This error shows up when I try to run any unit tests. Is this...normal? I've cleared out the cake/ directory and replaced it with another one (same version) just to make sure that I hadn't inadvertently modified anything in the core files, but I still get the same error. I'm not sure if this is just a flaw in the framework or something else.

EDIT

Here are the results of the test run on the command line:

Welcome to CakePHP v1.3.11 Console
---------------------------------------------------------------
App : app
Path: /var/www/program/app
---------------------------------------------------------------
CakePHP Test Shell
---------------------------------------------------------------
Running app case models/owners_equity
E_NOTICE: Undefined variable: _SESSION in /var/www/program/cake/libs/cake_session.php on line 372
E_NOTICE: Undefined variable: _SESSION in /var/www/program/cake/libs/cake_session.php on line 372
ERROR->Unexpected PHP error [Undefined variable: _SESSION] severity [E_NOTICE] in [/var/www/program/cake/libs/cake_session.php line 372]
    in testGenerateOwnerWithdrawals
    in BalanceTestCase
    in /var/www/program/app/tests/cases/models/owners_equity.test.php

ERROR->Unexpected PHP error [Undefined variable: _SESSION] severity [E_NOTICE] in [/var/www/program/cake/libs/cake_session.php line 372]
    in testGenerateOwnerWithdrawals
    in BalanceTestCase
    in /var/www/program/app/tests/cases/models/owners_equity.test.php

This question is related to php session cakephp e-notices

The answer is


You need make sure to start the session at the top of every PHP file where you want to use the $_SESSION superglobal. Like this:

<?php
  session_start();
  echo $_SESSION['youritem'];
?>

You forgot the Session HELPER.

Check this link : book.cakephp.org/2.0/en/core-libraries/helpers/session.html


Another possibility for this warning (and, most likely, problems with app behavior) is that the original author of the app relied on session.auto_start being on (defaults to off)

If you don't want to mess with the code and just need it to work, you can always change php configuration and restart php-fpm (if this is a web app):

/etc/php.d/my-new-file.ini :

session.auto_start = 1

(This is correct for CentOS 8, adjust for your OS/packaging)


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to session

What is the best way to manage a user's session in React? Spring Boot Java Config Set Session Timeout PHP Unset Session Variable How to kill all active and inactive oracle sessions for user Difference between request.getSession() and request.getSession(true) PHP - Session destroy after closing browser Get Current Session Value in JavaScript? Invalidating JSON Web Tokens How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session How can I get session id in php and show it?

Examples related to cakephp

SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' using CakePHP CakePHP 3.0 installation: intl extension missing from system How to check not in array element 404 Not Found The requested URL was not found on this server What is /var/www/html? Request exceeded the limit of 10 internal redirects due to probable configuration error how we add or remove readonly attribute from textbox on clicking radion button in cakephp using jquery? PHP Fatal error: Class 'PDO' not found Undefined variable: $_SESSION CentOS: Enabling GD Support in PHP Installation

Examples related to e-notices

Undefined variable: $_SESSION