session example


use CodeIgniter\Test\CIUnitTestCase;
use Config\Services;

/** * @internal */
final class ExampleSessionTest extends CIUnitTestCase
{
    public function testSessionSimple()
    {
        $session = Services::session();

        $session->set('logged_in', 123);
        $this->assertSame(123, $session->get('logged_in'));
    }
}
/** * Helper function to return to previous page. * * Example: * return redirect()->back(); * * @return $this */
    public function back(?int $code = null, string $method = 'auto')
    {
        Services::session();

        return $this->redirect(previous_url()$method$code);
    }

    /** * Sets the current $_GET and $_POST arrays in the session. * This also saves the validation errors. * * It will then be available via the 'old()' helper function. * * @return $this */
$response ??= Services::response();

        if ((ENVIRONMENT !== 'testing' && (is_cli() || $request->isSecure()))
            || $request->getServer('HTTPS') === 'test'
        ) {
            return; // @codeCoverageIgnore         }

        // If the session status is active, we should regenerate         // the session ID for safety sake.         if (ENVIRONMENT !== 'testing' && session_status() === PHP_SESSION_ACTIVE) {
            Services::session(null, true)->regenerate(); // @codeCoverageIgnore         }

        $baseURL = config(App::class)->baseURL;

        if (strpos($baseURL, 'https://') === 0) {
            $authority = substr($baseURLstrlen('https://'));
        } elseif (strpos($baseURL, 'http://') === 0) {
            $authority = substr($baseURLstrlen('http://'));
        } else {
            $authority = $baseURL;
        }

        
$this->generateHash();
        }
    }

    private function isCSRFCookie(): bool
    {
        return $this->config->csrfProtection === self::CSRF_PROTECTION_COOKIE;
    }

    private function configureSession(): void
    {
        $this->session = Services::session();
    }

    private function configureCookie(CookieConfig $cookie): void
    {
        $cookiePrefix     = $cookie->prefix;
        $this->cookieName = $cookiePrefix . $this->rawCookieName;
        Cookie::setDefaults($cookie);
    }

    /** * CSRF Verify * * @return $this|false * * @throws SecurityException * * @deprecated Use `CodeIgniter\Security\Security::verify()` instead of using this method. * * @codeCoverageIgnore */
if (is_array($input)) {
            // Note: in_array('', array(0)) returns TRUE, do not use it             foreach ($input as &$v) {
                if ($value === $v) {
                    return ' checked="checked"';
                }
            }

            return '';
        }

        $session     = Services::session();
        $hasOldInput = $session->has('_ci_old_input');

        // Unchecked checkbox and radio inputs are not even submitted by browsers ...         if ((string) $input === '0' || ! empty($request->getPost()) || $hasOldInput) {
            return ($input === $value) ? ' checked="checked"' : '';
        }

        return ($default === true) ? ' checked="checked"' : '';
    }
}

Home | Imprint | This part of the site doesn't use cookies.