Welcome Guest, Not a member yet? Register   Sign In
Undefined variable: _SESSION
#1

I'm developing a pretty large project with CI4 and things have been working out pretty well so far. This is the first time I've run into an issue and cannot figure out why. php spark won't load with the following code in place:

C:\Users\Kyle\Desktop\OGD\framework>php spark serve

CodeIgniter CLI Tool - Version 4.0.0-alpha.2 - Server-Time: 2018-11-20 09:45:28am


An uncaught Exception was encountered

Type:        ErrorException
Message:     Undefined variable: _SESSION
Filename:    C:\Users\Kyle\Desktop\OGD\framework\system\Session\Session.php
Line Number: 474

If I comment out the following code php spark serve will work:

PHP Code:
<?php
namespace App\Filters;

use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\Filters\FilterInterface;

class 
AuthUser implements FilterInterface {

    public function 
before(RequestInterface $request) {
        
$session session();
        
$user $session->get('user');

        if (empty(
$user['id'])) {
            return 
redirect('login');
        }
    }
    
    public function 
after(RequestInterface $requestResponseInterface $response) {

    }
}
?>

I think the real offending line is:

PHP Code:
return redirect('login'); 

I can run php -S localhost:8080 and it all works. Is this because the code is running through the CLI?

Any thoughts?
Reply
#2

PHP Code:
//$session = session();
//$user = $session->get('user'); 


Removes the session error message. Is the FilterInterface being called too early in the bootstrap process before sessions can even work?

PHP Code:
return redirect('login'); 

This line causes no errors but spark will load but no server will start.

This is how I fixed it to load with no errors:

PHP Code:
    public function before(RequestInterface $request) {
        if (! 
$request->isCLI()) {
            
$session session();
            
$user $session->get('user');

            if (empty(
$user['id'])) {
                return 
redirect('login');
            }
        }
    } 

Is this a bug or am I doing something wrong?

Thanks,
kyle
Reply
#3

Sessions are not available form the command line - they are an HTTP thing.
https://codeigniter4.github.io/CodeIgnit...sions-work
Reply
#4

Hi ciadmin, I know they are an HTTP thing. I guess then the new question is: Do I need to comment all of the things in my code that do not work with the CLI when running "php spark serve"? I'm trying to debug my code on my local machine. Why can't "php spark serve" just bring up the web server without parsing my code? The funny thing is I can comment out the session code, run php spark serve and then add the code back and it'll all work fine. Should I not use php spark serve for local testing?

Thanks,
Kyle
Reply
#5

According with this lines https://github.com/codeigniter4/CodeIgni...#L288-L300 the filters will run if match the config.

Maybe a property in the user filter class could help with that. Or in the \Config\Filters.

PHP Code:
public $ignored = [
 
   'cli'  => ['serve''etc'],
 
   'http' => [],
]; 

Otherwise you will always need to add something like https://github.com/codeigniter4/CodeIgni...hp#L27-L30
Reply
#6

If you're using php spark serve it should work mostly like any other web server. Which means that, no, you don't need to comment out session based stuff.

Looks like it's because you're using it in a filter that's causing the problem, if I had to guess. That's probably something we need to look into.

Your workaround works fine for now. I think the filter is being ran when the server is being setup, which it probably shouldn't be.
Reply
#7

I just pushed a fix for this. Let me know if it works for you.
Reply
#8

(11-22-2018, 09:49 PM)kilishan Wrote: I just pushed a fix for this. Let me know if it works for you.

Thanks so much kilishan! I really appreciate the very quick bug fix. I'll test this when Alpha 3 is released. I don't want to change the framework that often in the middle of development.

Happy Holidays!
Reply
#9

(11-22-2018, 09:49 PM)kilishan Wrote: I just pushed a fix for this. Let me know if it works for you.

In alpha3 you fixed the issue where php spark serve would not load (or exit out) if a redirect was in a filter... but I think a side effect of this is redirects no longer work in filters AT ALL. I'm not sure if this is because of your code or something else related to redirects... but redirects no longer work in filters at all... try a simple "return redirect('test');" in before method and you'll see.
Reply
#10

That should be fixed as of a day or so ago. Let me know if it's working for you or not!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB