{"id":18929422,"url":"https://github.com/thecodingmachine/utils.session.optimistic-session-handler","last_synced_at":"2025-04-15T15:30:57.672Z","repository":{"id":36177881,"uuid":"40482006","full_name":"thecodingmachine/utils.session.optimistic-session-handler","owner":"thecodingmachine","description":"Session handler that releases session lock quickly. Usefull for multiple ajax calls on the same page","archived":false,"fork":false,"pushed_at":"2021-07-06T15:44:34.000Z","size":44,"stargazers_count":6,"open_issues_count":2,"forks_count":12,"subscribers_count":9,"default_branch":"2.0","last_synced_at":"2025-04-09T14:03:31.782Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thecodingmachine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-10T12:44:53.000Z","updated_at":"2021-07-06T15:36:15.000Z","dependencies_parsed_at":"2022-08-18T18:20:08.066Z","dependency_job_id":null,"html_url":"https://github.com/thecodingmachine/utils.session.optimistic-session-handler","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Futils.session.optimistic-session-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Futils.session.optimistic-session-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Futils.session.optimistic-session-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Futils.session.optimistic-session-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thecodingmachine","download_url":"https://codeload.github.com/thecodingmachine/utils.session.optimistic-session-handler/tar.gz/refs/heads/2.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249097816,"owners_count":21212358,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T11:32:38.677Z","updated_at":"2025-04-15T15:30:57.400Z","avatar_url":"https://github.com/thecodingmachine.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Latest Stable Version](https://poser.pugx.org/mouf/utils.session.optimistic-session-handler/v/stable)](https://packagist.org/packages/mouf/utils.session.optimistic-session-handler)\n[![Latest Unstable Version](https://poser.pugx.org/mouf/utils.session.optimistic-session-handler/v/unstable)](https://packagist.org/packages/mouf/utils.session.optimistic-session-handler)\n[![License](https://poser.pugx.org/mouf/utils.session.optimistic-session-handler/license)](https://packagist.org/packages/mouf/utils.session.optimistic-session-handler)\n[![Build Status](https://travis-ci.org/thecodingmachine/utils.session.optimistic-session-handler.svg?branch=1.0)](https://travis-ci.org/thecodingmachine/utils.session.optimistic-session-handler)\n\n\n# OptimisticSessionHandler\nPHP file-based session handler that **releases session lock quickly**. Useful to speed up multiple Ajax calls on the same page.\n\n## Why do we need this session handler?\nIt improves performances in case of projects with multiple and long Ajax requests running.\nSeveral requests using the user session can be concurrently executed by the server since the session is not locked for a long time.\n\nBy default, PHP writes session files on the disk. When you execute `session_start`, the session file is opened, and a lock is \nput on the file. If another process tries to perform a `session_start`, the process will wait until the lock on the session file is released.\nThis is a security feature of PHP (2 processes cannot modify the same session at the same time), but this is dreadful for performances,\nas PHP requests sharing the same session must be run sequentially.\n\nThis package offers a way around this problem. It assumes that *everything is going to be alright* (hence the \"optimistic\" name),\nand let several processes access the session at the same time. If two processes modify the session at the same time, it will\ntry to merge the 2 results. If it fails to do so, it will throw an exception.\n\n## How does it work?\nThis session handler modifies the default behaviour of PHP session handling.\nSessions are still written to disk (like PHP does by default).\nThe session is opened and read when you call `session_start()` to fill the global variable `$_SESSION`.\nBut the session is closed immediately after.\nAt the end of your PHP script, the `$_SESSION` is compared with the old session. If the session has been modified in your script,\nthe handler re-opens a session and compare the new session with your changes. The merged session is saved.\n\n*Note:* if you use an alternative session handler (like APC or Memcache), do not use this session handler. It is designed to be\nused with file based sessions.\n\n## Using the session handler\nIt's extremely easy to use.\nJust declare a new instance :\n\n    $handler = new OptimisticSessionHandler();\n\nAnd save it as your default session handler :\n\n    session_set_save_handler($handler, true);\n\nThen you can start the session as usual\n\n    session_start(['read_and_close' =\u003e true]);\n\nThen the `$_SESSION` array is accessible.\n\nYou can configure rules for managing conflicts. Just add element to the class parameter $conflictRules.\nThe possible rules are:\n\n* IGNORE =\u003e Don't use the current change.\n* OVERWRITE =\u003e Use the current change.\n* FAIL =\u003e Throw exception.\n\nSo you can just declare a new instance like this:\n\n    $handler = new OptimisticSessionHandler(array(\"key_to_override\" =\u003e OptimisticSessionHandler::OVERRIDE));\n\n### Destroying the session\n**Warning:** The session can't be destroyed by `session_destroy()` (It will throw an error). To destroy the session, you must empty the `$_SESSION` array.\n\n    $_SESSION = array();\n\nIf you want more information about this package you can go on [OptimisticSessionHandler: A New Way To Think PHP Sessions](http://www.thecodingmachine.com/optimisticsessionhandler-a-new-way-to-think-php-sessions/)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Futils.session.optimistic-session-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecodingmachine%2Futils.session.optimistic-session-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Futils.session.optimistic-session-handler/lists"}