mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
1330 lines
90 KiB
HTML
1330 lines
90 KiB
HTML
<!DOCTYPE html>
|
||
<html class="writer-html5" lang="en">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>Session Library — CodeIgniter 4.6.0 documentation</title>
|
||
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
|
||
<link rel="stylesheet" type="text/css" href="../_static/css/citheme.css" />
|
||
<link rel="stylesheet" type="text/css" href="../_static/css/citheme_dark.css" />
|
||
|
||
|
||
<link rel="shortcut icon" href="../_static/favicon.ico"/>
|
||
<!--[if lt IE 9]>
|
||
<script src="../_static/js/html5shiv.min.js"></script>
|
||
<![endif]-->
|
||
|
||
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/_sphinx_javascript_frameworks_compat.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/sphinx_highlight.js"></script>
|
||
<script src="../_static/js/citheme.js"></script>
|
||
<script src="../_static/js/carbon.js"></script>
|
||
<script src="../_static/js/theme.js"></script>
|
||
<link rel="index" title="Index" href="../genindex.html" />
|
||
<link rel="search" title="Search" href="../search.html" />
|
||
<link rel="next" title="Throttler" href="throttler.html" />
|
||
<link rel="prev" title="Security" href="security.html" />
|
||
</head>
|
||
|
||
<body class="wy-body-for-nav">
|
||
<div class="wy-grid-for-nav">
|
||
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
||
<div class="wy-side-scroll">
|
||
<div class="wy-side-nav-search" >
|
||
|
||
|
||
|
||
<a href="../index.html">
|
||
|
||
<img src="../_static/ci-logo-text.svg" class="logo" alt="Logo"/>
|
||
</a>
|
||
<div role="search">
|
||
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
|
||
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/requirements.html">Server Requirements</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/credits.html">Credits</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/psr.html">PSR Compliance</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../license.html">License Agreement</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_composer.html">Composer Installation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_manual.html">Manual Installation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/running.html">Running Your App</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/deployment.html">Deployment</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../changelogs/index.html">Change Logs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/repositories.html">CodeIgniter Repositories</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Build Your First Application</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static Pages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News Section</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create News Items</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/structure.html">Application Structure</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/mvc.html">Models, Views, and Controllers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/autoloader.html">Autoloading Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/services.html">Services</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/factories.html">Factories</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/http.html">Working with HTTP Requests</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/security.html">Security Guidelines</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/goals.html">Design and Architectural Goals</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/configuration.html">Configuration</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helper Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Global Functions and Constants</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/logging.html">Logging Information</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Web Page Caching</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/ajax.html">AJAX Requests</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/modules.html">Code Modules</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/routing.html">URI Routing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/controllers.html">Controllers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/filters.html">Controller Filters</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/auto_routing_improved.html">Auto Routing (Improved)</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/message.html">HTTP Messages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/request.html">Request Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/incomingrequest.html">IncomingRequest Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/content_negotiation.html">Content Negotiation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/methodspoofing.html">HTTP Method Spoofing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/restful.html">RESTful Resource Handling</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/views.html">Views</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_renderer.html">View Renderer</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_layouts.html">View Layouts</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_cells.html">View Cells</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_parser.html">View Parser</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_decorators.html">View Decorators</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/table.html">HTML Table Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/response.html">HTTP Responses</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/api_responses.html">API Response Trait</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/csp.html">Content Security Policy</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/localization.html">Localization</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/alternative_php.html">Alternate PHP Syntax for View Files</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working with Databases</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Methods</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting Metadata</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/events.html">Database Events</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../models/model.html">Using CodeIgniter's Model</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../models/entities.html">Using Entity Classes</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/forge.html">Database Forge</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/migration.html">Database Migrations</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/seeds.html">Database Seeding</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/db_commands.html">Database Commands</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul class="current">
|
||
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Library Reference</a><ul class="current">
|
||
<li class="toctree-l2"><a class="reference internal" href="caching.html">Caching Driver</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="cookies.html">Cookies</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="cors.html">Cross-Origin Resource Sharing (CORS)</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="curlrequest.html">CURLRequest Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="email.html">Email Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="encryption.html">Encryption Service</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="files.html">Working with Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="file_collections.html">File Collections</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="honeypot.html">Honeypot Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="images.html">Image Manipulation Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="pagination.html">Pagination</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="publisher.html">Publisher</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="security.html">Security</a></li>
|
||
<li class="toctree-l2 current"><a class="current reference internal" href="#">Session Library</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="throttler.html">Throttler</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="time.html">Times and Dates</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="typography.html">Typography</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="uploaded_files.html">Working with Uploaded Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="uri.html">Working with URIs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="user_agent.html">User Agent Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="validation.html">Validation</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/filesystem_helper.html">Filesystem Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/test_helper.html">Test Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/overview.html">Getting Started</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/database.html">Database</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/fabricator.html">Generating Data</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/controllers.html">Controller Testing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/feature.html">HTTP Testing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/response.html">Testing Responses</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/cli.html">Testing CLI Commands</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/mocking.html">Mocking</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/benchmark.html">Benchmarking</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/debugging.html">Debugging Your Application</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_overview.html">CLI Overview</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_controllers.html">Running Controllers via CLI</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/spark_commands.html">Spark Commands</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Creating Spark Commands</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_generators.html">CLI Generators</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_library.html">CLI Library</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_request.html">CLIRequest Class</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/core_classes.html">Creating Core System Classes</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/common.html">Replacing Common Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/events.html">Events</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/basecontroller.html">Extending the Controller</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/authentication.html">Authentication</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/composer_packages.html">Creating Composer Packages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="official_packages.html">Official Packages</a></li>
|
||
</ul>
|
||
|
||
</div>
|
||
</div>
|
||
</nav>
|
||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
|
||
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
||
<a href="../index.html">CodeIgniter</a>
|
||
</nav>
|
||
|
||
<div class="wy-nav-content">
|
||
<div class="rst-content">
|
||
<a class="btn btn-neutral float-right" href="https://github.com/codeigniter4/CodeIgniter4/edit/develop/user_guide_src/source/libraries/sessions.rst">Edit this page</a>
|
||
|
||
<div role="navigation" aria-label="Page navigation">
|
||
<ul class="wy-breadcrumbs">
|
||
<li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
|
||
<li class="breadcrumb-item"><a href="index.html">Library Reference</a></li>
|
||
<li class="breadcrumb-item active">Session Library</li>
|
||
<li class="wy-breadcrumbs-aside">
|
||
</li>
|
||
</ul>
|
||
<hr/>
|
||
</div>
|
||
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
||
<div itemprop="articleBody">
|
||
|
||
<section id="session-library">
|
||
<h1>Session Library<a class="headerlink" href="#session-library" title="Permalink to this heading"></a></h1>
|
||
<p>The Session class permits you to maintain a user’s “state” and track their
|
||
activity while they browse your site.</p>
|
||
<p>CodeIgniter comes with a few session storage drivers, that you can see
|
||
in the last section of the table of contents:</p>
|
||
<nav class="contents local" id="contents">
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="#using-the-session-class" id="id3">Using the Session Class</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#initializing-a-session" id="id4">Initializing a Session</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-sessions-work" id="id5">How Do Sessions Work?</a></p></li>
|
||
<li><p><a class="reference internal" href="#what-is-session-data" id="id6">What is Session Data?</a></p></li>
|
||
<li><p><a class="reference internal" href="#retrieving-session-data" id="id7">Retrieving Session Data</a></p></li>
|
||
<li><p><a class="reference internal" href="#adding-session-data" id="id8">Adding Session Data</a></p></li>
|
||
<li><p><a class="reference internal" href="#pushing-new-value-to-session-data" id="id9">Pushing New Value to Session Data</a></p></li>
|
||
<li><p><a class="reference internal" href="#removing-session-data" id="id10">Removing Session Data</a></p></li>
|
||
<li><p><a class="reference internal" href="#flashdata" id="id11">Flashdata</a></p></li>
|
||
<li><p><a class="reference internal" href="#tempdata" id="id12">Tempdata</a></p></li>
|
||
<li><p><a class="reference internal" href="#closing-a-session" id="id13">Closing a Session</a></p></li>
|
||
<li><p><a class="reference internal" href="#destroying-a-session" id="id14">Destroying a Session</a></p></li>
|
||
<li><p><a class="reference internal" href="#accessing-session-metadata" id="id15">Accessing Session Metadata</a></p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p><a class="reference internal" href="#session-preferences" id="id16">Session Preferences</a></p></li>
|
||
<li><p><a class="reference internal" href="#session-drivers" id="id17">Session Drivers</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#filehandler-driver-the-default" id="id18">FileHandler Driver (the default)</a></p></li>
|
||
<li><p><a class="reference internal" href="#databasehandler-driver" id="id19">DatabaseHandler Driver</a></p></li>
|
||
<li><p><a class="reference internal" href="#redishandler-driver" id="id20">RedisHandler Driver</a></p></li>
|
||
<li><p><a class="reference internal" href="#memcachedhandler-driver" id="id21">MemcachedHandler Driver</a></p></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</nav>
|
||
<section id="using-the-session-class">
|
||
<h2><a class="toc-backref" href="#id3" role="doc-backlink">Using the Session Class</a><a class="headerlink" href="#using-the-session-class" title="Permalink to this heading"></a></h2>
|
||
<section id="initializing-a-session">
|
||
<h3><a class="toc-backref" href="#id4" role="doc-backlink">Initializing a Session</a><a class="headerlink" href="#initializing-a-session" title="Permalink to this heading"></a></h3>
|
||
<p>Sessions will typically run globally with each page load, so the Session
|
||
class should be magically initialized.</p>
|
||
<p>To access and initialize the session:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span> <span class="o">=</span> <span class="nx">service</span><span class="p">(</span><span class="s1">'session'</span><span class="p">,</span> <span class="nv">$config</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">$config</span></code> parameter is optional - your application configuration.
|
||
If not provided, the services register will instantiate your default
|
||
one.</p>
|
||
<p>Once loaded, the Sessions library object will be available using:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$session</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Alternatively, you can use the helper function that will use the default
|
||
configuration options. This version is a little friendlier to read,
|
||
but does not take any configuration options.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span> <span class="o">=</span> <span class="nx">session</span><span class="p">();</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="how-do-sessions-work">
|
||
<h3><a class="toc-backref" href="#id5" role="doc-backlink">How Do Sessions Work?</a><a class="headerlink" href="#how-do-sessions-work" title="Permalink to this heading"></a></h3>
|
||
<p>When a page is loaded, the session class will check to see if a valid
|
||
session cookie is sent by the user’s browser. If a session cookie does
|
||
<strong>not</strong> exist (or if it doesn’t match one stored on the server or has
|
||
expired) a new session will be created and saved.</p>
|
||
<p>If a valid session does exist, its information will be updated. With each
|
||
update, the session ID may be regenerated if configured to do so.</p>
|
||
<p>It’s important for you to understand that once initialized, the Session
|
||
class runs automatically. There is nothing you need to do to cause the
|
||
above behavior to happen. You can, as you’ll see below, work with session
|
||
data, but the process of reading, writing, and updating a session is
|
||
automatic.</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Under CLI, the Session library will automatically halt itself,
|
||
as this is a concept based entirely on the HTTP protocol.</p>
|
||
</div>
|
||
<section id="a-note-about-concurrency">
|
||
<h4>A Note about Concurrency<a class="headerlink" href="#a-note-about-concurrency" title="Permalink to this heading"></a></h4>
|
||
<p>Unless you’re developing a website with heavy AJAX usage, you can skip this
|
||
section. If you are, however, and if you’re experiencing performance
|
||
issues, then this note is exactly what you’re looking for.</p>
|
||
<p>Sessions in CodeIgniter v2.x didn’t implement locking,
|
||
which meant that two HTTP requests using the same session could run exactly
|
||
at the same time. To use a more appropriate technical term - requests were
|
||
non-blocking.</p>
|
||
<p>However, non-blocking requests in the context of sessions also means
|
||
unsafe, because, modifications to session data (or session ID regeneration)
|
||
in one request can interfere with the execution of a second, concurrent
|
||
request. This detail was at the root of many issues and the main reason why
|
||
CodeIgniter 3 has a completely re-written Session library.</p>
|
||
<p>Why are we telling you this? Because it is likely that after trying to
|
||
find the reason for your performance issues, you may conclude that locking
|
||
is the issue and therefore look into how to remove the locks …</p>
|
||
<p>DO NOT DO THAT! Removing locks would be <strong>wrong</strong> and it will cause you
|
||
more problems!</p>
|
||
<p>Locking is not the issue, it is a solution. Your issue is that you still
|
||
have the session open, while you’ve already processed it and therefore no
|
||
longer need it. So, what you need is to close the session for the
|
||
current request after you no longer need it.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">close</span><span class="p">();</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="what-is-session-data">
|
||
<h3><a class="toc-backref" href="#id6" role="doc-backlink">What is Session Data?</a><a class="headerlink" href="#what-is-session-data" title="Permalink to this heading"></a></h3>
|
||
<p>Session data is simply an array associated with a particular session ID
|
||
(cookie).</p>
|
||
<p>If you’ve used sessions in PHP before, you should be familiar with PHP’s
|
||
<a class="reference external" href="https://www.php.net/manual/en/reserved.variables.session.php">$_SESSION superglobal</a>
|
||
(if not, please read the content on that link).</p>
|
||
<p>CodeIgniter gives access to its session data through the same means, as it
|
||
uses the session handlers’ mechanism provided by PHP. Using session data is
|
||
as simple as manipulating (read, set and unset values) the <code class="docutils literal notranslate"><span class="pre">$_SESSION</span></code>
|
||
array.</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>In general, it is bad practice to use global variables.
|
||
So using the superglobal <code class="docutils literal notranslate"><span class="pre">$_SESSION</span></code> directly is not recommended.</p>
|
||
</div>
|
||
<p>In addition, CodeIgniter also provides 2 special types of session data
|
||
that are further explained below: <a class="reference internal" href="#flashdata">Flashdata</a> and <a class="reference internal" href="#tempdata">Tempdata</a>.</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>For historical reasons, we refer to session data excluding Flashdata and Tempdata as “userdata”.</p>
|
||
</div>
|
||
</section>
|
||
<section id="retrieving-session-data">
|
||
<h3><a class="toc-backref" href="#id7" role="doc-backlink">Retrieving Session Data</a><a class="headerlink" href="#retrieving-session-data" title="Permalink to this heading"></a></h3>
|
||
<p>Any piece of information from the session array is available through the
|
||
<code class="docutils literal notranslate"><span class="pre">$_SESSION</span></code> superglobal:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$item</span> <span class="o">=</span> <span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">'item'</span><span class="p">];</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Or through the conventional accessor method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$item</span> <span class="o">=</span> <span class="nv">$session</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'item'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Or through the magic getter:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$item</span> <span class="o">=</span> <span class="nv">$session</span><span class="o">-></span><span class="na">item</span><span class="p">;</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Or even through the session helper method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$item</span> <span class="o">=</span> <span class="nx">session</span><span class="p">(</span><span class="s1">'item'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Where <code class="docutils literal notranslate"><span class="pre">item</span></code> is the array key corresponding to the item you wish to fetch.
|
||
For example, to assign a previously stored <code class="docutils literal notranslate"><span class="pre">name</span></code> item to the <code class="docutils literal notranslate"><span class="pre">$name</span></code>
|
||
variable, you will do this:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$name</span> <span class="o">=</span> <span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">'name'</span><span class="p">];</span>
|
||
|
||
<span class="c1">// or:</span>
|
||
|
||
<span class="nv">$name</span> <span class="o">=</span> <span class="nv">$session</span><span class="o">-></span><span class="na">name</span><span class="p">;</span>
|
||
|
||
<span class="c1">// or:</span>
|
||
|
||
<span class="nv">$name</span> <span class="o">=</span> <span class="nv">$session</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'name'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">get()</span></code> method returns null if the item you are trying
|
||
to access does not exist.</p>
|
||
</div>
|
||
<p>If you want to retrieve all of the existing session data, you can simply
|
||
omit the item key (magic getter only works for single property values):</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$userData</span> <span class="o">=</span> <span class="nv">$_SESSION</span><span class="p">;</span>
|
||
<span class="c1">// or:</span>
|
||
<span class="nv">$userData</span> <span class="o">=</span> <span class="nv">$session</span><span class="o">-></span><span class="na">get</span><span class="p">();</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition important">
|
||
<p class="admonition-title">Important</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">get()</span></code> method WILL return flashdata or tempdata items when
|
||
retrieving a single item by key. It will not return flashdata or tempdata when
|
||
grabbing all data from the session, however.</p>
|
||
</div>
|
||
</section>
|
||
<section id="adding-session-data">
|
||
<h3><a class="toc-backref" href="#id8" role="doc-backlink">Adding Session Data</a><a class="headerlink" href="#adding-session-data" title="Permalink to this heading"></a></h3>
|
||
<p>Let’s say a particular user logs into your site. Once authenticated, you
|
||
could add their username and e-mail address to the session, making that
|
||
data globally available to you without having to run a database query when
|
||
you need it.</p>
|
||
<p>You can simply assign data to the <code class="docutils literal notranslate"><span class="pre">$_SESSION</span></code> array, as with any other
|
||
variable. Or as a property of <code class="docutils literal notranslate"><span class="pre">$session</span></code>.</p>
|
||
<p>You can pass an array containing your new session data to the
|
||
<code class="docutils literal notranslate"><span class="pre">set()</span></code> method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">set</span><span class="p">(</span><span class="nv">$array</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Where <code class="docutils literal notranslate"><span class="pre">$array</span></code> is an associative array containing your new data. Here’s
|
||
an example:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$newdata</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'username'</span> <span class="o">=></span> <span class="s1">'johndoe'</span><span class="p">,</span>
|
||
<span class="s1">'email'</span> <span class="o">=></span> <span class="s1">'johndoe@some-site.com'</span><span class="p">,</span>
|
||
<span class="s1">'logged_in'</span> <span class="o">=></span> <span class="k">true</span><span class="p">,</span>
|
||
<span class="p">];</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">set</span><span class="p">(</span><span class="nv">$newdata</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If you want to add session data one value at a time, <code class="docutils literal notranslate"><span class="pre">set()</span></code> also
|
||
supports this syntax:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">set</span><span class="p">(</span><span class="s1">'some_name'</span><span class="p">,</span> <span class="s1">'some_value'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If you want to verify that a session value exists, simply check with
|
||
<code class="docutils literal notranslate"><span class="pre">isset()</span></code>:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="c1">// returns false if the 'some_name' item doesn't exist or is null,</span>
|
||
<span class="c1">// true otherwise:</span>
|
||
<span class="k">if</span> <span class="p">(</span><span class="nb">isset</span><span class="p">(</span><span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">'some_name'</span><span class="p">]))</span> <span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Or you can call <code class="docutils literal notranslate"><span class="pre">has()</span></code>:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">has</span><span class="p">(</span><span class="s1">'some_name'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="pushing-new-value-to-session-data">
|
||
<h3><a class="toc-backref" href="#id9" role="doc-backlink">Pushing New Value to Session Data</a><a class="headerlink" href="#pushing-new-value-to-session-data" title="Permalink to this heading"></a></h3>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">push()</span></code> method is used to push a new value onto a session value that is an array.
|
||
For instance, if the <code class="docutils literal notranslate"><span class="pre">hobbies</span></code> key contains an array of hobbies, you can add a new value onto the array like so:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">push</span><span class="p">(</span><span class="s1">'hobbies'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'sport'</span> <span class="o">=></span> <span class="s1">'tennis'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="removing-session-data">
|
||
<h3><a class="toc-backref" href="#id10" role="doc-backlink">Removing Session Data</a><a class="headerlink" href="#removing-session-data" title="Permalink to this heading"></a></h3>
|
||
<p>Just as with any other variable, unsetting a value in <code class="docutils literal notranslate"><span class="pre">$_SESSION</span></code> can be
|
||
done through <code class="docutils literal notranslate"><span class="pre">unset()</span></code>:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nb">unset</span><span class="p">(</span><span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">'some_name'</span><span class="p">]);</span>
|
||
<span class="c1">// or multiple values:</span>
|
||
<span class="nb">unset</span><span class="p">(</span>
|
||
<span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">'some_name'</span><span class="p">],</span>
|
||
<span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">'another_name'</span><span class="p">]</span>
|
||
<span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Also, just as <code class="docutils literal notranslate"><span class="pre">set()</span></code> can be used to add information to a
|
||
session, <code class="docutils literal notranslate"><span class="pre">remove()</span></code> can be used to remove it, by passing the
|
||
session key. For example, if you wanted to remove <code class="docutils literal notranslate"><span class="pre">some_name</span></code> from your
|
||
session data array:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">remove</span><span class="p">(</span><span class="s1">'some_name'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This method also accepts an array of item keys to unset:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$array_items</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'username'</span><span class="p">,</span> <span class="s1">'email'</span><span class="p">];</span>
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">remove</span><span class="p">(</span><span class="nv">$array_items</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="flashdata">
|
||
<span id="sessions-flashdata"></span><h3><a class="toc-backref" href="#id11" role="doc-backlink">Flashdata</a><a class="headerlink" href="#flashdata" title="Permalink to this heading"></a></h3>
|
||
<p>CodeIgniter supports “flashdata”, or session data that will only be
|
||
available for the next request, and is then automatically cleared.</p>
|
||
<p>This can be very useful, especially for one-time informational, error or
|
||
status messages (for example: “Record 2 deleted”).</p>
|
||
<p>It should be noted that flashdata variables are regular session variables,
|
||
managed inside the CodeIgniter session handler.</p>
|
||
<p>To mark an existing item as “flashdata”:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">markAsFlashdata</span><span class="p">(</span><span class="s1">'item'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If you want to mark multiple items as flashdata, simply pass the keys as an
|
||
array:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">markAsFlashdata</span><span class="p">([</span><span class="s1">'item'</span><span class="p">,</span> <span class="s1">'item2'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>To add flashdata:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">'item'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'value'</span><span class="p">;</span>
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">markAsFlashdata</span><span class="p">(</span><span class="s1">'item'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Or alternatively, using the <code class="docutils literal notranslate"><span class="pre">setFlashdata()</span></code> method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">setFlashdata</span><span class="p">(</span><span class="s1">'item'</span><span class="p">,</span> <span class="s1">'value'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You can also pass an array to <code class="docutils literal notranslate"><span class="pre">setFlashdata()</span></code>, in the same manner as
|
||
<code class="docutils literal notranslate"><span class="pre">set()</span></code>.</p>
|
||
<p>Reading flashdata variables is the same as reading regular session data
|
||
through <code class="docutils literal notranslate"><span class="pre">$_SESSION</span></code>:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$item</span> <span class="o">=</span> <span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">'item'</span><span class="p">];</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition important">
|
||
<p class="admonition-title">Important</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">get()</span></code> method WILL return flashdata items when
|
||
retrieving a single item by key. It will not return flashdata when
|
||
grabbing all data from the session, however.</p>
|
||
</div>
|
||
<p>However, if you want to be sure that you’re reading “flashdata” (and not
|
||
any other kind), you can also use the <code class="docutils literal notranslate"><span class="pre">getFlashdata()</span></code> method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">getFlashdata</span><span class="p">(</span><span class="s1">'item'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">getFlashdata()</span></code> method returns null if the item cannot be
|
||
found.</p>
|
||
</div>
|
||
<p>Or to get an array with all flashdata, simply omit the key parameter:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">getFlashdata</span><span class="p">();</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If you find that you need to preserve a flashdata variable through an
|
||
additional request, you can do so using the <code class="docutils literal notranslate"><span class="pre">keepFlashdata()</span></code> method.
|
||
You can either pass a single item or an array of flashdata items to keep.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">keepFlashdata</span><span class="p">(</span><span class="s1">'item'</span><span class="p">);</span>
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">keepFlashdata</span><span class="p">([</span><span class="s1">'item1'</span><span class="p">,</span> <span class="s1">'item2'</span><span class="p">,</span> <span class="s1">'item3'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="tempdata">
|
||
<h3><a class="toc-backref" href="#id12" role="doc-backlink">Tempdata</a><a class="headerlink" href="#tempdata" title="Permalink to this heading"></a></h3>
|
||
<p>CodeIgniter also supports “tempdata”, or session data with a specific
|
||
expiration time. After the value expires, or the session expires or is
|
||
deleted, the value is automatically removed.</p>
|
||
<p>Similarly to flashdata, tempdata variables are managed internally by the
|
||
CodeIgniter session handler.</p>
|
||
<p>To mark an existing item as “tempdata”, simply pass its key and expiry time
|
||
(in seconds!) to the <code class="docutils literal notranslate"><span class="pre">markAsTempdata()</span></code> method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="c1">// 'item' will be erased after 300 seconds</span>
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">markAsTempdata</span><span class="p">(</span><span class="s1">'item'</span><span class="p">,</span> <span class="mi">300</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You can mark multiple items as tempdata in two ways, depending on whether
|
||
you want them all to have the same expiry time or not:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="c1">// Both 'item' and 'item2' will expire after 300 seconds</span>
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">markAsTempdata</span><span class="p">([</span><span class="s1">'item'</span><span class="p">,</span> <span class="s1">'item2'</span><span class="p">],</span> <span class="mi">300</span><span class="p">);</span>
|
||
|
||
<span class="c1">// 'item' will be erased after 300 seconds, while 'item2'</span>
|
||
<span class="c1">// will do so after only 240 seconds</span>
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">markAsTempdata</span><span class="p">([</span>
|
||
<span class="s1">'item'</span> <span class="o">=></span> <span class="mi">300</span><span class="p">,</span>
|
||
<span class="s1">'item2'</span> <span class="o">=></span> <span class="mi">240</span><span class="p">,</span>
|
||
<span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>To add tempdata:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">'item'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'value'</span><span class="p">;</span>
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">markAsTempdata</span><span class="p">(</span><span class="s1">'item'</span><span class="p">,</span> <span class="mi">300</span><span class="p">);</span> <span class="c1">// Expire in 5 minutes</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Or alternatively, using the <code class="docutils literal notranslate"><span class="pre">setTempdata()</span></code> method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">setTempdata</span><span class="p">(</span><span class="s1">'item'</span><span class="p">,</span> <span class="s1">'value'</span><span class="p">,</span> <span class="mi">300</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You can also pass an array to <code class="docutils literal notranslate"><span class="pre">setTempdata()</span></code>:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$tempdata</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'newuser'</span> <span class="o">=></span> <span class="k">true</span><span class="p">,</span> <span class="s1">'message'</span> <span class="o">=></span> <span class="s1">'Thanks for joining!'</span><span class="p">];</span>
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">setTempdata</span><span class="p">(</span><span class="nv">$tempdata</span><span class="p">,</span> <span class="k">null</span><span class="p">,</span> <span class="nv">$expire</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>If the expiration is omitted or set to 0, the default
|
||
time-to-live value of 300 seconds (or 5 minutes) will be used.</p>
|
||
</div>
|
||
<p>To read a tempdata variable, again you can just access it through the
|
||
<code class="docutils literal notranslate"><span class="pre">$_SESSION</span></code> superglobal array:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$item</span> <span class="o">=</span> <span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">'item'</span><span class="p">];</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition important">
|
||
<p class="admonition-title">Important</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">get()</span></code> method WILL return tempdata items when
|
||
retrieving a single item by key. It will not return tempdata when
|
||
grabbing all data from the session, however.</p>
|
||
</div>
|
||
<p>Or if you want to be sure that you’re reading “tempdata” (and not any
|
||
other kind), you can also use the <code class="docutils literal notranslate"><span class="pre">getTempdata()</span></code> method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">getTempdata</span><span class="p">(</span><span class="s1">'item'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">getTempdata()</span></code> method returns null if the item cannot be
|
||
found.</p>
|
||
</div>
|
||
<p>And of course, if you want to retrieve all existing tempdata:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">getTempdata</span><span class="p">();</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If you need to remove a tempdata value before it expires, you can directly
|
||
unset it from the <code class="docutils literal notranslate"><span class="pre">$_SESSION</span></code> array:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nb">unset</span><span class="p">(</span><span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">'item'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>However, this won’t remove the marker that makes this specific item to be
|
||
tempdata (it will be invalidated on the next HTTP request), so if you
|
||
intend to reuse that same key in the same request, you’d want to use
|
||
<code class="docutils literal notranslate"><span class="pre">removeTempdata()</span></code>:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">removeTempdata</span><span class="p">(</span><span class="s1">'item'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="closing-a-session">
|
||
<h3><a class="toc-backref" href="#id13" role="doc-backlink">Closing a Session</a><a class="headerlink" href="#closing-a-session" title="Permalink to this heading"></a></h3>
|
||
<section id="close">
|
||
<span id="session-close"></span><h4>close()<a class="headerlink" href="#close" title="Permalink to this heading"></a></h4>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 4.4.0.</span></p>
|
||
</div>
|
||
<p>To close the current session manually after you no longer need it, use the
|
||
<code class="docutils literal notranslate"><span class="pre">close()</span></code> method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">close</span><span class="p">();</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You do not have to close the session manually, PHP will close it automatically
|
||
after your script terminated. But as session data is locked to prevent concurrent
|
||
writes only one request may operate on a session at any time. You may improve
|
||
your site performance by closing the session as soon as all changes to session
|
||
data are done.</p>
|
||
<p>This method will work in exactly the same way as PHP’s
|
||
<a class="reference external" href="https://www.php.net/session_write_close">session_write_close()</a> function.</p>
|
||
</section>
|
||
</section>
|
||
<section id="destroying-a-session">
|
||
<h3><a class="toc-backref" href="#id14" role="doc-backlink">Destroying a Session</a><a class="headerlink" href="#destroying-a-session" title="Permalink to this heading"></a></h3>
|
||
<section id="destroy">
|
||
<span id="session-destroy"></span><h4>destroy()<a class="headerlink" href="#destroy" title="Permalink to this heading"></a></h4>
|
||
<p>To clear the current session (for example, during a logout), you may
|
||
simply use the library’s <code class="docutils literal notranslate"><span class="pre">destroy()</span></code> method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="nv">$session</span><span class="o">-></span><span class="na">destroy</span><span class="p">();</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This method will work in exactly the same way as PHP’s
|
||
<a class="reference external" href="https://www.php.net/session_destroy">session_destroy()</a> function.</p>
|
||
<p>This must be the last session-related operation that you do during the same request.
|
||
All session data (including flashdata and tempdata) will be destroyed permanently.</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>You do not have to call this method from usual code. Cleanup session
|
||
data rather than destroying the session.</p>
|
||
</div>
|
||
</section>
|
||
<section id="stop">
|
||
<span id="session-stop"></span><h4>stop()<a class="headerlink" href="#stop" title="Permalink to this heading"></a></h4>
|
||
<div class="deprecated">
|
||
<p><span class="versionmodified deprecated">Deprecated since version 4.3.5.</span></p>
|
||
</div>
|
||
<p>The session class also has the <code class="docutils literal notranslate"><span class="pre">stop()</span></code> method.</p>
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">Warning</p>
|
||
<p>Prior to v4.3.5, this method did not destroy the session due to a bug.</p>
|
||
</div>
|
||
<p>Starting with v4.3.5, this method has been modified to destroy the session.
|
||
However, it is deprecated because it is exactly the same as the <code class="docutils literal notranslate"><span class="pre">destroy()</span></code>
|
||
method. Use the <code class="docutils literal notranslate"><span class="pre">destroy()</span></code> method instead.</p>
|
||
</section>
|
||
</section>
|
||
<section id="accessing-session-metadata">
|
||
<h3><a class="toc-backref" href="#id15" role="doc-backlink">Accessing Session Metadata</a><a class="headerlink" href="#accessing-session-metadata" title="Permalink to this heading"></a></h3>
|
||
<p>In CodeIgniter 2, the session data array included 4 items
|
||
by default: ‘session_id’, ‘ip_address’, ‘user_agent’, ‘last_activity’.</p>
|
||
<p>This was due to the specifics of how sessions worked, but is now no longer
|
||
necessary with our new implementation. However, it may happen that your
|
||
application relied on these values, so here are alternative methods of
|
||
accessing them:</p>
|
||
<blockquote>
|
||
<div><ul class="simple">
|
||
<li><p>session_id: <code class="docutils literal notranslate"><span class="pre">$session->session_id</span></code> or <code class="docutils literal notranslate"><span class="pre">session_id()</span></code> (PHP’s built-in function)</p></li>
|
||
<li><p>ip_address: <code class="docutils literal notranslate"><span class="pre">$_SERVER['REMOTE_ADDR']</span></code></p></li>
|
||
<li><p>user_agent: <code class="docutils literal notranslate"><span class="pre">$_SERVER['HTTP_USER_AGENT']</span></code> (unused by sessions)</p></li>
|
||
<li><p>last_activity: Depends on the storage, no straightforward way. Sorry!</p></li>
|
||
</ul>
|
||
</div></blockquote>
|
||
</section>
|
||
</section>
|
||
<section id="session-preferences">
|
||
<h2><a class="toc-backref" href="#id16" role="doc-backlink">Session Preferences</a><a class="headerlink" href="#session-preferences" title="Permalink to this heading"></a></h2>
|
||
<p>CodeIgniter will usually make everything work out of the box. However,
|
||
Sessions are a very sensitive component of any application, so some
|
||
careful configuration must be done. Please take your time to consider
|
||
all of the options and their effects.</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Since v4.3.0, the new <strong>app/Config/Session.php</strong> has been added.
|
||
Previously, the Session Preferences were in your <strong>app/Config/App.php</strong> file.</p>
|
||
</div>
|
||
<p>You’ll find the following Session related preferences in your
|
||
<strong>app/Config/Session.php</strong> file:</p>
|
||
<table class="docutils align-default">
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>Preference</p></th>
|
||
<th class="head"><p>Default</p></th>
|
||
<th class="head"><p>Options</p></th>
|
||
<th class="head"><p>Description</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p><strong>driver</strong></p></td>
|
||
<td><p>CodeIgniter\Session\Handlers\FileHandler</p></td>
|
||
<td><p>CodeIgniter\Session\Handlers\FileHandler
|
||
CodeIgniter\Session\Handlers\DatabaseHandler
|
||
CodeIgniter\Session\Handlers\MemcachedHandler
|
||
CodeIgniter\Session\Handlers\RedisHandler
|
||
CodeIgniter\Session\Handlers\ArrayHandler</p></td>
|
||
<td><p>The session storage driver to use.</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>cookieName</strong></p></td>
|
||
<td><p>ci_session</p></td>
|
||
<td><p>[A-Za-z_-] characters only</p></td>
|
||
<td><p>The name used for the session cookie.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>expiration</strong></p></td>
|
||
<td><p>7200 (2 hours)</p></td>
|
||
<td><p>Time in seconds (integer)</p></td>
|
||
<td><p>The number of seconds you would like the session to last.
|
||
If you would like a non-expiring session (until browser is closed) set the value to zero: 0</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>savePath</strong></p></td>
|
||
<td><p>null</p></td>
|
||
<td><p>None</p></td>
|
||
<td><p>Specifies the storage location, depends on the driver being used.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>matchIP</strong></p></td>
|
||
<td><p>false</p></td>
|
||
<td><p>true/false (boolean)</p></td>
|
||
<td><p>Whether to validate the user’s IP address when reading the session cookie.
|
||
Note that some ISPs dynamically changes the IP, so if you want a non-expiring session you
|
||
will likely set this to false.</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>timeToUpdate</strong></p></td>
|
||
<td><p>300</p></td>
|
||
<td><p>Time in seconds (integer)</p></td>
|
||
<td><p>This option controls how often the session class will regenerate itself and create a new
|
||
session ID. Setting it to 0 will disable session ID regeneration.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>regenerateDestroy</strong></p></td>
|
||
<td><p>false</p></td>
|
||
<td><p>true/false (boolean)</p></td>
|
||
<td><p>Whether to destroy session data associated with the old session ID when auto-regenerating
|
||
the session ID. When set to false, the data will be later deleted by the garbage collector.</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>As a last resort, the Session library will try to fetch PHP’s
|
||
session related INI settings, as well as CodeIgniter 3 settings such as
|
||
‘sess_expire_on_close’ when any of the above is not configured.
|
||
However, you should never rely on this behavior as it can cause
|
||
unexpected results or be changed in the future. Please configure
|
||
everything properly.</p>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>If <code class="docutils literal notranslate"><span class="pre">expiration</span></code> is set to <code class="docutils literal notranslate"><span class="pre">0</span></code>, the <code class="docutils literal notranslate"><span class="pre">session.gc_maxlifetime</span></code>
|
||
setting set by PHP in session management will be used as-is
|
||
(often the default value of <code class="docutils literal notranslate"><span class="pre">1440</span></code>). This needs to be changed in
|
||
<code class="docutils literal notranslate"><span class="pre">php.ini</span></code> or via <code class="docutils literal notranslate"><span class="pre">ini_set()</span></code> as needed.</p>
|
||
</div>
|
||
<p>In addition to the values above, the Session cookie uses the
|
||
following configuration values in your <strong>app/Config/Cookie.php</strong> file:</p>
|
||
<table class="docutils align-default">
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>Preference</p></th>
|
||
<th class="head"><p>Default</p></th>
|
||
<th class="head"><p>Description</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p><strong>domain</strong></p></td>
|
||
<td><p>‘’</p></td>
|
||
<td><p>The domain for which the session is applicable</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>path</strong></p></td>
|
||
<td><p>/</p></td>
|
||
<td><p>The path to which the session is applicable</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>secure</strong></p></td>
|
||
<td><p>false</p></td>
|
||
<td><p>Whether to create the session cookie only on encrypted (HTTPS) connections</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>sameSite</strong></p></td>
|
||
<td><p>Lax</p></td>
|
||
<td><p>The SameSite setting for the session cookie</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">httponly</span></code> setting (in <strong>app/Config/Cookie.php</strong>) doesn’t have an effect on sessions.
|
||
Instead the HttpOnly parameter is always enabled, for security
|
||
reasons. Additionally, the <code class="docutils literal notranslate"><span class="pre">Config\Cookie::$prefix</span></code> setting is completely
|
||
ignored.</p>
|
||
</div>
|
||
</section>
|
||
<section id="session-drivers">
|
||
<h2><a class="toc-backref" href="#id17" role="doc-backlink">Session Drivers</a><a class="headerlink" href="#session-drivers" title="Permalink to this heading"></a></h2>
|
||
<p>As already mentioned, the Session library comes with five handlers, or storage
|
||
engines, that you can use:</p>
|
||
<blockquote>
|
||
<div><ul class="simple">
|
||
<li><p>CodeIgniter\Session\Handlers\FileHandler</p></li>
|
||
<li><p>CodeIgniter\Session\Handlers\DatabaseHandler</p></li>
|
||
<li><p>CodeIgniter\Session\Handlers\MemcachedHandler</p></li>
|
||
<li><p>CodeIgniter\Session\Handlers\RedisHandler</p></li>
|
||
<li><p>CodeIgniter\Session\Handlers\ArrayHandler</p></li>
|
||
</ul>
|
||
</div></blockquote>
|
||
<p>By default, the <code class="docutils literal notranslate"><span class="pre">FileHandler</span></code> will be used when a session is initialized,
|
||
because it is the safest choice and is expected to work everywhere
|
||
(virtually every environment has a file system).</p>
|
||
<p>However, any other driver may be selected via the <code class="docutils literal notranslate"><span class="pre">$driver</span></code>
|
||
setting in your <strong>app/Config/Session.php</strong> file, if you chose to do so.
|
||
Have it in mind though, every driver has different caveats, so be sure to
|
||
get yourself familiar with them (below) before you make that choice.</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>The ArrayHandler is used during testing and stores all data within
|
||
a PHP array, while preventing the data from being persisted.</p>
|
||
</div>
|
||
<section id="filehandler-driver-the-default">
|
||
<h3><a class="toc-backref" href="#id18" role="doc-backlink">FileHandler Driver (the default)</a><a class="headerlink" href="#filehandler-driver-the-default" title="Permalink to this heading"></a></h3>
|
||
<p>The ‘FileHandler’ driver uses your file system for storing session data.</p>
|
||
<p>It can safely be said that it works exactly like PHP’s own default session
|
||
implementation, but in case this is an important detail for you, in fact it is not the same code
|
||
and it has some limitations (and advantages).</p>
|
||
<p>To be more specific, it doesn’t support PHP’s <a class="reference external" href="https://www.php.net/manual/en/session.configuration.php#ini.session.save-path">directory level and mode
|
||
formats used in session.save_path</a>,
|
||
and it has most of the options hard-coded for safety. Instead, only
|
||
absolute paths are supported with <code class="docutils literal notranslate"><span class="pre">$savePath</span></code> setting.</p>
|
||
<p>Another important thing that you should know, is to make sure that you
|
||
don’t use a publicly-readable or shared directory for storing your session
|
||
files. <em>Only you</em> should have access to the contents of your
|
||
chosen <em>savePath</em> directory. Otherwise, anybody can see and
|
||
steal session data (also known as “session fixation” attack).</p>
|
||
<p>On UNIX-like operating systems, this is usually achieved by setting the
|
||
0700 mode permissions on that directory via the <cite>chmod</cite> command, which
|
||
allows only the directory’s owner to perform read and write operations on
|
||
it. But be careful because the system user <em>running</em> the script is usually
|
||
not your own, but something like ‘www-data’ instead, so only setting those
|
||
permissions will probably break your application.</p>
|
||
<p>Instead, you should do something like this, depending on your environment:</p>
|
||
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="go">mkdir /<path to your application directory>/writable/sessions/</span>
|
||
<span class="go">chmod 0700 /<path to your application directory>/writable/sessions/</span>
|
||
<span class="go">chown www-data /<path to your application directory>/writable/sessions/</span>
|
||
</pre></div>
|
||
</div>
|
||
<section id="bonus-tip">
|
||
<h4>Bonus Tip<a class="headerlink" href="#bonus-tip" title="Permalink to this heading"></a></h4>
|
||
<p>Some of you will probably opt to choose another session driver because
|
||
file storage is usually slower. This is only half true.</p>
|
||
<p>A very basic test will probably trick you into believing that an SQL
|
||
database is faster, but in 99% of the cases, this is true only if you
|
||
have a few current sessions. As the sessions count and server loads
|
||
increase - which is the time when it matters - the file system will
|
||
consistently outperform almost all relational database setups.</p>
|
||
<p>In addition, if performance is your only concern, you may want to look
|
||
into using <a class="reference external" href="https://eddmann.com/posts/storing-php-sessions-file-caches-in-memory-using-tmpfs/">tmpfs</a>,
|
||
which can make your sessions blazing fast.</p>
|
||
</section>
|
||
</section>
|
||
<section id="databasehandler-driver">
|
||
<span id="sessions-databasehandler-driver"></span><h3><a class="toc-backref" href="#id19" role="doc-backlink">DatabaseHandler Driver</a><a class="headerlink" href="#databasehandler-driver" title="Permalink to this heading"></a></h3>
|
||
<div class="admonition important">
|
||
<p class="admonition-title">Important</p>
|
||
<p>Only MySQL and PostgreSQL databases are officially
|
||
supported, due to lack of advisory locking mechanisms on other
|
||
platforms. Using sessions without locks can cause all sorts of
|
||
problems, especially with heavy usage of AJAX. Use the <a class="reference internal" href="#session-close"><span class="std std-ref">close()</span></a> method
|
||
after you’ve done processing session data if you’re having performance issues.</p>
|
||
</div>
|
||
<p>The ‘DatabaseHandler’ driver uses a relational database such as MySQL or
|
||
PostgreSQL to store sessions. This is a popular choice among many users,
|
||
because it allows the developer easy access to the session data within
|
||
an application - it is just another table in your database.</p>
|
||
<p>However, there is a restriction: You can NOT use a persistent connection.</p>
|
||
<section id="configure-databasehandler">
|
||
<h4>Configure DatabaseHandler<a class="headerlink" href="#configure-databasehandler" title="Permalink to this heading"></a></h4>
|
||
<section id="setting-table-name">
|
||
<h5>Setting Table Name<a class="headerlink" href="#setting-table-name" title="Permalink to this heading"></a></h5>
|
||
<p>In order to use the ‘DatabaseHandler’ session driver, you must also create this
|
||
table that we already mentioned and then set it as your
|
||
<code class="docutils literal notranslate"><span class="pre">$savePath</span></code> value.
|
||
For example, if you would like to use ‘ci_sessions’ as your table name,
|
||
you would do this:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Config\BaseConfig</span><span class="p">;</span>
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Session\Handlers\FileHandler</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Session</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="k">public</span> <span class="nx">string</span> <span class="nv">$driver</span> <span class="o">=</span> <span class="s1">'CodeIgniter\Session\Handlers\DatabaseHandler'</span><span class="p">;</span>
|
||
|
||
<span class="c1">// ...</span>
|
||
<span class="k">public</span> <span class="nx">string</span> <span class="nv">$savePath</span> <span class="o">=</span> <span class="s1">'ci_sessions'</span><span class="p">;</span>
|
||
|
||
<span class="c1">// ...</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="creating-database-table">
|
||
<h5>Creating Database Table<a class="headerlink" href="#creating-database-table" title="Permalink to this heading"></a></h5>
|
||
<p>And then of course, create the database table.</p>
|
||
<p>For MySQL:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">CREATE</span> <span class="nx">TABLE</span> <span class="k">IF</span> <span class="k">NOT</span> <span class="nx">EXISTS</span> <span class="sb">`ci_sessions`</span> <span class="p">(</span>
|
||
<span class="sb">`id`</span> <span class="nx">varchar</span><span class="p">(</span><span class="mi">128</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">null</span><span class="p">,</span>
|
||
<span class="sb">`ip_address`</span> <span class="nx">varchar</span><span class="p">(</span><span class="mi">45</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">null</span><span class="p">,</span>
|
||
<span class="sb">`timestamp`</span> <span class="nx">timestamp</span> <span class="k">DEFAULT</span> <span class="nx">CURRENT_TIMESTAMP</span> <span class="k">NOT</span> <span class="k">null</span><span class="p">,</span>
|
||
<span class="sb">`data`</span> <span class="nx">blob</span> <span class="k">NOT</span> <span class="k">null</span><span class="p">,</span>
|
||
<span class="nx">KEY</span> <span class="sb">`ci_sessions_timestamp`</span> <span class="p">(</span><span class="sb">`timestamp`</span><span class="p">)</span>
|
||
<span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>For PostgreSQL:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">CREATE</span> <span class="nx">TABLE</span> <span class="s2">"ci_sessions"</span> <span class="p">(</span>
|
||
<span class="s2">"id"</span> <span class="nx">varchar</span><span class="p">(</span><span class="mi">128</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
|
||
<span class="s2">"ip_address"</span> <span class="nx">inet</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
|
||
<span class="s2">"timestamp"</span> <span class="nx">timestamptz</span> <span class="k">DEFAULT</span> <span class="nx">CURRENT_TIMESTAMP</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
|
||
<span class="s2">"data"</span> <span class="nx">bytea</span> <span class="k">DEFAULT</span> <span class="s1">''</span> <span class="k">NOT</span> <span class="k">NULL</span>
|
||
<span class="p">);</span>
|
||
|
||
<span class="nx">CREATE</span> <span class="nx">INDEX</span> <span class="s2">"ci_sessions_timestamp"</span> <span class="nx">ON</span> <span class="s2">"ci_sessions"</span> <span class="p">(</span><span class="s2">"timestamp"</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">id</span></code> value contains the session cookie name (<code class="docutils literal notranslate"><span class="pre">Config\Session::$cookieName</span></code>)
|
||
and the session ID and a delimiter. It should be increased as needed, for example,
|
||
when using long session IDs.</p>
|
||
</div>
|
||
</section>
|
||
<section id="adding-primary-key">
|
||
<h5>Adding Primary Key<a class="headerlink" href="#adding-primary-key" title="Permalink to this heading"></a></h5>
|
||
<p>You will also need to add a PRIMARY KEY <strong>depending on your $matchIP
|
||
setting</strong>. The examples below work both on MySQL and PostgreSQL:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// When $matchIP = true</span>
|
||
<span class="nx">ALTER</span> <span class="nx">TABLE</span> <span class="nx">ci_sessions</span> <span class="nx">ADD</span> <span class="nx">PRIMARY</span> <span class="nx">KEY</span> <span class="p">(</span><span class="nx">id</span><span class="p">,</span> <span class="nx">ip_address</span><span class="p">);</span>
|
||
|
||
<span class="c1">// When $matchIP = false</span>
|
||
<span class="nx">ALTER</span> <span class="nx">TABLE</span> <span class="nx">ci_sessions</span> <span class="nx">ADD</span> <span class="nx">PRIMARY</span> <span class="nx">KEY</span> <span class="p">(</span><span class="nx">id</span><span class="p">);</span>
|
||
|
||
<span class="c1">// To drop a previously created primary key (use when changing the setting)</span>
|
||
<span class="nx">ALTER</span> <span class="nx">TABLE</span> <span class="nx">ci_sessions</span> <span class="nx">DROP</span> <span class="nx">PRIMARY</span> <span class="nx">KEY</span><span class="p">;</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition important">
|
||
<p class="admonition-title">Important</p>
|
||
<p>If you don’t add the correct primary key, the following error
|
||
may occur:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">Uncaught</span> <span class="nx">mysqli_sql_exception</span><span class="o">:</span> <span class="nx">Duplicate</span> <span class="nx">entry</span> <span class="s1">'ci_session:***'</span> <span class="k">for</span> <span class="nb">key</span> <span class="s1">'ci_sessions.PRIMARY'</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="changing-database-group">
|
||
<h5>Changing Database Group<a class="headerlink" href="#changing-database-group" title="Permalink to this heading"></a></h5>
|
||
<p>The default database group is used by default.
|
||
You can change the database group to use by changing the <code class="docutils literal notranslate"><span class="pre">$DBGroup</span></code> property
|
||
in the <strong>app/Config/Session.php</strong> file to the name of the group to use:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Config\BaseConfig</span><span class="p">;</span>
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Session\Handlers\FileHandler</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Session</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="k">public</span> <span class="o">?</span><span class="nx">string</span> <span class="nv">$DBGroup</span> <span class="o">=</span> <span class="s1">'groupName'</span><span class="p">;</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="setting-up-database-table-with-command">
|
||
<h5>Setting Up Database Table with Command<a class="headerlink" href="#setting-up-database-table-with-command" title="Permalink to this heading"></a></h5>
|
||
<p>If you’d rather not do all of this by hand, you can use the <code class="docutils literal notranslate"><span class="pre">make:migration</span> <span class="pre">--session</span></code> command
|
||
from the cli to generate a migration file for you:</p>
|
||
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="go">php spark make:migration --session</span>
|
||
<span class="go">php spark migrate</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This command will take the <code class="docutils literal notranslate"><span class="pre">$savePath</span></code> and <code class="docutils literal notranslate"><span class="pre">$matchIP</span></code> settings into account
|
||
when it generates the code.</p>
|
||
</section>
|
||
</section>
|
||
</section>
|
||
<section id="redishandler-driver">
|
||
<span id="sessions-redishandler-driver"></span><h3><a class="toc-backref" href="#id20" role="doc-backlink">RedisHandler Driver</a><a class="headerlink" href="#redishandler-driver" title="Permalink to this heading"></a></h3>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Since Redis doesn’t have a locking mechanism exposed, locks for
|
||
this driver are emulated by a separate value that is kept for up
|
||
to 300 seconds.</p>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Starting with v4.3.2, you can connect Redis with <strong>TLS</strong> protocol.</p>
|
||
</div>
|
||
<p>Redis is a storage engine typically used for caching and popular because
|
||
of its high performance, which is also probably your reason to use the
|
||
‘RedisHandler’ session driver.</p>
|
||
<p>The downside is that it is not as ubiquitous as relational databases and
|
||
requires the <a class="reference external" href="https://github.com/phpredis/phpredis">phpredis</a> PHP
|
||
extension to be installed on your system, and that one doesn’t come
|
||
bundled with PHP.
|
||
Chances are, you’re using the RedisHandler driver only if you’re already
|
||
both familiar with Redis and using it for other purposes.</p>
|
||
<section id="configure-redishandler">
|
||
<h4>Configure RedisHandler<a class="headerlink" href="#configure-redishandler" title="Permalink to this heading"></a></h4>
|
||
<p>Just as with the ‘FileHandler’ and ‘DatabaseHandler’ drivers, you must also configure
|
||
the storage location for your sessions via the
|
||
<code class="docutils literal notranslate"><span class="pre">$savePath</span></code> setting.
|
||
The format here is a bit different and complicated. It is
|
||
best explained by the <em>phpredis</em> extension’s README file, so we’ll simply
|
||
give a link to it:</p>
|
||
<blockquote>
|
||
<div><p><a class="reference external" href="https://github.com/phpredis/phpredis">https://github.com/phpredis/phpredis</a></p>
|
||
</div></blockquote>
|
||
<p>For the most common case however, a simple <code class="docutils literal notranslate"><span class="pre">host:port</span></code> pair should be
|
||
sufficient:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Config\BaseConfig</span><span class="p">;</span>
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Session\Handlers\FileHandler</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Session</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="k">public</span> <span class="nx">string</span> <span class="nv">$driver</span> <span class="o">=</span> <span class="s1">'CodeIgniter\Session\Handlers\RedisHandler'</span><span class="p">;</span>
|
||
|
||
<span class="c1">// ...</span>
|
||
<span class="k">public</span> <span class="nx">string</span> <span class="nv">$savePath</span> <span class="o">=</span> <span class="s1">'tcp://localhost:6379'</span><span class="p">;</span>
|
||
|
||
<span class="c1">// ...</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Starting with v4.5.0, you can use Redis ACL (username and password):</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="nx">string</span> <span class="nv">$savePath</span> <span class="o">=</span> <span class="s1">'tcp://localhost:6379?auth[user]=username&auth[pass]=password'</span><span class="p">;</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Starting with v4.5.0, the interval time for acquiring locks
|
||
(<code class="docutils literal notranslate"><span class="pre">$lockRetryInterval</span></code>) and the number of retries (<code class="docutils literal notranslate"><span class="pre">$lockMaxRetries</span></code>) are
|
||
configurable.</p>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="memcachedhandler-driver">
|
||
<span id="sessions-memcachedhandler-driver"></span><h3><a class="toc-backref" href="#id21" role="doc-backlink">MemcachedHandler Driver</a><a class="headerlink" href="#memcachedhandler-driver" title="Permalink to this heading"></a></h3>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Since Memcached doesn’t have a locking mechanism exposed, locks
|
||
for this driver are emulated by a separate value that is kept for
|
||
up to 300 seconds.</p>
|
||
</div>
|
||
<p>The ‘MemcachedHandler’ driver is very similar to the ‘RedisHandler’ one in all of its
|
||
properties, except perhaps for availability, because PHP’s <a class="reference external" href="https://www.php.net/memcached">Memcached</a> extension is distributed via PECL and some
|
||
Linux distributions make it available as an easy to install package.</p>
|
||
<p>Other than that, and without any intentional bias towards Redis, there’s
|
||
not much different to be said about Memcached - it is also a popular
|
||
product that is usually used for caching and famed for its speed.</p>
|
||
<p>However, it is worth noting that the only guarantee given by Memcached
|
||
is that setting value X to expire after Y seconds will result in it being
|
||
deleted after Y seconds have passed (but not necessarily that it won’t
|
||
expire earlier than that time). This happens very rarely, but should be
|
||
considered as it may result in loss of sessions.</p>
|
||
<section id="configure-memcachedhandler">
|
||
<h4>Configure MemcachedHandler<a class="headerlink" href="#configure-memcachedhandler" title="Permalink to this heading"></a></h4>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">$savePath</span></code> format is fairly straightforward here,
|
||
being just a <code class="docutils literal notranslate"><span class="pre">host:port</span></code> pair:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Config\BaseConfig</span><span class="p">;</span>
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Session\Handlers\FileHandler</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Session</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="k">public</span> <span class="nx">string</span> <span class="nv">$driver</span> <span class="o">=</span> <span class="s1">'CodeIgniter\Session\Handlers\MemcachedHandler'</span><span class="p">;</span>
|
||
|
||
<span class="c1">// ...</span>
|
||
<span class="k">public</span> <span class="nx">string</span> <span class="nv">$savePath</span> <span class="o">=</span> <span class="s1">'localhost:11211'</span><span class="p">;</span>
|
||
|
||
<span class="c1">// ...</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="id2">
|
||
<h4>Bonus Tip<a class="headerlink" href="#id2" title="Permalink to this heading"></a></h4>
|
||
<p>Multi-server configuration with an optional <em>weight</em> parameter as the
|
||
third colon-separated (<code class="docutils literal notranslate"><span class="pre">:weight</span></code>) value is also supported, but we have
|
||
to note that we haven’t tested if that is reliable.</p>
|
||
<p>If you want to experiment with this feature (on your own risk), simply
|
||
separate the multiple server paths with commas:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Config\BaseConfig</span><span class="p">;</span>
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Session\Handlers\FileHandler</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Session</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
|
||
<span class="c1">// localhost will be given higher priority (5) here,</span>
|
||
<span class="c1">// compared to 192.0.2.1 with a weight of 1.</span>
|
||
<span class="k">public</span> <span class="nx">string</span> <span class="nv">$savePath</span> <span class="o">=</span> <span class="s1">'localhost:11211:5,192.0.2.1:11211:1'</span><span class="p">;</span>
|
||
|
||
<span class="c1">// ...</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
|
||
<a href="security.html" class="btn btn-neutral float-left" title="Security" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
|
||
<a href="throttler.html" class="btn btn-neutral float-right" title="Throttler" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
|
||
</div>
|
||
|
||
<hr/>
|
||
|
||
<div role="contentinfo">
|
||
<p>© Copyright 2019-2025 CodeIgniter Foundation.
|
||
<span class="lastupdated">Last updated on Feb 07, 2025.
|
||
</span></p>
|
||
</div>
|
||
|
||
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
|
||
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
|
||
provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
||
|
||
|
||
</footer>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
<script>
|
||
jQuery(function () {
|
||
SphinxRtdTheme.Navigation.enable(false);
|
||
});
|
||
</script>
|
||
|
||
</body>
|
||
</html> |