Welcome Guest, Not a member yet? Register   Sign In
Sessions, how?
#1

(This post was last modified: 09-08-2020, 05:03 AM by blaasvaer.)

I seem to constantly having a hard time reading and actually USING the docs:

Can someone tell med HOW, WHERE and WHEN to use this:
Code:
$session = \Config\Services::session($config);

or

$session = \Config\Services::session(); // Without the $config


It's the first line of 'code' in the docs about the Session Library (https://codeigniter4.github.io/userguide...ht=session) ... but I cannot – from the docs – figure out HOW to implement and USE it.

If I put it into a Controller, I get this:
Code:
syntax error, unexpected '$session' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST)

Can somone explain why I'm getting this, and what it is that I seem to be constantly missing when 'reading' the docs?

I see this a LOT in the docs '$variable = something … ', but WHENEVER I try to implement what I'm reading in the docs I ALWAYS get errors.

Now, does:

Code:
$variable = ...

in the docs have some 'magical' meaning (should it be 'read' in some particular way)? Or is it simply because the docs 'assume' YOU know where you're supposed to 'put' this code ... and therefore leaving out the (not so) 'obvious'?
Reply
#2

(This post was last modified: 09-08-2020, 09:01 AM by captain-sensible.)

yes i struggle with the docs sometimes; what i do is get concepts working anyway i can then re-visit docs for more elegant ways of doing it.

This is the start of one of my controller's :
Code:
public function credentials()

{
    session_start();
        
    $theArray= $_SESSION['captcha'];

first line of controller (and also any controller) that will pick up the ball will have as first line
session_start();

I set a session array variable using $_SESSION


then later

Code:
public function logout()
              {
               session_start();
                unset($_SESSION['role']);
                unset($_SESSION['count']);
// first line is still session_start();

this is part of a method to unset sessions I use

this is basically old school php ; i'm using it on codeigniter4.0.4


mind you, when i say old school, $_SESSION is not that old since if you look inside SESSION.php file

mine is at vendor/codeigniter4/framework/system/Session/Session.php there are a lot of references and use of $_SESSION
Reply
#3

(This post was last modified: 09-08-2020, 04:07 PM by stlake2011.)

Looking at your code, it looks like your not declaring the $session variable first. 

Heres a quick example:

PHP Code:
class Controller
{
    protected $session;

    public function __construct()
    {
        $this->session = \Config\Services::session();
    }


Or if you need/want sessions available everywhere, then uncomment the line:
$this->session = \Config\Services:Confusedession();
In the BaseController and extend your controllers off that.

If you search a bit, I believe InSiteFX has a better way to implement them, by checking to see if the session is already active or not, posted here in the forums, just can't remember where.

Hope this helps.
Reply
#4

(This post was last modified: 09-09-2020, 05:10 AM by InsiteFX.)

I start it in the BaseController:

PHP Code:
<?php

namespace App\Controllers;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *     class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 *
 * @package CodeIgniter
 */

use CodeIgniter\Controller;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Config\Services;
use 
Psr\Log\LoggerInterface;

/**
 * Class BaseController
 *
 * @package App\Controllers
 */
class BaseController extends Controller
{

    
/**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    
protected $helpers = [];

    
/**
     * @var string
     * Holds the session instance
     */
    
protected $session;

    
/**
     * Constructor.
     */
    
public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);

        
//--------------------------------------------------------------------
        // Preload any models, libraries, etc, here.
        //--------------------------------------------------------------------
        // E.g.:
        // $this->session = \Config\Services::session();

        // Ensure that the session is started and running
        
if (session_status() == PHP_SESSION_NONE)
        {
            
$this->session Services::session();
        }

    }

}
   // End of BaseController Class.

/**
 * -----------------------------------------------------------------------
 * Filename: BaseController.php
 * Location: ./app/Controllers/BaseController.php
 * -----------------------------------------------------------------------
 */ 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB